<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%"
    :before-close="handleClose"
  >
    <el-card shadow="never" v-loading="loading">
      <seekPage :seekList="seekList" @search="reload" :maxLength="4"></seekPage>
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="select-plan-rules-table-2511141448"
      >
        <!-- 单选列 -->
        <template v-slot:radio="{ row }">
          <el-radio
            class="radio"
            v-model="currentRowId"
            :label="row.id"
            @change="radioChange($event, row)"
            ><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 tabMixins from '@/mixins/tableColumnsMixin';
  import dictMixins from '@/mixins/dictMixins';
  import { planConfigPage } from '@/api/ruleManagement/plan.js';

  export default {
    components: {},
    mixins: [tabMixins, dictMixins],
    props: {
      // 是否多选
      multiple: {
        type: Boolean,
        default: false
      },
      // 默认 筛选条件
      where: {
        type: Object,
        default: () => ({
          // 生效
          status: 1
        })
      },
      // 1巡点检 2保养
      ruleTypes: {
        type: Array,
        default: () => {
          return [1, 2];
        }
      }
    },
    computed: {
      columns() {
        const list = [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '计划配置单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'code'
          },
          {
            prop: 'name',
            label: '计划配置名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          ...[
            this.ruleType == 1
              ? {
                  prop: 'groupName',
                  label: '巡点检部门',
                  align: 'center',
                  showOverflowTooltip: true,
                  minWidth: 110,
                  slot: 'groupName'
                }
              : {
                  prop: 'groupName',
                  label: '部门',
                  align: 'center',
                  showOverflowTooltip: true,
                  minWidth: 110,
                  slot: 'groupName'
                }
          ],
          {
            prop: 'categoryName',
            label: '设备分类',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'autoOrder',
            label: '自动派单',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) => {
              let autoOrder =
                _row.autoOrder == 1 ? '是' : _row.autoOrder == 0 ? '否' : '-';
              return autoOrder;
            }
          },
          {
            //修改此prop名称时，请同步修改columnKey属性和下方selectType方法
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) => {
              return this.getDictValue('规则状态', _row.status);
            },
            filters: [
              { value: 1, text: '生效' },
              { value: 0, text: '失效' }
            ],
            filterMultiple: false,
            columnKey: 'status'
          },
          {
            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);
            }
          }
        ];

        if (this.multiple) {
          list.unshift({
            type: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
          });
        } else {
          list.unshift({
            prop: 'radio',
            width: 50,
            align: 'center',
            fixed: 'left',
            slot: 'radio'
          });
        }

        return list;
      },
      seekList() {
        return [
          {
            label: '规则名称:',
            value: 'name',
            type: 'input',
            placeholder: '规则名称'
          },
          {
            label: '规则编码:',
            value: 'code',
            type: 'input',
            placeholder: '规则编码'
          }
        ];
      }
    },
    data() {
      return {
        loading: false,
        visible: false,
        selection: [],
        produceTaskId: '',
        reportWorkType: '', // 事项报工类型 产前、过程、产后
        currentRowId: null,
        currentRow: null
      };
    },
    created() {
      this.requestDict('事项类型');
      this.requestDict('规则周期');
    },
    methods: {
      open() {
        this.visible = true;
        this.reload(); // 刷新表格
      },
      updateVisible(val) {
        this.visible = val;
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return planConfigPage({
          pageNum: page,
          size: limit,
          ...where,
          ...this.where,
          ruleTypes: this.ruleTypes
        });
      },
      /* 刷新表格 */
      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.handleClose();
      },
      radioChange(_, row) {
        this.currentRow = row;
      },
      handleClose() {
        this.selection = [];
        this.currentRowId = null;
        this.currentRow = null;
        this.visible = false;
      }
    }
  };
</script>
