<template>
  <ele-modal
    :title="title"
    :visible.sync="visible"
    :close-on-click-modal="false"
    @close="handleClose"
    resizable
    maxable
    append-to-body
    width="90%"
  >
    <div>
      <seek-page
        :seekList="seekList"
        :maxLength="4"
        @search="search"
      ></seek-page>
      <ele-pro-table
        ref="table"
        key="id"
        :columns="basicColumns"
        :datasource="datasource"
        row-key="code"
        :selection.sync="selection"
        autoAmendPage
        :cache-key="cacheKeyUrl"
      >
        <template v-slot:radio="{ row }">
          <el-radio
            class="radio"
            :label="row.id"
            v-model="currentId"
            @change="
              () => {
                currentObj = row;
              }
            "
          >
            <i></i
          ></el-radio>
        </template>

        <!-- <template v-slot:code="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            {{ row.code }}
          </el-link>
        </template> -->

        <template v-slot:QRcode="{ row }">
          <el-link type="primary" :underline="false" @click="handleQRcode(row)">
            生成二维码
          </el-link>
        </template>

        <template v-slot:apsWorkOrderCode="{ row }">
          <label>{{ row.apsWorkOrderCode || '' }}</label>
        </template>

        <template v-slot:formingNum="{ row }">
          <span> {{ row.formingNum }} </span>
        </template>

        <template v-slot:formingWeight="{ row }">
          <span> {{ row.formingWeight }} {{ row.weightUnit }} </span>
        </template>

        <template v-slot:singleReport="{ row }">
          <span v-if="row.singleReport == 0">批量报工</span>
          <span v-if="row.singleReport == 1">单个报工</span>
        </template>

        <template v-slot:outsourceStatus="{ row }">
          <div v-if="row.outsourceStatus">
            <span v-if="row.outsourceStatus == 1">未委外</span>
            <span v-if="row.outsourceStatus == 2">委外中</span>
            <span v-if="row.outsourceStatus == 3">完成委外</span>
          </div>
        </template>

        <template v-slot:status="{ row }">
          <span :class="{ 'ele-text-danger': row.status == 3 }">
            {{ statusFormatter(row.status) }}
          </span>
        </template>
      </ele-pro-table>
    </div>

    <template v-slot:footer>
      <el-button type="primary" @click="submit">选 择</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </template>
  </ele-modal>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import tableColumnsMixin from '@/mixins/tableColumnsMixin';
  import { pageOrderByProduct } from '@/api/produce/workOrder.js';

  export default {
    mixins: [dictMixins, tableColumnsMixin],
    props: {
      // 额外的过滤条件
      where: {
        type: Object,
        default: () => ({})
      },
      // 选择后是否自动关闭弹窗
      autoClose: {
        type: Boolean,
        default: true
      },
      // 是否多选
      multiple: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        visible: false,
        title: '选择生产工单',
        statusOpt: {
          first: [
            { label: '所有状态', value: '5,4' },
            { label: '待生产', value: '4' },
            { label: '生产中', value: '5' }
            // { label: '已延期', value: '7' }
          ],
          second: [{ label: '已完成', value: '6' }]
        },
        planType: [
          { label: '所有计划类型', value: null },
          { label: '内销计划', value: '1' },
          { label: '外销计划', value: '2' },
          { label: '预制计划', value: '3' }
        ],
        cacheKeyUrl: 'public-2025926-select-work-order',
        selection: [],
        currentId: '',
        currentObj: null
      };
    },
    computed: {
      // 表格列配置
      seekList() {
        return [
          {
            label: '关键字：',
            value: 'keyWord',
            type: 'input',
            placeholder: ''
          },
          {
            label: '产品编码',
            value: 'productCode',
            type: 'input',

            placeholder: '',
            width: 240
          },

          {
            label: '产品名称：',
            value: 'productName',
            type: 'input',

            placeholder: '',
            width: 240
          },
          // {
          //   label: '批次号：',
          //   value: 'batchNo',
          //   type: 'input',
          //   placeholder: '',
          //   width: 240
          // },
          {
            label: '型号',
            value: 'model',
            type: 'input',

            placeholder: '',
            width: 240
          },
          {
            label: '生产工单号：',
            value: 'code',
            type: 'input',
            placeholder: '',
            labelWidth: '100'
          },
          {
            label: '生产订单号：',
            value: 'apsWorkOrderCode',
            type: 'input',
            labelWidth: '100'
          },
          {
            label: '计划编号：',
            value: 'productionPlanCode',
            type: 'input',
            width: 240
          },
          {
            label: '计划类型：',
            value: 'planType',
            type: 'select',

            placeholder: '',
            width: 240,
            // 加载状态
            planList: this.planType
          },
          {
            label: '工艺路线',
            value: 'produceRoutingName',
            type: 'input',

            placeholder: '',
            width: 240
          },
          {
            label: '牌号',
            value: 'brandNo',
            type: 'input',

            placeholder: '',
            width: 240
          },
          // {
          //   label: '班组：',
          //   value: 'teamId',
          //   type: 'select',
          //   multiple: false, // 是否多选
          //   filterable: true, // 是否可搜索
          //   placeholder: '',
          //   width: 240,
          //   // 加载状态
          //   planList: this.statusOpt.first
          // },
          {
            label: '创建时间：',
            value: 'createTime',
            type: 'date',
            dateType: 'daterange',
            placeholder: '',
            width: 240,
            // 加载状态
            planList: this.statusOpt.first
          }
        ];
      },
      basicColumns() {
        const list = [
          {
            slot: 'code',
            prop: 'code',
            label: '生产工单号',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          // {
          //   prop: 'originalCode',
          //   label: '原始工单号',
          //   align: 'center',
          //   minWidth: 110
          // },

          {
            prop: 'productCode',
            label: '产品编码',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'productName',
            label: '产品名称',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productionCodes',
            label: '生产编号',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            label: '生产订单号',
            slot: 'apsWorkOrderCode',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true
          },
          {
            prop: 'productionPlanCode',
            label: '计划编号',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true
          },
          {
            prop: 'planType',
            label: '计划类型',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row) => {
              const obj = this.planType.find((i) => i.value == row.planType);
              return obj && obj.label;
            }
          },
          {
            prop: 'bomType',
            label: '生产类型',
            align: 'center',
            width: 120,
            formatter: (row) => {
              if (row.bomType == 1) {
                return '产品(PBOM)';
              }
              if (row.bomType == 2) {
                return '加工(MBOM)';
              }
              if (row.bomType == 3) {
                return '装配(ABOM)';
              }
              // if (row.bomType == 4) {
              //   return '装配(EBOM)';
              // }
              return '';
            }
          },
          {
            prop: 'bomCategoryName',
            label: 'BOM版本',
            align: 'center',
            width: 130,
            showOverflowTooltip: true,
            formatter: (row) => {
              if (row.bomCategoryName) {
                return `${row.bomCategoryName} (V${row.bomCategoryVersions}.0)`;
              }
              return '';
            }
          },
          {
            prop: 'produceRoutingName',
            label: '工艺路线',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'lineNumber',
            label: '行号',
            align: 'center',
            minWidth: 130,
            showOverflowTooltip: true
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center'
          },
          {
            prop: 'taskName',
            label: '工序进度',
            align: 'center'
          },
          {
            prop: 'singleReport',
            slot: 'singleReport',
            label: '报工类型',
            align: 'center'
          },
          {
            prop: 'outsourceStatus',
            label: '委外状态',
            align: 'center',
            slot: 'outsourceStatus',
            showOverflowTooltip: true
          },
          {
            prop: 'formingNum',
            label: '要求生产数量',
            align: 'center',
            slot: 'formingNum',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formingWeight',
            label: '要求生产重量',
            slot: 'formingWeight',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formedNum',
            label: '已生产数量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formedWeight',
            label: '已生产重量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planStartTime',
            label: '计划开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'planCompleteTime',
            label: '计划结束时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'startTime',
            label: '实际开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'completeTime',
            label: '实际完成时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          // {
          //   prop: 'status',
          //   slot: 'status',
          //   label: '状态',
          //   align: 'center',
          //   formatter: (row) => {
          //     const obj = this.statusOpt[this.activeName].find(
          //       (i) => i.value == row.status
          //     );
          //     return obj && obj.label;
          //   }
          // },
          {
            prop: 'deviceName',
            slot: 'deviceName',
            label: '设备名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'crewNames',
            slot: 'crewNames',
            label: '报工人员',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'teamName',
            label: '班组',
            align: 'center',
            showOverflowTooltip: true
          }
        ];

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

        return list;
      }
    },
    methods: {
      // 外部调用，打开弹窗
      open(data) {
        this.visible = true;
      },
      // 关闭时清理表单
      handleClose() {
        this.currentId = '';
        this.currentObj = null;
        this.visible = false;
      },
      // 提交 选择
      submit() {
        console.log('this.selection', this.selection);

        if (this.multiple) {
          if (this.selection.length == 0) {
            this.$message.warning('请至少选择一条工单');
            return;
          }

          this.$emit('confirm', this.selection);
        } else {
          if (!this.currentObj) {
            this.$message.warning('请选择工单');
            return;
          }
          this.$emit('confirm', this.currentObj);
        }

        if (this.autoClose) {
          this.handleClose();
        }
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return pageOrderByProduct({
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          ...this.where
        });
      },
      /* 刷新表格 */
      reload(where = {}) {
        this.$refs.table.reload({ page: 1, where });
      },
      search(e) {
        if (Array.isArray(e.createTime) && e.createTime.length) {
          e.createTimeStart = e.createTime[0];
          e.createTimeEnd = e.createTime[1];
        }
        this.reload(e);
      }
    }
  };
</script>

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