<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('dataEnquiry.cgq')" prop="sensorName">
              <el-select
                v-model="queryForm.sensorName"
                size="small"
                :placeholder="$t('sensor.qxzqgq')"
              >
                <el-option
                  v-for="item in sensorData"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </vab-query-form>
      </div>
      <div class="manage-input">
        <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.tjcfqgz') }}
        </el-button>
        <el-button type="primary" size="small" @click="deletes">
          {{ $t('defalult.plsc') }}
        </el-button>
        <el-button type="primary" size="small" @click="triggerRuleUse">
          {{ $t('trigger.plyy') }}
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="ruleData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sensorName"
        :label="$t('sensor.cgqmc')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerName"
        :label="$t('trigger.cfqmc')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerCycle"
        :label="$t('trigger.cfzq')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerCondition.dataValue"
        :label="$t('trigger.cftj')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerConditionParams"
        :label="$t('trigger.cfcs')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmFlag"
        :label="$t('trigger.sffsbj')"
        width="120"
        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')"
        width="120"
        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>
    <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>
    <rule-edit ref="edit"></rule-edit>
  </div>
</template>
<script>
import {
  doDelete,
  getList,
  triggerRuleUse,
  getSensorsData,
} from '@/api/triggerRule';
import ruleEdit from './ruleEdit';
export default {
  name: 'Index',
  components: {
    ruleEdit,
  },
  data() {
    return {
      ruleData: [],
      deviceId: '',
      projectId: '',
      sensorData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      ids: [],
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        projectId: '',
        sensorName: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
  },
  methods: {
    showData() {
      this.getSensorData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.ruleData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    //获取传感器
    async getSensorData() {
      const { data } = await getSensorsData(this.queryForm);
      this.sensorData = data;
      this.fetchData();
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    async triggerRuleUse() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.$confirm('你确定应用触发器规则到整个项目吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = triggerRuleUse(ids);
          this.$message({
            type: 'success',
            message: msg == undefined ? '应用成功' : msg,
          });
          const that = this;
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消应用',
          });
        });
    },
    search() {
      this.fetchData();
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.queryForm.sensorName, 0);
    },
    handleEdit(row) {
      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();
    },
    deleteData(data) {
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(data);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          const that = this;
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      if (row.id) {
        this.deleteData([row.id]);
      }
    },
  },
};
</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>
