<template>
  <div class="manage-container">
    <el-table
      v-loading="listLoading"
      :data="triggerData"
      border
      stripe
      style="width: 100%"
    >
      <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="name"
        :label="$t('trigger.cfqmc')"
        width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="device.name"
        :label="$t('maintenance.sbmc')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sensor.name"
        :label="$t('sensor.cgqmc')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerConditionParams"
        :label="$t('trigger.cftj')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="triggerCycle"
        :label="$t('trigger.cfzq')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmFlag"
        label="是否发送报警信息给用户"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.alarmFlag == 1"
            key="发送"
            effect="dark"
            type="success"
            size="mini"
          >
            发送
          </el-tag>
          <el-tag
            v-if="row.alarmFlag == 0"
            key="不发送"
            effect="dark"
            type="danger"
            size="mini"
          >
            不发送
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="alarmLevel.dataValue"
        :label="$t('alarm.bjjb')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="controlFlag"
        label="是否联控其他设备"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.controlFlag == 1"
            key="联控"
            effect="success"
            type="success"
            size="mini"
          >
            联控
          </el-tag>
          <el-tag
            v-if="row.controlFlag == 0"
            key="不发送"
            effect="dark"
            type="danger"
            size="mini"
          >
            未联控
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="controlDevice"
        label="联控设备"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="controlSensor"
        label="联控设备的传感器"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="controlData"
        label="给联控设备下发的数据"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmTemplate"
        label="报警信息模板"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        label="触发器状态"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.status == 1"
            key="禁用"
            effect="dark"
            type="danger"
            size="mini"
          >
            禁用
          </el-tag>
          <el-tag
            v-if="row.status == 0"
            key="正常"
            effect="dark"
            type="success"
            size="mini"
          >
            正常
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { selectTriggerById } from '@/api/trigger';
export default {
  components: {},
  data() {
    return {
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      triggerData: [],
      queryForm: {
        triggerId: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.triggerId = this.$route.query.triggerId;
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await selectTriggerById(this.queryForm);
      this.triggerData[0] = data;
      this.replaceData();
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    replaceData() {
      const dataValue = this.triggerData[0].triggerCondition.dataValue;
      const params = this.triggerData[0].triggerConditionParams.split(',');
      let newValue = '';
      if (dataValue.includes('{x}') && dataValue.includes('{y}')) {
        newValue = dataValue.replace('{x}', params[0]);
        newValue = newValue.replace('{y}', params[1]);
      } else if (dataValue.includes('{m}') && dataValue.includes('{x}')) {
        newValue = dataValue.replace('{m}', params[0]);
        newValue = newValue.replace('{x}', params[1]);
      } else if (dataValue.includes('{x}')) {
        newValue = dataValue.replace('{x}', params[0]);
      } else if (dataValue.includes('{y}')) {
        newValue = dataValue.replace('{y}', params[0]);
      } else if (dataValue.includes('{m}')) {
        newValue = dataValue.replace('{m}', params[0]);
      }
      this.triggerData[0].triggerConditionParams = newValue;
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-container {
  width: 100%;
  .vab-query-form {
    margin-bottom: 30px;
    .el-input {
      width: 180px;
      margin-right: 20px;
    }
  }
}
</style>
