<template>
  <el-dialog
    title="工单列表"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <produceOrderSearch @search="reload"></produceOrderSearch>
    <ele-pro-table
      ref="table"
      height="55vh"
      :columns="columns"
      width="30%"
      :initLoad="false"
      :datasource="datasource"
      :selection.sync="selection"
      cache-key="produceOrderZ"
      highlight-current-row
    >
      <template v-slot:code="{ row }">
        <el-link type="primary" :underline="false">{{ row.code }}</el-link>
      </template>

      <template v-slot:singleReport="{ row }">
        <el-tag> {{ row.singleReport == 1 ? '单个报工' : '批量报工' }}</el-tag>
      </template>

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

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

    <template slot="footer">
      <el-button size="mini" @click="handleClose">取 消</el-button>
      <el-button size="mini" type="primary" @click="handleSelect()"
        >确 定</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
  import { pickTaskReportPage } from '@/api/produce/workOrder.js';

  import produceOrderSearch from './produceOrder-search.vue';

  export default {
    components: { produceOrderSearch },

    data() {
      return {
        loading: false,
        selection: [],

        visible: true,
        taskId: null
      };
    },

    watch: {},

    computed: {
      columns() {
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: true
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center'
          },

          {
            prop: 'code',
            slot: 'code',
            label: '生产工单号',
            align: 'center',
            minWidth: '110'
          },
          {
            prop: '',
            label: '计划编号',
            align: 'center'
          },
          {
            prop: '',
            label: '计划类型',
            align: 'center'
          },

          {
            prop: 'productCode',
            label: '产品编码',
            align: 'center'
          },
          {
            prop: 'productName',
            label: '产品名称',
            align: 'center'
          },

          {
            prop: 'singleReport',
            slot: 'singleReport',
            label: '报工类型',
            align: 'center',
            width: 100
          },

          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center'
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center'
          },

          {
            prop: 'priority',
            label: '优先级',
            align: 'center',
            minWidth: 120
          },

          {
            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: 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
          }
        ];
      }
    },

    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return pickTaskReportPage({
          pageNum: page,
          size: limit,

          ...where
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.taskId = where.taskId || '';
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      },
      handleSelect() {
        let ids = [];
        ids = this.selection.map((item) => {
          return item.id;
        });
        if (!this.taskId) {
          return this.$message.warning('请选择工序');
        }

        this.$emit('workSelect', ids, this.taskId);
      },

      handleClose() {
        this.$emit('close');
      }
    }
  };
</script>
