<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <vab-query-form>
          <el-form ref="queryForm" :inline="true" :model="queryForm">
            <el-form-item :label="$t('maintenance.sb')" prop="deviceId">
              <el-select
                v-model="queryForm.deviceId"
                size="small"
                :placeholder="$t('trigger.qxzsb')"
              >
                <el-option
                  v-for="item in deviceData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </vab-query-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('trigger.tjcfq') }}
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="triggerData"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        :label="$t('trigger.xh')"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="device.name"
        :label="$t('dataEnquiry.zdmc')"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        :label="$t('trigger.mc')"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerCondition.dataValue"
        :label="$t('trigger.cftjlx')"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerConditionParams"
        :label="$t('trigger.cs')"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerCycle"
        :label="$t('trigger.cfzq')"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmFlag"
        :label="$t('trigger.sffsbj')"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.alarmFlag == 1"
            key="是"
            effect="dark"
            type="success"
            size="mini"
          >
            {{ $t('trigger.s') }}
          </el-tag>
          <el-tag
            v-if="row.alarmFlag == 0"
            key="否"
            effect="dark"
            type="danger"
            size="mini"
          >
            {{ $t('trigger.f') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="alarmLevel.dataValue"
        :label="$t('alarm.bjjb')"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        width="160"
        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>
    <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>
    <trigger-edit ref="edit"></trigger-edit>
  </div>
</template>

<script>
import {
  doDelete,
  getList,
  doEdit,
  selectDeviceByProjectId,
} from '@/api/trigger';
import triggerEdit from './triggerEdit';
export default {
  name: 'TriggerTable',
  components: {
    triggerEdit,
  },
  props: {
    projectId: {
      type: String,
      default: 'projectId',
    },
  },
  data() {
    return {
      groupData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      triggerData: [],
      deviceData: [],
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        projectId: '',
        deviceId: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.fetchData();
    this.getDeviceData();
  },
  methods: {
    showData() {
      this.fetchData();
      this.getDeviceData();
    },
    async soreStatus(status, id) {
      const { code, msg } = await doEdit({ status: status, id: id });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      this.triggerData = [];
      const { data } = await getList(this.queryForm);
      this.triggerData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async getDeviceData() {
      const { data } = await selectDeviceByProjectId({
        projectId: this.queryForm.projectId,
      });
      this.deviceData = data;
    },
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.queryForm.projectId, 1);
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row, 2);
    },
    details(row) {
      this.$router.push({
        path: '/triggerdetail',
        name: '触发器详情',
        query: { triggerId: row.id },
      });
    },
    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) {
      var 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>
