<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"
      >
        <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:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="open('add')"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="remove()"
            plain
            :disabled="selection.length == 0"
          >
            删除
          </el-button>

          <!-- <el-button
            size="small"
            type="primary"
            icon="el-icon-circle-plus-outline"
            class="ele-btn-icon"
            plain
            :disabled="selection.length == 0"
          >
            处置
          </el-button> -->
        </template>

        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            v-if="row.status == 0"
            :underline="false"
            icon="el-icon-truck"
            @click="open('edit', row)"
          >
            编辑
          </el-link>
          <el-link
            type="primary"
            v-if="row.status == 2"
            :underline="false"
            icon="el-icon-truck"
            @click="openWarehouseEntry(row.id)"
          >
            申请入库
          </el-link>
          <el-popconfirm
            class="ele-action"
            v-if="row.status == 0"
            title="确定要删除吗？"
            @confirm="remove([row.id])"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>

          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-circle-plus-outline"
            @click="submit(row)"
            v-if="row.status == 0"
          >
            提交
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <Create ref="create" @refresh="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 './components/order-search.vue';
import dictMixins from '@/mixins/dictMixins';
import Create from './components/create';
import {
  getWarehouseList

  // queryKilnXCH
} from '@/api/mes';
import { getList, remove, submit, warehouseEntry } from '@/api/entrust/index';

export default {
  components: {
    OrderSearch,
    Create
  },
  mixins: [dictMixins],
  data() {
    return {
      visible: false,
      loading: false,
      id: '',
      warehouseId: '',
      selection: [],
      warehouseList: []
    };
  },
  computed: {
    // 表格列配置
    columns() {
      return [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          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: 240,
          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);
    },
    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;
      })
    },
    remove(id) {
      let ids = id || this.selection.map((item) => item.id);
      remove(ids).then((res) => {
        this.$message.success('删除' + res);
        this.reload();
      });
    },

    submit(item) {
      submit({
        businessId: item.id,
        beEntrustedDeptId: item.beEntrustedDeptId
      }).then((res) => {
        this.$message.success('提交' + res.message);
        this.reload();
        this.visible = false;
      });
    },
    /* 刷新表格 */
    reload(where) {
      this.$nextTick(() => {
        this.$refs.table.reload({ page: 1, where });
      });
    }
  }
};
</script>
  
  <style lang="scss" scoped></style>