<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"
        :current.sync="current"
        highlight-current-row
        row-key="id"
      >
        <template v-slot:toolbar>
          <el-button type="primary" @click="salesToProductionOpen(1)"
            >转生产计划</el-button
          >
          <el-button type="primary" @click="salesToProductionOpen(2)"
            >转生产订单</el-button
          >
          <el-button type="primary" @click="salesToProductionOpen(3)"
            >转生产工单</el-button
          >
        </template>
        <template v-slot:type="{ row }">
          <span> {{ getDictValue('请托类型', row.type + '') }}</span>
        </template>

        <template v-slot:code="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="open('detail', row)"
          >
            {{ row.code }}
          </el-link>
        </template>
        <!-- 操作列 -->

        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            v-if="row.status == 0"
            @click="update(row.id)"
          >
            接收
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            v-if="row.status == 1"
            @click="openWarehouseEntry(row.id)"
          >
            申请入库
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <Create ref="create" @refresh="reload" />
    <SalesToProduction ref="salesToProductionRef" @success="reload" />
    <ele-modal :visible.sync="visible" width="20vw" append-to-body :maxable="true">
      <el-form label-width="100px">
        <el-form-item label="入库仓库:" prop="warehouseId">
          <el-select v-model="warehouseId">
            <el-option
              v-for="item in warehouseList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="warehouseEntry"> 确定 </el-button>
      </template>
    </ele-modal>
  </div>
</template>
  
  <script>
import OrderSearch from '@/views/entrust/components/order-search.vue';
import dictMixins from '@/mixins/dictMixins';
import Create from './components/create';
import SalesToProduction from '@/views/unacceptedProduct/components/salesToProduction.vue';

import { getList, warehouseEntry, update } from '@/api/beEntrusted/index';
import {
  getWarehouseList

  // queryKilnXCH
} from '@/api/mes';
export default {
  components: {
    OrderSearch,
    Create,
    SalesToProduction
  },
  mixins: [dictMixins],
  data() {
    return {
      visible: false,
      loading: false,
      id: '',
      current: null,
      warehouseId: '',
      warehouseList: []
    };
  },
  computed: {
    // 表格列配置
    columns() {
      return [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'type',
          label: '类型',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          slot: 'type'
        },
        {
          prop: 'priority',
          label: '紧急程度',
          width: 90,
          align: 'center',
          showOverflowTooltip: true,
          formatter: (row, column, cellValue) => {
            return cellValue == 1 ? '一般' : cellValue == 2 ? '紧急' : '';
          }
        },
        {
          prop: 'code',
          label: '编码',
          align: 'center',
          width: 160,
          showOverflowTooltip: true,
          slot: 'code'
        },
        {
          prop: 'name',
          label: '名称',
          width: 130,
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'workOrderCode',
          label: '工单编码',
          width: 160,
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'procedureList',
          width: 130,
          showOverflowTooltip: true,

          label: '工序',
          align: 'center',
          formatter: (row, column, cellValue) => {
            return cellValue.map((item) => item.produceTaskName).toString();
          }
        },

        // {
        //   prop: 'a',
        //   label: '关联名称',
        //   align: 'center'
        // },
        {
          prop: 'factoriesName',
          label: '请托工厂',
          align: 'center',
          width: 130,
          showOverflowTooltip: true
        },
        {
          prop: 'pleaseEntrustDeptName',
          label: '请托部门',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'pleaseEntrustUserName',
          label: '请托人',
          align: 'center',
          width: 80,
          showOverflowTooltip: true
        },
        {
          prop: 'beEntrustedFactoriesName',
          label: '受托工厂',
          align: 'center',
          width: 130,
          showOverflowTooltip: true
        },
        {
          prop: 'beEntrustedDeptName',
          label: '受托部门',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'finishTime',
          label: '完成时间',
          align: 'center',
          width: 150,
          showOverflowTooltip: true
        },
        {
          prop: 'describes',
          label: '需求描述',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          slot: 'status',
          width: 80,
          showOverflowTooltip: true,
          formatter: (row, column, cellValue) => {
            return cellValue == 0
              ? '未接收'
              : cellValue == 1
              ? '已接收'
              : cellValue == 2
              ? '申请入库'
              : '';
          }
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 220,
          align: 'center',
          resizable: false,
          fixed: 'right',
          slot: 'action',
          showOverflowTooltip: true
        }
      ];
    }
  },
  created() {
    this.requestDict('请托类型');
    getWarehouseList().then((res) => {
      this.warehouseList = res;
    });
  },
  filters: {},
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where }) {
      return getList({
        pageNum: page,
        size: limit,
        ...where
      });
    },
    open(type, row) {
      this.$refs.create.open(type, row);
    },
    salesToProductionOpen(type) {
      this.$refs.salesToProductionRef.open(
        this.current.pleaseEntrustObjList,
        type
      );
    },
    update(id) {
      update({ id, status: 1 }).then((res) => {
        this.reload();
      });
    },
    openWarehouseEntry(id) {
      this.id = id;
      this.visible = true;
    },
    warehouseEntry() {
      if (!this.warehouseId) {
        this.$message.error('请选择仓库');
        return;
      }
      warehouseEntry({
        id: this.id,
        warehouseId: this.warehouseId
      }).then((res) => {
        this.reload();
        this.visible = false;
      });
    },
    /* 刷新表格 */
    reload(where) {
      this.$nextTick(() => {
        this.$refs.table.reload({ page: 1, where });
      });
    }
  }
};
</script>
  
  <style lang="scss" scoped></style>