<!-- 返工返修 -->
<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <order-search @search="reload" ref="searchRef"> </order-search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
        cache-key="rework_list_data"
        :pageSize="20"
        :height="tableHeight"
        @fullscreen-change="fullscreenChange"
      >
        <!-- 操作列 -->
        <template v-slot:toolbar>
          <el-button
            type="primary"
            slot="reference"
            :disabled="selection.length == 0"
            @click="addProductPlan(1)"
            >转生产计划</el-button
          >
          <!-- <el-button
            type="primary"
            slot="reference"
            :disabled="selection.length == 0"
            @click="open(2)"
            >转生产订单</el-button
          >
          <el-button
            type="primary"
            slot="reference"
            :disabled="selection.length == 0"
            @click="open(3)"
            >转生产工单</el-button
          >

          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove"
          >
            <template v-slot:reference>
              <el-button :disabled="selection.length == 0" type="danger"
                >批量删除</el-button
              >
            </template>
          </el-popconfirm> -->
        </template>
        <template v-slot:action="{ row }">
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            v-if="row.status == 1"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
          <!-- <el-link
            slot="reference"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            v-if="row.status == 0"
            @click="disposeList(row)"
          >
            处置
          </el-link> -->
        </template>
      </ele-pro-table>
    </el-card>
    <SalesToProduction ref="salesToProductionRef" @success="reload" />

    <factory-add
      v-if="factoryVisable"
      :orderType="orderType"
      ref="factoryAddRef"
      :factoryVisable.sync="factoryVisable"
      :factoryData="selection"
    ></factory-add>
  </div>
</template>

<script>
  import OrderSearch from '../components/probationalGoods-search.vue';

  // import OrderSearch from '../components/rework-search.vue';
  import SalesToProduction from '../components/salesToProduction.vue';
  import dictMixins from '@/mixins/dictMixins';
  // import { reworkStatus } from '@/utils/util';

  import {
    getReworkList,
    deleteReworkList
  } from '@/api/unacceptedProduct/index';
  import FactoryAdd from '../components/factoryAdd.vue';
  export default {
    components: {
      OrderSearch,
      SalesToProduction,
      FactoryAdd
    },
    mixins: [dictMixins],
    data() {
      return {
        factoryVisable: false,
        loading: false,
        dialogVisible: false,
        current: null,
        selection: [],
        disposeType: '',
        releasParams: {
          teamId: '',
          id: ''
        },
        orderType: 1,
        tableHeight: 'calc(100vh - 340px)'
      };
    },
    computed: {
      // 表格列配置
      columns() {
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            reserveSelection: true,
            selectable: (row, index) => {
              return row.status == 0; //
            }
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '编码',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'sampleCode',
            label: '样品编码',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryCode',
            label: '物品编码',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryName',
            label: '物品名称',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'brandNum',
            label: '牌号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'modelType',
            label: '型号',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'measureQuantity',
            label: '计量数量',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'measureUnit',
            label: '计量单位',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },

          {
            prop: 'weight',
            label: '重量',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'weightUnit',
            label: '重量单位',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          // {
          //   prop: 'disposeTime',
          //   label: '处置时间',
          //   align: 'center',
          //   width: 180,
          //   showOverflowTooltip: true
          // },
          {
            prop: 'produceRoutingName',
            label: '工艺路线',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'produceTaskName',
            label: '工序',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'unqualifiedReason',
            label: '原因',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'type',
            label: '处理类型',
            align: 'center',
            width: 120,
            showOverflowTooltip: true,
            formatter: (row, column, cellValue) => {
              return cellValue == 1
                ? '转生产计划'
                : cellValue == 2
                ? '转生产订单'
                : cellValue == 3
                ? '转生产工单'
                : cellValue == 4
                ? '回流'
                : '';
            }
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row, column, cellValue) => {
              return cellValue == 0 ? '未处理' : cellValue == 1 ? '已处理' : '';
            }
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          }
          // {
          //   prop: 'reviewerName',
          //   label: '创建人',
          //   align: 'center'
          // },
          // {
          //   prop: 'depotName',
          //   label: '仓库名称',
          //   align: 'center'
          // },
          // {
          //   prop: 'status',
          //   label: '状态',
          //   align: 'center',
          //   formatter: (row, column, cellValue) => {
          //     return cellValue == 0 ? '未入库' : cellValue == 1 ? '已入库' : '';
          //   }
          // }

          // {
          //   columnKey: 'action',
          //   label: '操作',
          //   width: 150,
          //   align: 'center',
          //   resizable: false,
          //   fixed: 'right',
          //   slot: 'action',
          //   showOverflowTooltip: true
          // }
        ];
      }
    },
    created() {},
    filters: {},
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return getReworkList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      remove(row) {
        let ids = row ? [row.id] : this.selection.map((item) => item.id);
        deleteReworkList(ids).then((res) => {
          this.$message.success('删除' + res);
          this.reload();
        });
      },
      open(type) {
        let isCode = true,
          isBatchNo = true;
        this.selection.forEach((item) => {
          this.selection.forEach((item1) => {
            if (item.productCodeNew != item1.productCodeNew) {
              isCode = false;
            }
            if (item.batchNo != item1.batchNo) {
              isBatchNo = false;
            }
          });
        });
        if (!isCode) {
          this.$message.error('产品编码不一致！');
          return;
        }
        if (!isBatchNo) {
          this.$message.error('批次号不一致！');
          return;
        }
        this.$refs.salesToProductionRef.open(this.selection, type);
      },
      async disposeList(row) {
        if (row && row.id) {
          this.current = row;
          this.dialogVisible = true;
          return;
        }
        let ids = this.current
          ? [this.current.id]
          : this.selection.map((item) => item.id);
        await dispose({
          id: ids,
          disposeType: Number(this.disposeType)
        });
        this.dialogVisible = false;
        this.current = null;
        this.$message.success('处置成功！');
        this.getList();
      },
      fullscreenChange(fullscreen) {
        if (fullscreen) {
          this.tableHeight = 'calc(100vh - 120px)';
        } else {
          this.tableHeight = 'calc(100vh - 340px)';
        }
      },

      /* 刷新表格 */
      reload(where) {
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      },
      addProductPlan(type) {
        this.orderType = type;
        this.factoryVisable = true;
      }
    }
  };
</script>

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