﻿<template>
	<div class="mesPackTransferLog-container">
		<el-dialog v-model="state.isShowDialog" v-if="state.isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="扫描载具二维码" prop="flowBarcode">
                            <el-input v-model="state.ruleForm.flowBarcode" placeholder="请扫描载具二维码" clearable @keyup.enter="scanCode"  >
                                <template #append>
                                    <el-icon  @click="scanCode"><ele-FullScreen /></el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移前载具编码" prop="packCode">
							<el-input v-model="state.ruleForm.packCode" readonly="true" placeholder="请输入转移前载具编码" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移前载具名称" prop="packName">
							<el-input v-model="state.ruleForm.packName" readonly="true" placeholder="请输入转移前载具名称" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移前载具类型编码" prop="packTypeCode">
							<el-input v-model="state.ruleForm.packTypeCode" readonly="true" placeholder="请输入转移前载具类型编码" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移前生产工单号" prop="produceWorkOrderNo">
							<el-input v-model="state.ruleForm.produceWorkOrderNo" readonly="true" placeholder="请输入转移前生产工单号" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移前工序" prop="processCode">
							<el-input v-model="state.ruleForm.processCode" readonly="true" placeholder="请输入转移前工序" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="扫描转移后载具二维码" prop="flow">
                            <el-input v-model="state.ruleForm.flow" placeholder="请扫描转移后载具二维码" clearable @keyup.enter="scanList"  >
                                <template #append>
                                    <el-icon  @click="scanList"><ele-FullScreen /></el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移后载具编码" prop="packCodeAfter">
							<el-input v-model="state.ruleForm.packCodeAfter" readonly="true" placeholder="请输入转移后载具编码" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移后载具名称" prop="packNameAfter">
							<el-input v-model="state.ruleForm.packNameAfter" readonly="true" placeholder="请输入转移后载具名称" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移后载具类型编码" prop="packTypeCodeAfter">
							<el-input v-model="state.ruleForm.packTypeCodeAfter" readonly="true" placeholder="请输入转移后载具类型编码" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移后生产工单号" prop="produceWorkOrderNoAfter">
							<el-input v-model="state.ruleForm.produceWorkOrderNoAfter" readonly="true" placeholder="请输入转移后生产工单号" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移后工序" prop="processCodeAfter">
							<el-input v-model="state.ruleForm.processCodeAfter" readonly="true" placeholder="请输入转移后工序" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="转移装载量" prop="num">
							<el-input-number v-model="state.ruleForm.num" min="0" :precision="0" placeholder="请输入转移装载量" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="MES编码" prop="materialCode">
							<el-input v-model="state.ruleForm.materialCode" readonly="true" placeholder="请输入MES编码" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="MES名称" prop="materialName">
							<el-input v-model="state.ruleForm.materialName" readonly="true"  placeholder="请输入MES名称" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="单位用量" prop="unitUse">
							<el-input v-model="state.ruleForm.unitUse" readonly="true" placeholder="请输入单位用量" maxlength="10" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="批次号" prop="batchNo">
							<el-input v-model="state.ruleForm.batchNo" readonly="true" placeholder="请输入批次号" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属工厂编码" prop="factoryCode">
							<el-input v-model="state.ruleForm.factoryCode" disabled="true" placeholder="请输入所属工厂编码" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="描述" prop="remark">
							<el-input v-model="state.ruleForm.remark"  show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
    import { ref,onMounted, reactive } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addMesPackTransferLog, updateMesPackTransferLog, detailMesPackTransferLog,Scan } from "/@/api/simple/mesPackTransferLog";
	import { pageMesPackTransferLog, deleteMesPackTransferLog } from '/@/api/simple/mesPackTransferLog';

  	import { formatDate } from '/@/utils/formatTime';
