<template>
  <avue-crud
    :option="option"
    :table-loading="loading"
    :data="data"
    v-model="form"
    v-model:page="page"
    ref="crud"
    @row-del="rowDel"
    @search-change="searchChange"
    @search-reset="searchReset"
    @selection-change="selectionChange"
    @current-change="currentChange"
    @size-change="sizeChange"
    @refresh-change="refreshChange"
    @on-load="onLoad"
    @row-update="rowUpdate"
  >
    <template #menu-left>
      <el-button type="primary" @click="claseOrder()" >关闭订单</el-button>
    </template>
    <template #menu-right> </template>

    <template #ypCode="{ row }">
      <el-text type="primary">{{ row.ypCode }}</el-text>
    </template>
    <template #productIdent="{ row }">
      <el-tag>{{ row.productIdent }}</el-tag>
    </template>
    <template #curStatusText="{ row }">
      <el-tag v-if="row.curStatusText == '已派工'">{{ row.curStatusText }}</el-tag>
      <el-tag class="ml-2" type="success" v-if="row.curStatusText == '已完工'">
        {{ row.curStatusText }}</el-tag
      >
      <el-tag class="ml-2" type="info" v-if="row.curStatusText == '已关闭'">{{
        row.curStatusText
      }}</el-tag>
      <el-tag class="ml-2" type="warning" v-if="row.curStatusText == '加工中'">{{
        row.curStatusText
      }}</el-tag>
      <el-tag class="ml-2" type="danger" v-if="row.curStatusText == '已退回'">{{
        row.curStatusText
      }}</el-tag>
      <el-tag class="ml-2" type="danger" v-if="row.curStatusText == '无工艺'">{{
        row.curStatusText
      }}</el-tag>
    </template>
    <template #demandDate="{ row }">
      <el-text class="mx-1" type="danger">{{ row.demandDate }}</el-text>
    </template>
    <template #menu="{ row }"> </template>
  </avue-crud>
</template>

