<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="120px">
          <vab-query-form>
            <el-form-item width="100" prop="ldDeviceSerial" label="力鼎序列号">
              <el-input
                v-model="queryForm.ldDeviceSerial"
                size="small"
                placeholder="请输入力鼎序列号"
              ></el-input>
            </el-form-item>
            <el-form-item width="100" prop="tuyaDeviceId" label="涂鸦设备ID">
              <el-input
                v-model="queryForm.tuyaDeviceId"
                size="small"
                placeholder="请输入力鼎序列号"
              ></el-input>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">重置</el-button>
        <el-button type="primary" size="small" @click="search">查询</el-button>
      </div>
    </div>
    <div class="manage-button">
      <el-button type="primary" size="small" @click="registerDevice()">
        注册设备
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="flowData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="tuyaDeviceName"
        label="涂鸦平台设备名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="ldDeviceSerial"
        label="站点序列号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tuyaDeviceId"
        label="涂鸦设备id"
        align="center"
      ></el-table-column>

      <el-table-column fixed="right" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">删除设备</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageIndex"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>

    <bind-device ref="device"></bind-device>
  </div>
</template>
<script>
import { getList, deleteDevice } from '@/api/tuya';

import bindDevice from './bindDevice';
export default {
  name: 'Index',
  components: {
    bindDevice,
  },
  data() {
    return {
      flowData: [],
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      statusData: [],
      queryForm: {
        page: 1,
        size: 20,
        tuyaDeviceId: '',
        ldDeviceSerial: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.flowData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    registerDevice(row) {
      this.$refs['device'].showDetail();
    },
    ShowDetail(row) {
      this.$refs['details'].showDetail(row.accNumber);
    },
    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) {
      let that = this;
      if (row.tuyaDeviceId) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const { msg } = await deleteDevice(row.tuyaDeviceId);
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(e => {
            console.log(e);
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-wrap {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .el-input {
    width: 200px !important;
  }
  .el-select {
    width: 200px !important;
  }
}
.manage-button {
  padding-left: 12px;
  margin-bottom: 10px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
</style>
