<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="100px">
          <vab-query-form>
            <el-form-item
              width="130"
              prop="sensorName"
              :label="$t('sensor.cgqmc')"
            >
              <el-select
                v-model="queryForm.sensorName"
                filterable
                :placeholder="$t('defalult.qxz')"
              >
                <el-option
                  v-for="item in sensorData"
                  :key="item.name"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">
          {{ $t('defalult.chongz') }}
        </el-button>
        <el-button type="primary" size="small" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
        <el-button type="primary" size="small" @click="handleAdd()">
          {{ $t('sensor.tjcgq') }}
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="sensorData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        :label="$t('sensor.cgqmc')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="transferId"
        :label="$t('sensor.csid')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="addressId"
        :label="$t('sensor.dz')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="offset"
        :label="$t('sensor.jcqh')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit.dataValue"
        :label="$t('sensor.dw')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="readWrite.dataValue"
        :label="$t('sensor.dxqx')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dataFormat.dataValue"
        :label="$t('sensor.sjgs')"
        width="200"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="status"
        :label="$t('sensor.sfdtxs')"
        width="120"
        align="center"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.homeShowFlag"
            active-value="1"
            inactive-value="0"
            @click="soreStatus(scope.row.homeShowFlag, scope.row.id)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="formulaUp"
        :label="$t('sensor.sxgs')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="formulaDown"
        :label="$t('sensor.xsgs')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <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>
    <vab-query-form>
      <vab-query-form-right-panel>
        <el-pagination
          :background="background"
          :current-page="queryForm.page"
          :layout="layout"
          :page-size="queryForm.size"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        ></el-pagination>
      </vab-query-form-right-panel>
    </vab-query-form>
    <sensor-add ref="edit"></sensor-add>
  </div>
</template>
<script>
import { doDelete, getList, doEdit } from '@/api/sensor';
import sensorAdd from './add';
export default {
  name: 'Index',
  components: {
    sensorAdd,
  },
  data() {
    return {
      sensorData: [],
      deviceId: '',
      projectId: '',
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        deviceId: '',
        sensorName: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.deviceId = this.$route.query.deviceId;
    this.projectId = this.$route.query.projectId;
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.sensorData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    search() {
      this.fetchData();
    },
    async soreStatus(status, id) {
      const { code, msg } = await doEdit({ homeShowFlag: status, id: id });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.queryForm.deviceId, 0);
    },
    handleEdit(row) {
      row.deviceId = this.queryForm.deviceId;
      this.$refs['edit'].showEdit(row, 1);
    },
    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([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>
.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: 30px;
  }
}
</style>
