<template>
  <basic-container>
    <!-- 热表生产监控 -->
    <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"
    >
      <template #menu-left>
        <el-button type="primary" @click="handlePrintTags">过程记录打印</el-button>
        <el-button type="primary" @click="handlePrintTags">打印标签</el-button>
        <el-button type="primary" @click="priorityFn">优先级调整</el-button>
        <!-- <el-button type="primary" icon="el-icon-printer" plain @click="handlePrint">打印</el-button>
        <el-button type="primary" icon="el-icon-monitor" @click="handleOutsource(null, 'all')">批量外协</el-button> -->
        <!-- <el-button type="success" icon="el-icon-download">导出</el-button> -->
      </template>

      <template #menu-right>
        <el-button type="success" plain @click="queryOrdersHandle">在制品</el-button>
        <el-button type="warning" plain>加工中</el-button>
        <el-button type="danger" plain>检验中</el-button>
        <!-- <el-button type="success">已下达</el-button> -->
        <el-button type="success" plain>已完工</el-button>
        <!-- <el-button type="primary">全部</el-button> -->
      </template>

      <template #menu="scope">
        <el-button type="text" @click="clickProduction(scope.row)">生产监控</el-button>
        <el-button type="text" @click="handleClose(scope.row)" v-show="scope.row.runStatus < 15"
          >关闭</el-button
        >
        <!-- <el-button type="text">打印</el-button> -->
        <el-dropdown
          @command="handleCommand"
          style="margin-top: 4px; margin-left: 12px"
          v-show="
            scope.row.approvalStatus != 2 &&
            scope.row.oemType === -1 &&
            scope.row.runStatus < 15 &&
            scope.row.runStatus !== 5
          "
        >
          <span class="el-dropdown-link" style="color: #284c89; cursor: pointer">
            更多<i class="el-icon-arrow-down el-icon--right" />
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <!-- <el-dropdown-item @click="handleIssued(scope.row)"
                v-show="scope.row.runStatus === 1">下达</el-dropdown-item> -->
              <!-- 分批按钮（状态2、3、4、6显示） -->
              <el-dropdown-item
                @click="handleBatches(scope.row)"
                v-if="[2, 3, 4, 6].includes(scope.row.runStatus)"
                >分批</el-dropdown-item
              >
              <el-dropdown-item
                @click="handleOutsource(scope.row)"
                v-if="scope.row.runStatus <= 6 && scope.row.runStatus !== 5"
                >外协</el-dropdown-item
              >
              <!-- <el-dropdown-item @click="handleReturn(scope.row)"
                v-show="scope.row.runStatus === 2">退货</el-dropdown-item> -->
              <!-- <el-dropdown-item @click="handleScheduling(scope.row)"
                v-show="scope.row.runStatus < 15 && scope.row.priority != 5">调度标注</el-dropdown-item> -->
              <!-- <el-dropdown-item @click="handleClose(scope.row)" v-show="scope.row.runStatus < 15">关闭</el-dropdown-item> -->
              <!-- <el-dropdown-item @click="handleSintering(scope.row)"
                v-show="scope.row.runStatus < 15">转烧结</el-dropdown-item> -->
              <el-dropdown-item @click="priorityFn(scope.row)" v-if="scope.row.runStatus === 2"
                >优先级调整</el-dropdown-item
              >
              <el-dropdown-item
                @click="handleAbnormal(scope.row)"
                v-if="scope.row.runStatus === 3 || scope.row.runStatus === 4"
                >异常申报</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
          <!-- <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-show="scope.row.runStatus === 1"
                  :command="composeValue('send', scope.row)">下达</el-dropdown-item>
              </el-dropdown-menu> -->
        </el-dropdown>
      </template>

      <!-- <template #tsName="{ row }">{{ row.makeTeam && row.makeTeam.tsName }}</template>
      <template #ppsName="{ row }">{{ row.procedureSet && row.procedureSet.ppsName }}</template>
      <template #partCode="{ row }">{{ row.pjYieldOrder && row.pjYieldOrder.partCode }}</template>
      <template #prodIdent="{ row }">{{ row.pjYieldOrder && row.pjYieldOrder.prodIdent }}</template>
      <template #partName="{ row }">{{ row.pjYieldOrder && row.pjYieldOrder.partName }}</template>
      <template #plate="{ row }">{{ row.pjYieldOrder && row.pjYieldOrder.plate }}</template>
      <template #planEndDate="{ row }">
        {{ Array.isArray(row.planEndDate) ? row.planEndDate.join(' ~ ') : row.planEndDate || '-' }}
      </template>
      <template #planTime="{ row }">
        {{ Array.isArray(row.planTime) ? row.planTime.join(' ~ ') : row.planTime || '-' }}
      </template>
      <template #putStoreTime="{ row }">
        {{
          Array.isArray(row.putStoreTime) ? row.putStoreTime.join(' ~ ') : row.putStoreTime || '-'
        }}
      </template>
      <template #productType="{ row }">{{
        row.pjYieldOrder && row.pjYieldOrder.productType
      }}</template>
      <template #useDept="{ row }">{{ row.pjYieldOrder && row.pjYieldOrder.useDept }}</template>
      <template #poArea="{ row }">{{ row.pjYieldOrder && row.pjYieldOrder.poArea }}</template>
      <template #userName="{ row }">{{ row.dispatcher && row.dispatcher.userName }}</template>
      <template #nextPpsName="{ row }">{{
        row.nextProcedureSet && row.nextProcedureSet.ppsName
      }}</template>
      <template #nextTeamName="{ row }">{{ row.nextMakeTeam && row.nextMakeTeam.tsName }}</template>
      <template #failureMan="{ row }">{{
        row.pjYieldOrder && row.pjYieldOrder.failureMan && row.pjYieldOrder.failureMan.userName
      }}</template> -->
    </avue-crud>
    <!-- 打印标签 -->
    <el-dialog append-to-body v-model="printDialog" title="打印标签">
      <el-form ref="printForm" :model="printForm" :rules="printRules">
        <el-form-item label="数量" prop="quantity">
          <el-input
            v-model="printForm.quantity"
            placeholder="请输入数量"
            oninput="value=value.replace(/[^\d]/g,'')"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="printDialog = false">取 消</el-button>
          <el-button type="primary" @click="submitPrint">打 印</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 转外协弹窗 -->
    <outsource-dialog
      :show-dialog="showDialog"
      :show-radio="showRadio"
      @closeDialog="closeDialog"
    ></outsource-dialog>
    <!-- 生产监控 -->
    <production-dialog
      :monitor-dialog="monitorDialog"
      :row-item="rowItem"
      @closeDialog="closeDialog"
    ></production-dialog>
    <!-- 分批弹窗  -->
    <batches-dialog :show-batches="showBatches" @closeDialog="closeDialog"></batches-dialog>
    <!-- 关闭弹窗  -->
    <closed-dialog :show-close="showClose" @closeDialog="closeDialog"></closed-dialog>
    <!-- 退货弹窗 -->
    <return-dialog :show-return="showReturn" @closeDialog="closeDialog"></return-dialog>
    <!-- 调度标注 -->
    <scheduling-dialog
      :show-scheduling="showScheduling"
      @closeDialog="closeDialog"
    ></scheduling-dialog>
    <!-- 异常处理 -->
    <abnormalDialog :show-abnormal="showAbnormal" @closeDialog="closeDialog"></abnormalDialog>
    <productionMonitoringDialog
      :showDialog="isPriorityOpen"
      v-if="isPriorityOpen"
      @closeDialog="closeDialog"
      @submitPriority="handlePrioritySubmit"
    ></productionMonitoringDialog>
  </basic-container>
