<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <matter-search @search="reload"> </matter-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :page-size="pageSize"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
        @filter-change="selectType"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
            v-if="$hasPermission('main:warnrule:save')"
          >
            新建
          </el-button>
        </template>
        <!-- 编码列 -->
        <template v-slot:ruleCode="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            {{ row.ruleCode }}
          </el-link>
        </template>
        <template v-slot:alertCycleValue="{ row }">
          <span v-if="row.alertCycleValue">{{ row.alertCycleValue }}</span>
          <template v-if="row.alertCycleUnit">
            <span v-for="(value, key) in cycleUnitOpt">
              <span v-if="key == row.alertCycleUnit">{{ value }}</span>
            </span>
          </template>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-finished"
            @click="openEdit('clone', row)"
            v-if="$hasPermission('main:warnrule:update')"

          >
            克隆
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
            v-if="$hasPermission('main:warnrule:update')"

          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此角色吗？"
            @confirm="remove(row)"
            v-if="$hasPermission('main:warnrule:delete')"

          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 新建或编辑弹窗 -->
    <matter-add
      ref="addMatterRulesRef"
      :pageType="pageType"
      :dialogTitle="dialogTitle"
      @done="reload"
    />
  </div>
</template>

<script>
import tabMixins from '@/mixins/tableColumnsMixin';
import MatterSearch from './components/matter-search.vue';
import MatterAdd from './components/matter-add.vue';
import {
  getList,
  getDetail,
  removeRule,
  listAllDateUnit
} from '@/api/ruleManagement/earlyWarning';
import dictMixins from '@/mixins/dictMixins';
export default {
  mixins: [dictMixins, tabMixins],
  components: {
    MatterSearch,
    MatterAdd
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'ruleCode',
          label: '规则编码',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'ruleCode'
        },
        {
          prop: 'ruleName',
          label: '规则名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {//修改此prop名称时，请同步修改columnKey属性和下方selectType方法
          prop: 'enabled',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (_row, _column, cellValue) => {
            return this.getDictValue('规则状态', _row.enabled);
          },
          filters: [
              { value: 1, text: '生效' },
              { value: 0, text: '失效' },
            ],
          filterMultiple: false,
          columnKey: 'enabled'
        },
        {
          prop: 'alertCycleValue',
          label: '周期',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'alertCycleValue'
        },
        {
          prop: 'createUserName',
          label: '创建人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 230,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],
      // 加载状态
      loading: false,
      pageType: 'add',
      dialogTitle: '',

      cycleUnitOpt: {},
      pageSize: this.$store.state.tablePageSize,
      cacheKeyUrl: 'dfb89646-rulesManagement-earlyWarningRules'
    };
  },
  computed: {},
  created() {
    // this.requestDict('规则周期');
    this.requestDict('预警类型');
    this.requestDict('规则状态');
    this.getData();
  },
  methods: {
    selectType(value) {
      let where = {}
      if (value.enabled.length > 0) {
        where['enabled'] = value.enabled[0]
      }
      this.reload(where)
    },
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return getList({ pageNum: page, size: limit, ...where });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where });
    },
    async getData() {
      const res2 = await listAllDateUnit();
      this.cycleUnitOpt = res2;
    },

    remove(row) {
      removeRule([row.id]).then((res) => {
        this.$message.success('删除成功！');
        this.reload();
      });
    },

    openEdit(type, row) {
      this.pageType = type;
      this.dialogTitle =
        type == 'add' ? '新建规则' : type == 'edit' ? '编辑规则' : '克隆规则';
      this.$refs.addMatterRulesRef.openDialog(row, type);
    },

    goDetail({ id }) {
      this.$router.push({
        path: '/rulesManagement/earlyWarningRules/details',
        query: { id }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
