<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"
      >
        <!-- 单选列 -->
        <template v-slot:radio="{ row }">
          <el-radio
            class="radio"
            v-model="currentRowId"
            :label="row.id"
            @change="radioChange($event, row)"
            :disabled="activeIds.includes(row.id)"
            ><i></i
          ></el-radio>
        </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 search from './search.vue';
  import {
    recordrulesPage,
    recordrulesNotProduceTaskConfigRecordRulesPage
  } from '@/api/recordRules/index';
  import tabMixins from '@/mixins/tableColumnsMixin';
  import dictMixins from '@/mixins/dictMixins';

  export default {
    components: { search },
    mixins: [tabMixins, dictMixins],
    props: {
      // NotProduceTaskConfig bool 是否排除已配置的记录规则 过程控制传true
      notProduceTaskConfig: {
        type: Boolean,
        default: false
      },
      // 是否多选
      multiple: {
        type: Boolean,
        default: false
      }
    },
    computed: {
      columns() {
        const list = [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'code',
            label: '记录规则编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'name',
            label: '记录规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'industryType',
            label: '行业类别',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return this.getDictValue('行业类别', row.industryType);
            }
          },
          {
            prop: 'businessType',
            label: '业务分类',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return this.getDictValue('业务类型', row.businessType);
            }
          },
          {
            prop: 'reportWorkType',
            label: '模块划分',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return this.getDictValue('记录规则报工类型', row.reportWorkType);
            }
          },
          {
            prop: 'classify',
            label: '应用场景',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return this.getDictValue('记录规则类型', row.classify);
            }
          },
          {
            prop: 'recordTemplateStyle',
            label: '模板样式',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return this.getDictValue(
                '记录表模板样式',
                row.recordTemplateStyle
              );
            }
          },
          {
            prop: 'executeMethodName',
            label: '执行方式',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'version',
            label: '版本号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              return `${row.versionSymbol}${row.bigVersion}${row.versionMark}${row.smallVersion}`;
            }
          },
          {
            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 '';
              }
            }
          }
        ];

        if (this.multiple) {
          list.unshift({
            type: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left',
            selectable: (row) => {
              return !this.activeIds.includes(row.id);
            }
          });
        } else {
          list.unshift({
            prop: 'radio',
            width: 50,
            align: 'center',
            fixed: 'left',
            slot: 'radio'
          });
        }

        return list;
      }
    },
    data() {
      return {
        loading: false,
        visible: false,
        selection: [],
        produceTaskId: '',
        reportWorkType: '', // 记录规则报工类型 产前、过程、产后
        currentRowId: null,
        currentRow: null,
        activeIds: []
      };
    },
    created() {
      this.requestDict('记录规则类型');
      this.requestDict('规则周期');
    },
    methods: {
      open(reportWorkType = '', produceTaskId = '', activeIds = []) {
        console.log('reportWorkType', reportWorkType);
        console.log('produceTaskId', produceTaskId);

        this.visible = true;
        this.selection = [];
        this.currentRowId = null;
        this.currentRow = null;
        this.reportWorkType = reportWorkType || '';
        this.produceTaskId = produceTaskId || '';
        this.activeIds = activeIds;
        this.reload(); // 刷新表格
      },
      updateVisible(val) {
        this.visible = val;
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        const API = this.notProduceTaskConfig
          ? recordrulesNotProduceTaskConfigRecordRulesPage
          : recordrulesPage;
        return API({
          pageNum: page,
          size: limit,
          // 已发布
          publishStatus: 1,
          // 启用
          enable: 1,
          ...where,
          reportWorkType: this.reportWorkType,
          produceTaskId: this.produceTaskId
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.tableRef?.reload({
          page: 1, // 已发布
          where
        });
      },
      confirmSelection() {
        if (this.multiple && this.selection.length === 0) {
          this.$message.warning('请先选择记录规则');
          return;
        }

        if (!this.multiple && !this.currentRow) {
          this.$message.warning('请先选择记录规则');
          return;
        }

        this.$emit(
          'chooseRules',
          this.multiple ? this.selection : this.currentRow
        );
        this.visible = false;
      },
      radioChange(_, row) {
        this.currentRow = row;
      }
    }
  };
</script>
