<template>
  <basic-container>
    <div class="search_box"></div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left> </template>
      <template #menu-right> </template>
      <template #menu="scope">
        <!-- 按钮展示规则
            完成：sjMoldPreparation字段是空或者不等于2
            发送：sjKitPreparation字段是空或者不等于2
            车间订单生成：sjKitPreparation字段等于2
         -->
        <!-- 石墨模准备完成 -->
        <el-button
          v-if="scope.row.sjMoldPreparation != 2"
          type="text"
          @click="moldComplete(scope.row.id)"
          >完成</el-button
        >
        <!-- 齐套流转完成 -->
        <el-button
          v-if="scope.row.sjKitPreparation != 2"
          type="text"
          @click="sendKit(scope.row.id)"
          >发送</el-button
        >
        <!-- 车间订单生成 -->
        <el-button v-if="scope.row.sjKitPreparation == 2" type="text" @click="orderGen(scope.row)"
          >车间订单生成</el-button
        >
      </template>

      <template #heatTreat="scope">
        <el-tag v-if="scope.row.afterPlating" type="success">是</el-tag>
        <el-tag v-else type="warning">否</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  moldComplete,
  sendKit,
  loadBigWmsSend,
} from '@/api/productionManagement/sjKitPreparation';

export default {
  components: {},
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: 'auto',
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtnText: '修改',
        labelWidth: 120,
        menuWidth: 140,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '配套人',
            prop: 'pkUsername',
            search: true,
            sortable: true,
            width: 150,
          },
          {
            label: '配套时间',
            prop: 'pkDate',
            search: true,
            sortable: true,
            width: 150,
          },
          {
            label: '计划单号',
            prop: 'ypCode',
            search: true,
            sortable: true,
            width: 150,
          },
          {
            label: '生产单号',
            prop: 'yoCode',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '零件号',
            prop: 'partCode',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '批次号',
            prop: 'batchNo',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '流程卡号',
            prop: 'cardNo',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '产品型号',
            prop: 'productType',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '名称',
            prop: 'partName',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '镀种',
            prop: 'plate',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '生产标识',
            prop: 'productIdent',
            search: false,
            sortable: true,
            width: 150,
          },
          
          {
            label: '使用部门',
            prop: 'useDept',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '数量',
            prop: 'ypQty',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '面积(d㎡)',
            prop: 'ypArea',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '需求交期',
            prop: 'demandDate',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '计划员',
            prop: 'planUser',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '计划下达时间',
            prop: 'releaseDate',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '流转编号',
            prop: 'roamNo',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '备注',
            prop: 'memo',
            search: false,
            sortable: true,
            width: 150,
          },
          {
            label: '状态',
            prop: 'curStatusTitle',
            search: false,
            sortable: true,
            width: 150,
          },
        ],
      },

      data: [],
    };
  },
  methods: {
    // 完成
    moldComplete(yoId) {
      this.$confirm('模具准备完成？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.loading = true;
          moldComplete({ yoId: yoId }).then(res => {
            this.$message.success('操作成功');
            this.onLoad(this.page, this.query);
          });
        })
        .catch(() => {
          this.$message.info('已取消');
        });
    },
    // 发送
    sendKit(yoId) {
      this.$confirm('发送至待生成车间订单？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.loading = true;
          sendKit({ yoId: yoId }).then(res => {
            this.$message.success('操作成功');
            this.onLoad(this.page, this.query);
          });
        })
        .catch(() => {
          this.$message.info('已取消');
        });
    },
    // 车间订单生成
    orderGen(row) {
      this.$confirm('生成车间订单？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.loading = true;
          loadBigWmsSend({ id: row.id }).then(res => {
            this.$message.success('操作成功');
            this.onLoad(this.page, this.query);
          });
        })
        // .catch(() => {
        //   // this.$message.info('已取消');
        //   this.onLoad(this.page, this.query);
        // });
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page, this.query);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    onLoad(page, params = {}) {
      this.loading = true;
      this.data = [];
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.data = res.data.data.records;
        this.loading = false;
        this.page.total = res.data.data.total;
      });
    },
  },
};
</script>
