<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">
      <search @search="reload" />
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
      </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 search from './search.vue';
  import { recordrulesPage } from '@/api/recordrules/index';
  import tabMixins from '@/mixins/tableColumnsMixin';
  import dictMixins from '@/mixins/dictMixins';

  export default {
    components: { search },
    mixins: [tabMixins, dictMixins],
    computed: {
      columns() {
        return [
          {
            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
          },
          {
            prop: 'name',
            label: '记录规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'classify',
            label: '记录规则分类',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return this.getDictValue('记录规则类型', row.classify);
            }
          },
          {
            prop: 'version',
            label: '版本号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return row.version ? `V${row.version.toFixed(1)}` : '';
            }
          },
          {
            prop: 'fromName',
            label: '来源版本',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'startDate',
            label: '启用日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'stopDate',
            label: '停用日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: '',
            label: '周期',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row) => {
              return (
                row.frequencyValue +
                this.getDictValue('记录规则频率', row.frequencyUnit + '')
              );
            }
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'enable',
            label: '是否启用',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return row.enable ? '启用' : '停用';
            }
          },
          {
            prop: 'publishStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              switch (row.publishStatus) {
                case 0:
                  return '草稿';
                case 1:
                  return '已发布';
                case 2:
                  return '已撤销';
                default:
                  return '';
              }
            }
          }
        ];
      }
    },
    data() {
      return {
        loading: false,
        visible: false,
        selection: []
      };
    },
    created() {
      this.requestDict('记录规则类型');
      this.requestDict('记录规则频率');
    },
    methods: {
      open() {
        this.visible = true;
        this.selection = [];
        this.reload({}); // 刷新表格
      },
      updateVisible(val) {
        this.visible = val;
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return recordrulesPage({
          pageNum: page,
          size: limit,
          // 已发布
          publishStatus: 1,
          // 启用
          enable: 1,
          ...where
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.tableRef?.reload({
          page: 1, // 已发布
          publishStatus: 1,
          // 启用
          enable: 1,
          where
        });
      },
      confirmSelection() {
        this.$emit('chooseRules', this.selection);
        this.visible = false;
      }
    }
  };
</script>
