<template>
  <div class="manage-container">
    <div class="manage-button">
      <el-button type="primary" size="small" @click="handleAdd()">
        添加萤石云
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="fluoriteData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="account"
        label="账户名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="appKey"
        label="appKey"
        width="220"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="appSecret"
        label="appSecret"
        width="220"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="captureCycle"
        label="抓拍周期"
        width="90"
        align="center"
      ></el-table-column>
      <el-table-column prop="enableStatus" label="是否抓拍" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.captureFlag"
            active-value="1"
            inactive-value="0"
            @click="updateFlag(scope.row.captureFlag, scope.row.account)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="添加时间"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="编辑时间"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        width="180"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="sensorData(row)">设备</el-button>
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <fluorite-add ref="edit"></fluorite-add>
  </div>
</template>
<script>
import { doDelete, getList, capture } from '@/api/fluorite';
import fluoriteAdd from './add';
export default {
  name: 'Index',
  components: {
    fluoriteAdd,
  },
  data() {
    return {
      fluoriteData: [],
      deviceId: '',
      projectId: '',
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      console.log(data);
      this.fluoriteData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.deviceId, 0);
    },
    handleEdit(row) {
      row.deviceId = this.deviceId;
      this.$refs['edit'].showEdit(row, 1);
    },
    sensorData(row) {
      this.$router.push({
        path: '/videoData',
        name: '设备列表',
        query: { account: row.account },
      });
    },
    async updateFlag(captureFlag, account) {
      const { code, msg } = await capture({
        captureFlag: parseInt(captureFlag),
        account: account,
      });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    handleDelete(row) {
      const that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete({ id: row.id });
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  margin-bottom: 30px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
      .el-cascader {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
