<template>
  <!-- 工单列表 -->
  <el-dialog
    :visible.sync="equipmentdialog"
    :before-close="handleClose"
    :close-on-click-modal="true"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
    title="生产工单"
  >
    <div>
      <seek-page :seekList="seekList" @search="search"></seek-page>
      <el-row>
        <el-col :span="24" class="table_col" v-if="equipmentdialog">
          <ele-pro-table
            ref="equiTable"
            :columns="columns"
            :datasource="datasource"
            :selection.sync="selection"
            :current.sync="current"
            highlight-current-row
            row-key="id"
            height="50vh"
            :pageSize="20"
            @update:selection="handleSelectionChange"
          >
          </ele-pro-table>
        </el-col>
      </el-row>
    </div>
    <div class="btns">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { produceOrder } from '@/api/aps/index.js';
  import produceOrderSearch from './produceOrderSearch.vue';

  export default {
    components: { produceOrderSearch },
    props: {
      selectList: Array,
      type: {
        default: 2 //1多选 2单选
      }
    },
    data() {
      return {
        planType: [
          { label: '所有计划类型', value: null },
          { label: '内销计划', value: '1' },
          { label: '外销计划', value: '2' },
          { label: '预制计划', value: '3' }
        ],
        statusOpt: {
          first: [
            { label: '所有状态', value: '5,4' },
            { label: '待生产', value: '4' },
            { label: '生产中', value: '5' }
            // { label: '已延期', value: '7' }
          ],
          second: [{ label: '已完成', value: '6' }]
        },
        equipmentdialog: false,
        current: null,
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            reserveSelection: true
          },
          {
            label: '生产工单号',
            align: 'center',
            prop: 'code',
            minWidth: 110,
            showOverflowTooltip: true
          },

          {
            prop: 'productionPlanCode',
            label: '计划编号',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true
          },
          {
            prop: 'planType',
            label: '计划类型',
            align: 'center',
            formatter: (row) => {
              const obj = this.planType.find((i) => i.value == row.planType);
              return obj && obj.label;
            },
            showOverflowTooltip: true
          },
          {
            prop: 'produceRoutingName',
            label: '工艺路线',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productCode',
            label: '产品编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productName',
            label: '产品名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            minWidth: 100,
            showOverflowTooltip: true
          },

          {
            prop: 'priority',
            label: '优先级',
            align: 'center',
            minWidth: 120,
            sortable: 'custom',
            showOverflowTooltip: true
          },

          {
            prop: 'formingNum',
            label: '要求生产数量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formingWeight',
            label: '要求生产重量',
            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: 110
          },
          {
            prop: 'planCompleteTime',
            label: '计划结束时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'startTime',
            label: '实际开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'teamName',
            label: '班组',
            align: 'center',
            showOverflowTooltip: true
          }
        ],
        categoryLevelId: null,
        code: null,
        selection: [],
        ids: [],
        allSelection: [],
        activeName: 'first'
      };
    },

    computed: {
      // 表格列配置
      seekList() {
        return [
          {
            label: '关键字：',
            value: 'keyWord',
            type: 'input',
            placeholder: ''
          },
          {
            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: 'productCode',
            type: 'input',

            placeholder: '',
            width: 240
          },

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

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

            placeholder: '',
            width: 240
          },
          {
            label: '型号',
            value: 'model',
            type: 'input',

            placeholder: '',
            width: 240
          },

          {
            label: '创建时间：',
            value: 'createTime',
            type: 'date',
            dateType: 'daterange',
            placeholder: '',
            width: 240
          }
        ];
      },
      basicColumns() {
        const num = this.columnsVersion;
        const opt = {
          first: [
            // {
            //   prop: 'deliveryTime',
            //   label: '预测交货日期',
            //   align: 'center',
            //   showOverflowTooltip: true,
            //   minWidth: 110
            // }
          ],
          second: [
            {
              prop: 'completeTime',
              label: '完成时间',
              align: 'center'
            },
            {
              prop: 'cycle',
              label: '生产周期',
              align: 'center'
            }
          ]
        };

        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: 'left'
          },

          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            minWidth: 130,
            showOverflowTooltip: true
          },

          {
            prop: 'lineNumber',
            label: '行号',
            align: 'center',
            minWidth: 130,
            showOverflowTooltip: true
          },

          {
            prop: 'code',
            slot: 'code',
            label: '生产工单号',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true
          },
          // {
          //   prop: 'originalCode',
          //   label: '原始工单号',
          //   align: 'center',
          //   minWidth: 110
          // },

          {
            prop: 'QRcode',
            slot: 'QRcode',
            label: '二维码',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true
          },
          {
            prop: 'apsWorkOrderCode',
            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',
            showOverflowTooltip: true,
            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: 'productCode',
            label: '编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'colorKey',
            label: '颜色',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'modelKey',
            label: '机型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'productName',
            label: '名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productionCodes',
            label: '生产编号',
            align: 'center',
            minWidth: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'taskName',
            label: '工序进度',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'singleReport',
            slot: 'singleReport',
            label: '报工类型',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'outsourceStatus',
            label: '委外状态',
            align: 'center',
            slot: 'outsourceStatus',
            showOverflowTooltip: true
          },
          {
            prop: 'entrustStatus',
            label: '请托状态',
            align: 'center',
            slot: 'entrustStatus',
            showOverflowTooltip: true
          },
          {
            prop: 'priority',
            label: '优先级',
            align: 'center',
            minWidth: 120,
            slot: 'priority',
            sortable: 'custom'
          },

          {
            prop: 'formingNum',
            label: '要求生产数量',
            align: 'center',
            slot: 'formingNum',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'unit',
            label: '单位',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'factoriesName',
            label: '所属工厂',
            align: 'center',
            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,
            sortable: 'custom'
          },
          {
            prop: 'planCompleteTime',
            label: '计划结束时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            sortable: 'custom'
          },
          {
            prop: 'startTime',
            label: '实际开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            sortable: 'custom'
          },
          {
            prop: 'completeTime',
            label: '实际完成时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            sortable: 'custom'
          },
          ...opt[this.activeName],
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            sortable: 'custom'
          },
          // {
          //   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
          }
        ];
      },

      clientEnvironmentId() {
        return this.$store.state.user.info.clientEnvironmentId;
      }
    },
    methods: {
      datasource({ page, where, limit }) {
        return produceOrder({
          ...where,
          pageNum: page,
          size: limit
        });
      },
      open(list) {
        this.equipmentdialog = true;
        this.allSelection = list;
        this.$nextTick(() => {
          this.allSelection.forEach((item) => {
            this.$refs.equiTable.toggleRowSelection(item, true);
          });
        });
      },

      search(e) {
        if (Array.isArray(e.createTime) && e.createTime.length) {
          e.createTimeStart = e.createTime[0];
          e.createTimeEnd = e.createTime[1];
        }
        this.reload(e);
      },

      /* 刷新表格 */
      reload(where = {}) {
        this.$nextTick(() => {
          where.statusList = (
            where.status || this.statusOpt[this.activeName][0].value
          ).split(',');
          this.$refs.equiTable.reload({ page: 1, where });
        });
      },

      handleClose() {
        this.equipmentdialog = false;
        this.$refs.equiTable.clearSelection();
      },

      handleSelectionChange(data) {
        this.allSelection = data;
      },

      // 选择
      selected() {
        this.$emit('choose', this.selection);
        this.handleClose();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tree_col {
    border: 1px solid #eee;
    padding: 10px 0;
    box-sizing: border-box;
    max-height: 530px;
    overflow: auto;
  }
  .table_col {
    padding-left: 10px;
    ::v-deep .el-table th.el-table__cell {
      background: #f2f2f2;
    }
  }
  .pagination {
    text-align: right;
    padding: 10px 0;
  }
  .btns {
    text-align: center;
    padding: 10px 0;
  }
  .topsearch {
    margin-bottom: 15px;
  }
</style>