<script>
import { getList, closeBatchs,updateData  } from '@/api/orderManagement/exceptionOrder';
export default {
  components: {},
  data() {
    return {
      isOpen: false,
      rowItem: {},
      poId: null,
      form: {},
      box: false,
      props: {
        label: 'title',
        value: 'key',
      },
      menuGrantList: [],
      dataScopeGrantList: [],
      apiScopeGrantList: [],
      apiGrantList: [],
      menuTreeObj: [],
      dataScopeTreeObj: [],
      apiScopeTreeObj: [],
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: 'auto',
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 12,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: false,
        editBtnText: '修改',
        labelWidth: 120,
        // searchLabelWidth: 120,
        menuWidth: 80,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        menuAlign: 'center',
        menu:false,
        gridBtn: false,
        searchLabelPosition: 'left',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',

        column: [
          {
            label: '计划单号',
            prop: 'ypCode',
            search: true,
            sortable: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入角色名称',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '零件号',
            prop: 'partCode1',
            search: true,
            sortable: true,
            addDisplay:false,
            editDisplay:false,
            width: 150,
            span: 12,
            hide:true,
            headerAlign: 'center',
            align: 'center',
            // rules: [
            //   {
            //     required: true,
            //     message: '请输入零件号',
            //     trigger: 'blur',
            //   },
            // ],
          },
          {
            label: '零件号',
            prop: 'partCode',
            type:"select",
            search: false,
            sortable: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            dicUrl:'/api/blade-desk/dsPart/getPartList',
            props:{
              label: 'partCode',
              value:'partCode'
            },
            onChange:val =>{
              this.form.partName = val.item.partName
            },
            rules: [
              {
                required: true,
                message: '请选择零件号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '产品型号',
            prop: 'productType',
            search: false,
            sortable: true,
            disabled: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: false,
                message: '请输入产品型号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '产品名称',
            prop: 'partName',
            search: false,
            sortable: true,
            disabled: true,
            span: 12,
            width: 150,
            headerAlign: 'center',
            align: 'left',
            rules: [
              {
                required: false,
                message: '请输入产品名称',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '镀种',
            prop: 'plate',
            search: false,
            sortable: true,
            disabled: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: false,
                message: '请输入镀种',
                trigger: 'blur',
              },
            ],
          },
          // {
          //   label: '镀种小类',
          //   prop: 'plate1',
          //   search: false,
          //   sortable: true,
          //   width: 120,
          //   display: false,
          //   span: 12,
          //   headerAlign: 'center',
          //   align: 'center',
          //   rules: [
          //     {
          //       required: true,
          //       message: '请输入角色名称',
          //       trigger: 'blur',
          //     },
          //   ],
          // },
          {
            label: '面积(d㎡)',
            prop: 'ypArea',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            disabled: true,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: false,
                message: '请输入面积(d㎡)',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '生产标识',
            prop: 'productIdent',
            type: 'select',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            dicUrl:'/api/blade-desk/BA/ProdMark/listForSelect',
            props:{
              label:'name',
              value:'name'
            },
            rules: [
              {
                required: true,
                message: '请输入生产标识',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '使用部门',
            prop: 'useDept',
            search: false,
            sortable: true,
            disabled: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入使用部门',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '生产数量',
            prop: 'ypQty',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入生产数量',
                trigger: 'blur',
              },
            ],
          },

          {
            label: '需求交期',
            prop: 'demandDate',
            search: false,
            sortable: true,
            width: 180,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            type: 'datetime', // 改成 datetime
            valueFormat: 'YYYY-MM-DD HH:mm:ss', // 输出格式
            rules: [
              {
                required: true,
                message: '请输入需求交期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划员',
            prop: 'planUser',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入计划员',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划下达时间',
            prop: 'releaseDate',
            search: false,
            sortable: true,
            searchLabelWidth: 100,
            width: 180,
            span: 12,
            type: 'datetime', // 改成 datetime
            valueFormat: 'YYYY-MM-DD HH:mm:ss', // 输出格式
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入计划下达时间',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '订单状态',
            prop: 'status',
            search: false,
            sortable: true,
            span: 12,
            display: false,
            type: 'select',
            width: '140',
            headerAlign: 'center',
            align: 'center',
            dicData: [
              {
                value: 10000,
                label: '已同步',
              },
              {
                value: 10001,
                label: '订单异常',
              },
              {
                value: 10002,
                label: '未下达',
              },
              {
                value: 10003,
                label: '待排产',
              },
              {
                value: 10004,
                label: '待生产',
              },
              {
                value: 10005,
                label: '加工中',
              },
              {
                value: 10006,
                label: '排产异常',
              },
              {
                value: 10015,
                label: '已完工',
              },
              {
                value: 10021,
                label: '已关闭',
              },
            ],
          },
          {
            label: '备注',
            prop: 'memo',
            search: false,
            sortable: true,
            span: 24,
            type: 'textarea',
            width: 150,
            headerAlign: 'center',
            align: 'left',
            // rules: [
            //   {
            //     required: true,
            //     message: '请输入备注',
            //     trigger: 'blur',
            //   },
            // ],
          },
        ],
      },

      data: [],
    };
  },

  methods: {
    rowUpdate(row, index, done, loading){
      updateData(row).then(res => { 
        if(res.data.code == 200){
          this.$message.success('修改成功')
          this.onLoad()
          done()
        }
      });
    },
    // 关闭订单
    claseOrder() {
      // 检查是否选择了数据
      if (this.selectionList.length === 0) {
        this.$message.error('请至少选择一条数据!');
        return;
      }
      let tmp = this.selectionList.filter(item => item.status === 10015 || item.status === 10021)
      if(tmp.length > 0){
        this.$message.error('请勿选择状态为【已完工】或【已关闭】的数据');
        return
      }
      this.$confirm('确定将选择数据批量关闭?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const ids = this.selectionList.map(item => item.id).join(',');
          closeBatchs({ ...{ ids } }).then(res => {
            if (res.data.code === 200) {
              this.$message.success('关闭成功');
              this.$refs.crud.toggleSelection();
              this.onLoad(this.page);
            }
          });
        })
        .catch(() => {
          // this.onLoad(this.page);
          // this.$message({
          //     type: 'success',
          //     message: '操作成功!',
          // });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleChange(file, fileList) {
      // proxy.$Export.xlsx(file.raw).then((data) => {
      //     data.value = data.results;
      // });
      this.$message({
        type: 'success',
        message: '操作成功!',
      });
    },

    async onLoad(page, params = {}) {
      this.query.partCode = this.query.partCode1
      if(this.query.partCode1){
        delete this.query.partCode1
      }
      this.loading = true;
      try {
        const res = await getList({
          current:this.page.currentPage,
          size:this.page.pageSize,
          validationResult: '16005',
          ...this.query
        });
        // if (res.code) {
          this.data = res.data.data.records;
          this.page.total = res.data.data.total;
          this.selectionClear();
          this.loading = false;
        // }
      } catch (err) {
        console.log(err);
      }
         },
  },
  mounted() {
    // this.option.column = orderCol['exceptionOrder']
  },
};
</script>