import { off } from "process";
import { Console } from "console";

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
    const state = reactive({
    isShowDialog: false,
    ruleForm: {} as any,
});

	//自行添加其他规则
	const rules = ref<FormRules>({
		packCode: [{required: true, message: '转移前载具编码，请先扫描载具！', trigger: 'blur',},],
		packCodeAfter: [{required: true, message: '转移后载具编码，请先扫描转移后载具！', trigger: 'blur',},],
		num: [{required: true, message: '转移装载量不可以为空！', trigger: 'blur',},],
	});

	// 页面加载时
	onMounted(() => {

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// state.ruleForm = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			state.ruleForm = (await detailMesPackTransferLog(rowData.id)).data.result;
		else
			state.ruleForm = rowData;
		state.isShowDialog = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		state.isShowDialog = false;
	};

	// 取消
	const cancel = () => {
		state.isShowDialog = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if(state.ruleForm.packCode==undefined)
		    {
				return ElMessage({
					message: `请先扫描转移前载具码！`,
					type: "error",
				});
			}
			if(state.ruleForm.packCodeAfter==undefined)
		    {
				return ElMessage({
					message: `请先扫描转移后载具码！`,
					type: "error",
				});
			}
			if(state.ruleForm.num==undefined || state.ruleForm.num<=0)
		    {
				return ElMessage({
					message: `载具转移数量不可以小于等于或者空`,
					type: "error",
				});
			}
			if(parseFloat(state.ruleForm.num)> parseFloat(state.ruleForm.number))
		    {
				return ElMessage({
					message: `载具转移数量不可以大于转移前的载具数量`,
					type: "error",
				});
			}
			if (isValid) {
				let values = state.ruleForm;
				if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
					//await addMesPackTransferLog(values);
					var result=(await addMesPackTransferLog(values)).data.result;
					if(result=="转移成功")
					 closeDialog();
					else
					{
						ElMessage({
					    message: `${'转移失败'}`,
					    type: "error",
				        });
					}
				} else {
					await updateMesPackTransferLog(values);
					closeDialog();
				}
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};


  // 扫码载具转移前条码
  const scanCode = async ()=>{
      
      let  res = await Scan({
        packCode:state.ruleForm.flowBarcode,
      })
      let data = res.data.result || {};
	//   console.log(data);
	//   if(data.stockCode==undefined)
	//   {
	// 	state.ruleForm.packId="";
    //     state.ruleForm.packCode="";
	//     state.ruleForm.packName="";
    //     state.ruleForm.packTypeCode="";
	//     state.ruleForm.produceWorkOrderNo="";
	//     state.ruleForm.processCode="";
	//     state.ruleForm.num="";
	//     state.ruleForm.materialCode="";
	//     state.ruleForm.materialName="";
	//     state.ruleForm.unitUse="";
	//     state.ruleForm.batchNo="";
	//     state.ruleForm.factoryCode="";
	// 	state.ruleForm.number=0;
	//     state.ruleForm.numberMax=0;
	// 	return ElMessage({
    //     message: `系统中不存在当前载具信息`,
    //     type: "error",
    //   });
	//   }
	if(data.packStatus==1)
     {
		state.ruleForm.packId="";
        state.ruleForm.packCode="";
	    state.ruleForm.packName="";
        state.ruleForm.packTypeCode="";
	    state.ruleForm.produceWorkOrderNo="";
	    state.ruleForm.processCode="";
	    state.ruleForm.num="";
	    state.ruleForm.materialCode="";
	    state.ruleForm.materialName="";
	    state.ruleForm.unitUse="";
	    state.ruleForm.batchNo="";
	    state.ruleForm.factoryCode="";
		state.ruleForm.number=0;
	    state.ruleForm.numberMax=0;
		state.ruleForm.flowinvestor=undefined;
  	   return ElMessage({
       message: `载具状态不可以是使用中的载具！`,
       type: "error",
     });
     }
	  //载具不可以是在库存中的载具
	  if(data.storeCode!=undefined && data.storeCode!=null && data.storeCode!="")
     {
		state.ruleForm.packId="";
        state.ruleForm.packCode="";
	    state.ruleForm.packName="";
        state.ruleForm.packTypeCode="";
	    state.ruleForm.produceWorkOrderNo="";
	    state.ruleForm.processCode="";
	    state.ruleForm.num="";
	    state.ruleForm.materialCode="";
	    state.ruleForm.materialName="";
	    state.ruleForm.unitUse="";
	    state.ruleForm.batchNo="";
	    state.ruleForm.factoryCode="";
		state.ruleForm.number=0;
	    state.ruleForm.numberMax=0;
		state.ruleForm.flowinvestor=undefined;
  	   return ElMessage({
       message: `载具状态不可以是在库存中的载具！`,
       type: "error",
     });
     }
	  if(data.materialCode==undefined && data.materialCode!=null && data.materialCode!="")
	  {
		state.ruleForm.packId="";
        state.ruleForm.packCode="";
	    state.ruleForm.packName="";
        state.ruleForm.packTypeCode="";
	    state.ruleForm.produceWorkOrderNo="";
	    state.ruleForm.processCode="";
	    state.ruleForm.num="";
	    state.ruleForm.materialCode="";
	    state.ruleForm.materialName="";
	    state.ruleForm.unitUse="";
	    state.ruleForm.batchNo="";
	    state.ruleForm.factoryCode="";
		state.ruleForm.number=0;
	    state.ruleForm.numberMax=0;
		return ElMessage({
        message: `当前载具为空载具，不可以作为被转移载具使用`,
        type: "error",
      });
	  }
      state.ruleForm.packId=data.id;
      state.ruleForm.packCode=data.packCode;
	  state.ruleForm.packName=data.packName;
      state.ruleForm.packTypeCode=data.packTypeCode;
	  state.ruleForm.produceWorkOrderNo=data.produceWorkOrderNo;
	  state.ruleForm.processCode=data.processCode;
	  state.ruleForm.num=data.num;
	  state.ruleForm.materialCode=data.materialCode;
	  state.ruleForm.materialName=data.materialName;
	  state.ruleForm.unitUse=data.unitUse;
	  state.ruleForm.batchNo=data.batchNo;
	  state.ruleForm.factoryCode=data.factoryCode;
	  state.ruleForm.number=data.num;
	  state.ruleForm.numberMax=data.maxNum;
	  state.ruleForm.packStatus=data.packStatus;
	  console.log(data.packStatus);
	  state.ruleForm.productStatus=data.productStatus;
      state.ruleForm.flowBarcode = "";
    }

// 扫码载具转移后条码
const scanList = async ()=>{
      
	  if(state.ruleForm.packCode==undefined)
	  {
		return ElMessage({
        message: `请先扫描需要转移的载具`,
        type: "error",
      });
	  }
      let  res = await Scan({
        packCode:state.ruleForm.flow,
      })
      let data = res.data.result || {};
	//   if(data.stockCode==undefined)
	//   {
	// 	state.ruleForm.packIdAfter="";
    //     state.ruleForm.packCodeAfter="";
	//     state.ruleForm.packNameAfter="";
    //     state.ruleForm.packTypeCodeAfter="";
	//     state.ruleForm.produceWorkOrderNoAfter="";
	//     state.ruleForm.processCodeAfter="";
	//     state.ruleForm.numberAfter=0;
	//     state.ruleForm.numberMaxAfter=0;
	// 	return ElMessage({
    //     message: `系统中不存在当前载具信息`,
    //     type: "error",
    //   });
	//   }
		  //载具不可以是使用中的状态
		  if(data.packStatus==1)
     {
		 state.ruleForm.packIdAfter="";
         state.ruleForm.packCodeAfter="";
	     state.ruleForm.packNameAfter="";
         state.ruleForm.packTypeCodeAfter="";
	     state.ruleForm.produceWorkOrderNoAfter="";
	     state.ruleForm.processCodeAfter="";
	     state.ruleForm.numberAfter=0;
	     state.ruleForm.numberMaxAfter=0;
		state.ruleForm.flowinvestor=undefined;
  	   return ElMessage({
       message: `载具状态不可以是使用中的载具！`,
       type: "error",
     });
     }
	  //载具不可以是在库存中的载具
	  if(data.storeCode!=undefined && data.storeCode!=null && data.storeCode!="")
     {
		 state.ruleForm.packIdAfter="";
         state.ruleForm.packCodeAfter="";
	     state.ruleForm.packNameAfter="";
         state.ruleForm.packTypeCodeAfter="";
	     state.ruleForm.produceWorkOrderNoAfter="";
	     state.ruleForm.processCodeAfter="";
	     state.ruleForm.numberAfter=0;
	     state.ruleForm.numberMaxAfter=0;
		state.ruleForm.flowinvestor=undefined;
  	   return ElMessage({
       message: `载具状态不可以是在库存中的载具！`,
       type: "error",
     });
     }
    if(data.packCode==state.ruleForm.packCode)
    {
         state.ruleForm.packCodeAfter="";
	     state.ruleForm.packNameAfter="";
         state.ruleForm.packTypeCodeAfter="";
	     state.ruleForm.produceWorkOrderNoAfter="";
	     state.ruleForm.processCodeAfter="";
	     state.ruleForm.numberAfter=0;
	     state.ruleForm.numberMaxAfter=0;
  	return ElMessage({
      message: `转移后的载具不允许和转移前的载具相同`,
      type: "error",
    });
    }
	  if(data.materialCode!=undefined && data.materialCode!=null && data.materialCode!="")
	  {
		if(data.materialCode!=state.ruleForm.materialCode && data.batchNo!=state.ruleForm.batchNo && data.processCode!=state.ruleForm.processCode && data.produceWorkOrderNo!=state.ruleForm.produceWorkOrderNo)
	    {
		  state.ruleForm.packIdAfter="";
          state.ruleForm.packCodeAfter="";
	      state.ruleForm.packNameAfter="";
          state.ruleForm.packTypeCodeAfter="";
	      state.ruleForm.produceWorkOrderNoAfter="";
	      state.ruleForm.processCodeAfter="";
	      state.ruleForm.numberAfter=0;
	      state.ruleForm.numberMaxAfter=0;
		  return ElMessage({
          message: `不是同一批次，同一产品，同一工序，同一计划的不允许相互转移`,
          type: "error",
        });
	    }
	  }
	  
      state.ruleForm.packIdAfter=data.id;
      state.ruleForm.packCodeAfter=data.packCode;
	  state.ruleForm.packNameAfter=data.packName;
      state.ruleForm.packTypeCodeAfter=data.packTypeCode;
	  state.ruleForm.produceWorkOrderNoAfter=data.produceWorkOrderNo;
	  state.ruleForm.processCodeAfter=data.processCode;
	  state.ruleForm.numberAfter=data.num;
	  state.ruleForm.numberMaxAfter=data.maxNum;
	  state.ruleForm.afterPackStatus=data.packStatus;
	  state.ruleForm.afterProductStatus=data.productStatus;
      state.ruleForm.flow = "";
    }



	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




