<template>
  <ele-modal
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    title="选择事项规则"
    @update:visible="updateVisible"
    :maxable="true"
    width="90%"
  >
    <el-card shadow="never" v-loading="loading">
      <matter-search @search="reload" :filterType="filterType"> </matter-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :page-size="pageSize"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
        :selection.sync="selection"
      >
        <!-- 编码列 -->
        <template v-slot:code="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            {{ row.code }}
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <template v-slot:footer>
      <el-button type="primary" @click="confirmSelection"> 选择 </el-button>

      <el-button @click="visible = false">关闭</el-button>
    </template>
  </ele-modal>
</template>

<script>
  import tabMixins from '@/mixins/tableColumnsMixin';
  import MatterSearch from './matter-search.vue';
  import { getList, removeRule } from '@/api/ruleManagement/matter';
  import dictMixins from '@/mixins/dictMixins';

  export default {
    mixins: [dictMixins, tabMixins],
    emits: ['chooseRules'],
    props: {
      filterType: {
        type: Array,
        default: () => {
          return [];
        }
      },
      // 是否多选
      multiple: {
        type: Boolean,
        default: false
      }
    },
    components: {
      MatterSearch
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            prop: 'code',
            label: '规则编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'code'
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) => {
              return this.getDictValue('规则状态', _row.status);
            }
          },
          {
            prop: 'ruleType',
            label: '规则类型',
            align: 'center',
            showOverflowTooltip: true,
            slot: 'enable',
            minWidth: 110,
            formatter: (_row) => {
              return this.getDictValue('规则类型', _row.ruleType);
            }
          },
          {
            prop: 'name',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'cycle',
            label: '周期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) => {
              return this.getDictValue('规则周期', _row.cycleType);
            }
          },
          {
            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);
            }
          }
        ],
        // 加载状态
        loading: false,
        pageType: 'add',
        dialogTitle: '',
        pageSize: this.$store.state.tablePageSize,
        cacheKeyUrl: '0c40331c-rulesManagement-matterRules',
        visible: false,
        selection: [],
        produceTaskOptions: []
      };
    },
    computed: {},
    mounted() {
      this.requestDict('规则周期');
      this.requestDict('规则类型');
      this.requestDict('规则状态');
    },
    methods: {
      open(selection = []) {
        this.visible = true;
        this.selection = selection;
        this.reload();
      },
      updateVisible(val) {
        this.visible = val;
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return getList({
          pageNum: page,
          size: limit,
          ...where,
          types: this.filterType
        });
      },
      /* 刷新表格 */
      reload(where = {}) {
        this.$refs.table?.reload({ page: 1, where, types: this.filterType });
      },

      remove(row) {
        removeRule([row.id]).then(() => {
          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/matterRules/details',
          query: { id }
        });
      },
      confirmSelection() {
        if (this.selection.length == 0) {
          this.$message.warning('请选择事项规则!');
          return;
        }
        if (this.multiple) {
          this.$emit('chooseRules', this.selection);
        } else {
          if (this.selection.length > 1) {
            this.$message.warning('只能选择一条事项规则');
            return;
          }
          this.$emit('chooseRules', this.selection[0]);
        }
        this.visible = false;
      }
    }
  };
</script>

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