</template>

<script>
import outsourceDialog from './components/outsourceDialog.vue';
import productionDialog from './components/productionDialog.vue';
import batchesDialog from './components/batchesDialog.vue';
import closedDialog from './components/closedDialog.vue';
import returnDialog from './components/returnDialog.vue';
import schedulingDialog from './components/schedulingDialog.vue';
import abnormalDialog from './components/abnormalDialog.vue';
import productionMonitoringDialog from './components/productionMonitoringDialog.vue';

import { getList } from '@/api/productionManagement/productionMonitoring';

export default {
  components: {
    outsourceDialog,
    productionDialog,
    batchesDialog,
    closedDialog,
    returnDialog,
    schedulingDialog,
    abnormalDialog,
    productionMonitoringDialog,
  },
  data() {
    return {
      isPriorityOpen: false,
      showAbnormal: false,
      showDialog: false,
      showRadio: true,
      monitorDialog: false,
      showBatches: false,
      showClose: false,
      showReturn: false,
      showScheduling: false,
      data: [],
      printForm: {},
      printRules: {
        quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }],
      },
      rowItem: {},
      selectionList: [],
      form: {},
      loading: false,
      printDialog: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: 'auto',
        calcHeight: 32,
        rowKey: 'cardNo',
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: false,
        editBtnText: '修改',
        viewBtnText: '详情',
        labelWidth: 120,
        menuWidth: 220,
        dialogWidth: 1200,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '主加工单位',
            prop: 'tsName',
            search: true,
            sortable: true,
            overHidden: true,
            fixed: 'left',
            width: 200,
            searchLabelWidth: 100,
          },

          {
            label: '当前工序',
            prop: 'ppsName',
            fixed: 'left',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '下一工序',
            prop: 'nextPpsName',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '订单状态',
            prop: 'runStatusTitle',
            type: 'select',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,

            dicData: [
              {
                value: 2,
                label: '待生产',
              },
              {
                value: 3,
                label: '生产中',
              },
              {
                value: 4,
                label: '检验中',
              },
              {
                value: 6,
                label: '审理中',
              },
              {
                value: 5,
                label: '工艺变更',
              },
              {
                value: 19,
                label: '已返工',
              },
              {
                value: 15,
                label: '已完成',
              },
              {
                value: 21,
                label: '已作废',
              },
            ],
          },
          {
            label: '车间订单号',
            prop: 'woCode',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            searchLabelWidth: 100,
          },
          {
            label: '加工单位',
            prop: 'processingUnit',
            search: false,
            sortable: true,
            fixed: 'left',
            overHidden: true,
            hide: true,
            width: 200,
          },
          {
            label: '加工班组',
            prop: 'tsId',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            hide: true,
          },
          {
            label: '加工外协商',
            prop: 'ocId',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            hide: true,
            searchLabelWidth: 100,
          },
          {
            label: '生产订单号',
            prop: 'yoCode',
            search: true,
            sortable: true,
            overHidden: true,
            // hide: true,
            width: 200,
            searchLabelWidth: 100,
          },
          {
            label: '计划单号',
            prop: 'poCode',
            search: true,
            sortable: true,
            // hide: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '返工单号',
            prop: 'qcReworkCode',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '零件号',
            prop: 'partCode',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '批次号',
            prop: 'batchNo',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '流程卡号',
            prop: 'cardNo',
            search: true,
            sortable: true,
            overHidden: true,
            hide: true,
            width: 200,
          },
          {
            label: '产品型号',
            prop: 'productModel',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '产品名称',
            prop: 'false',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: 'MES流程卡号',
            prop: 'false',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '生产标识',
            prop: 'prodIdent',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '镀种信息',
            prop: 'plate',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '生产数量',
            prop: 'makeQty',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '面积(dm²)',
            prop: 'poArea',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '单批次面积',
            prop: 'batchArea',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '订单优先级',
            prop: 'priorityTitle',
            type: 'select',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
            searchLabelWidth: 100,
            dicData: [
              {
                value: 1,
                label: '正常-白色',
              },
              {
                value: 2,
                label: '项目要求日期急件-橙色',
              },
              {
                value: 3,
                label: '合同急件-黄色',
              },
              {
                value: 4,
                label: '绩效零件-青色',
              },
              {
                value: 5,
                label: '调度标注急件-蓝色',
              },
            ],
          },
          {
            label: '需求部门',
            prop: 'useDept',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '需求交期',
            prop: 'demandDate',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '会议要求周期',
            prop: 'meetCycle',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '调度员',
            prop: 'userName',
            search: true,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '协作计划',
            prop: 'collaborateText',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '计划下达时间',
            prop: 'planTime',
            sortable: true,
            filter: true,
            selectWidth: 150,
            searchLabelWidth: 100,
            span: 12,
            search: true,
            width: 200,
            searchSpan: 8,
            type: 'datetimerange',
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            rangeSeparator: '-',
            searchRange: true,
          },
          {
            label: '计划完工时间',
            prop: 'planEndDate',
            search: false,
            sortable: true,
            overHidden: true,
            searchLabelWidth: 100,
            type: 'datetimerange',
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            startPlaceholder: '时间日期开始',
            endPlaceholder: '时间日期结束',
            width: 200,
          },

          {
            label: '镀后入库时间',
            prop: 'putStoreTime',
            sortable: true,
            filter: true,
            span: 12,
            search: true,
            searchSpan: 8,
            width: 200,
            searchLabelWidth: 100,
            type: 'datetimerange',
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            rangeSeparator: '-',
            searchRange: true,
          },

          {
            label: '外协状态',
            prop: 'oemType',
            type: 'select',
            search: false,
            hide: true,
            sortable: true,
            overHidden: true,
            width: 200,

            dicData: [
              {
                value: -1,
                label: '场内',
              },
              {
                value: 2,
                label: '外协',
              },
            ],
          },

          {
            label: '生产数量',
            prop: 'productionQuantity',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '入库数量',
            prop: 'inventoryQty',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '试验数量',
            prop: 'testQuantity',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
          {
            label: '报废数量',
            prop: 'scrapQuantity',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },

          {
            label: '备注',
            prop: 'memo',
            search: false,
            sortable: true,
            overHidden: true,
            width: 200,
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    // 优先级调整
    priorityFn(row) {
      this.rowItem = row;
      console.log(row.runStatus, 123);
      this.isPriorityOpen = true;
      // console.log(runStatus,123)
    },
    handlePrioritySubmit(data) {
      this.$message.success(
        `优先级已调整为${data.priority === 1 ? '一级' : data.priority === 2 ? '二级' : '三级'}`
      );
    },
    selectionChange(val) {
      this.selectionList = val;
    },
    // 转外协
    handleOutsource(row, type) {
      if (type == 'all') {
        if (this.selectionList.length == 0) {
          this.$message.error('请先选择数据');
        } else {
          this.showDialog = true;
        }
      } else {
        this.showDialog = true;
      }
    },
    // 退货
    handleReturn() {
      this.showReturn = true;
    },
    // 下达
    handleIssued() {
      this.$confirm('确认下达车间订单吗?', '确认消息', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {});
    },
    // 分批
    handleBatches() {
      this.showBatches = true;
    },
    // 调度标注
    handleScheduling() {
      this.showScheduling = true;
    },
    // 关闭
    handleClose() {
      this.showClose = true;
    },
    // 转烧结
    handleSintering() {
      this.$confirm('确定将此订单转到烧结中心吗?', '是否继续', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {});
    },
    // 生产监控
    clickProduction(row) {
      this.rowItem = row;
      this.monitorDialog = true;
    },
    // 打印标签
    handlePrintTags() {
      if (this.selectionList.length == 0) {
        this.$message.error('请先选择数据');
      } else {
        this.printForm = {
          quantity: 1,
        };
        this.printDialog = true;
      }
    },
    // 确定打印
    submitPrint() {
      this.$refs.printForm.validate(valid => {
        if (valid) {
          this.printDialog = false;
        }
      });
    },
    // 打印
    handlePrint() {
      if (this.selectionList.length == 0) {
        this.$message.error('请先选择数据');
      } else {
      }
    },
    //查询【生产中3、工艺变更5、检验中4、审理中6】状态的订单
    queryOrdersHandle() {
      const targetStatuses = [3, 5, 4, 6];
      const filteredData = this.data.filter(item => targetStatuses.includes(item.runStatus));
      const originalData = [...this.data];
      this.data = filteredData;
      this.$message.success(`已筛选出${filteredData.length}条符合条件的订单`);
    },
    // 异常申报
    handleAbnormal() {
      this.showAbnormal = true;
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false;
      this.monitorDialog = false;
      this.showBatches = false;
      this.showClose = false;
      this.showReturn = false;
      this.showScheduling = false;
      this.showAbnormal = false;
      this.isPriorityOpen = false;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    searchReset() {
      this.query = {};
      this.treeDeptId = '';
      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();
    },
    onLoad(page, params = {}) {
      // this.loading = true;
      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;
        this.selectionClear();
      });
    },
  },
};
</script>

<style></style>
