﻿<template>
	<div class="mesMonthSalePlan-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="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="销售计划号" prop="saleCode" v-if="props.title=='添加月销售计划'==false">
							<el-input v-model="state.ruleForm.saleCode" placeholder="请输入销售计划号" disabled="true" maxlength="50" 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="产品ERPCode" prop="partCode">
							<el-input v-model="state.ruleForm.partCode" placeholder="请输入产品ERPCode" maxlength="50" 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="partCode">
                            <el-input v-model="state.ruleForm.partCode" placeholder="请选择产品MES编码" readonly="true"  maxlength="100" show-word-limit clearable >
                        <template #append v-if="props.title=='编辑月销售计划'==false">
                            <el-button @click="openChoose" >选择</el-button>
                        </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="partName">
							<el-input v-model="state.ruleForm.partName" placeholder="请输入产品名称" readonly="true" 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="partModel">
							<el-input v-model="state.ruleForm.partModel" placeholder="请输入产品规格型号" readonly="true" 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="commissioningNumber">
							<el-input-number v-model="state.ruleForm.commissioningNumber" :precision="0" min="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="待指令数量" prop="instructionNumber">
							<el-input-number v-model="state.ruleForm.instructionNumber" 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="月份" prop="month">
							<el-date-picker v-model="monthvalue" type="month"   placeholder="请输入月份"  :default-value="new Date(2024,10)"  />
							<!-- format="YYYY-MM" value-format="YYYY-MM"  -->
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="可生成生产计划日期" prop="monthDate">
							<el-date-picker v-model="state.ruleForm.monthDate"   placeholder="请输入可生成生产计划日期"    show-word-limit clearable />
							<!-- format="YYYY-MM" value-format="YYYY-MM"  -->
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="投产数量" prop="number">
							<el-input-number v-model="state.ruleForm.number" disabled="true" 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="在制数量" prop="makeNumber">
							<el-input-number v-model="state.ruleForm.makeNumber"  disabled="true" 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="完工数量" prop="compleNumber">
							<el-input-number v-model="state.ruleForm.compleNumber" disabled="true" 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="下发月生产计划" prop="isOrderPlan">
							<el-switch v-model="state.ruleForm.isOrderPlan"  active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="停产状态" prop="stopState">
							<el-switch v-model="state.ruleForm.stopState" active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="完工状态" prop="recrdState">
							<el-switch v-model="state.ruleForm.recrdState" active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col> -->
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序" prop="sortCode">
							<el-input-number v-model="state.ruleForm.sortCode" 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="描述" prop="remark">
							<el-input v-model="state.ruleForm.remark" :rows="4"  show-word-limit clearable />
							<!-- placeholder="请输入描述" maxlength="155" -->
						</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>
		<choose-material title="选择产品" ref="chooseMaterialRef" :is-any="false" @finish="onChooseMaterialFinish"></choose-material>
	</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 { addMesMonthSalePlan, updateMesMonthSalePlan, detailMesMonthSalePlan } from "/@/api/simple/mesMonthSalePlan";
	import { pageMesMonthSalePlan, deleteMesMonthSalePlan } from '/@/api/simple/mesMonthSalePlan';
	import ChooseMaterial from "/@/views/simple/mesMonthSalePlan/component/chooseMaterial.vue";
  	import { formatDate } from '/@/utils/formatTime';
//import { tr } from "element-plus/es/locale";
	const monthvalue=ref(new Date());
	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
		monthvalue:String,
	},
	});
	
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
    const state = reactive({
    isShowDialog: false,
    ruleForm: {} as any,
});

	//自行添加其他规则
	const rules = ref<FormRules>({
		partCode: [{required: true, message: '请选择产品ERP编码！', trigger: 'blur',},],
		month: [{required: false, message: '月份不可以为空！', trigger: 'blur',},],
		commissioningNumber: [{required: true, message: '可投产数量不可以为空！', trigger: 'blur',},],
		monthDate: [{required: true, message: '可生成生产日期计划不可以为空！', trigger: 'blur',},],
	});

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

	const chooseMaterialRef = ref();
    const openChoose = ()=>{
      chooseMaterialRef.value.openDialog({})
    }
    const onChooseMaterialFinish = (row)=>{
      state.ruleForm.partName = row?.materialName;
      state.ruleForm.partCode = row?.materialCode;
	  state.ruleForm.partModel = row?.specs;
	  state.ruleForm.partId = row?.id;
      // state.ruleForm.productNo = row?.materialNo;
    }

	// 打开弹窗
	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 detailMesMonthSalePlan(rowData.id)).data.result;
			monthvalue.value=new Date(state.ruleForm.month+"-01");
		}
		else
		{
			state.ruleForm = rowData;
			monthvalue.value=new Date(rowData.monthvalue+"-01");
			//state.ruleForm.isOrderPlan=true;
		}
			
		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 (isValid==true) {
				let values = state.ruleForm;
				const data=new Date();
				var date=data.getFullYear()+"-"+('0' + (data.getMonth() + 1)).slice(-2)+"-1";
				if(monthvalue.value<new Date(date))
			    {
			    	return ElMessage({
                     message: `不可以创建或修改当前月之前的月销售计划！`,
                     type: "error",
                    });
			    }
				var monthvalues=monthvalue.value.getFullYear()+"-"+('0' + (monthvalue.value.getMonth() + 1)).slice(-2);
				state.ruleForm.month=monthvalues;
				if(state.ruleForm.partCode==undefined || state.ruleForm.partCode=="" ||state.ruleForm.partCode==null)
	            {
	            	return ElMessage({
                     message: `请选择产品ERP编码！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.month==undefined)
	            {
	            	return ElMessage({
                     message: `请选择月份！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.month==null)
	            {
	            	return ElMessage({
                     message: `请选择月份！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.month=="")
	            {
	            	return ElMessage({
                     message: `请选择月份！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.commissioningNumber==undefined || state.ruleForm.commissioningNumber=="" ||state.ruleForm.commissioningNumber==null)
	            {
	            	return ElMessage({
                     message: `请输入可投产数！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.commissioningNumber<=0)
	            {
	            	return ElMessage({
                     message: `可投产数不可以小于等于0！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.monthDate==undefined || state.ruleForm.monthDate=="" ||state.ruleForm.monthDate==null)
	            {
	            	return ElMessage({
                     message: `请选择可生成生产计划日期！`,
                     type: "error",
                });
	            }
				state.ruleForm.monthDate=new Date(state.ruleForm.monthDate);
				state.ruleForm.monthDate=state.ruleForm.monthDate.getFullYear()+"-"+('0' + (state.ruleForm.monthDate.getMonth() + 1)).slice(-2)+"-"+state.ruleForm.monthDate.getDate().toString().padStart(2, '0');

			     if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
			     	var result=(await addMesMonthSalePlan(values)).data.result;
			     	if(result=="添加成功")
			     	 closeDialog();
			     	else
			     	{
			     		ElMessage({
			     	message: `${'添加失败'}`,
			     	type: "error",
			     });
			     	}
			     } else {
			     	await updateMesMonthSalePlan(values);
			     	closeDialog();
			     }
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






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




