﻿<template>
	<div class="mesMonthProducePlan-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="orderCode">
							<el-input v-model="state.ruleForm.orderCode" 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="销售计划号" prop="saleCode">
                            <el-input v-model="state.ruleForm.saleCode" placeholder="请选择销售计划号" disabled="true" maxlength="100" show-word-limit clearable >
                               <template #append>
                               <el-button @click="openChooseApplyType">选择</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="产品ERP编码" prop="partCode">
                            <el-input v-model="state.ruleForm.partCode" placeholder="请输入产品ERP编码" readonly="true" maxlength="100" show-word-limit clearable >
                               <!-- <template #append>
                               <el-button @click="openChooseApplyType">选择</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="产品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="产品ERPCode" prop="partCode">
                            <el-input v-model="state.ruleForm.partCode" placeholder="请选择产品ERPCode" disabled="true" maxlength="100" show-word-limit clearable >
                        <template #append>
                            <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" 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="产品规格型号" prop="partModel">
							<el-input v-model="state.ruleForm.partModel" placeholder="请输入产品规格型号" readonly="true" maxlength="255" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="props.title=='编辑月生产计划'">
						<el-form-item label="可下计划数量" prop="isPlanNumber">
							<el-input-number v-model="state.ruleForm.isPlanNumber" readonly="true" 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="计划数量" prop="planNumber">
							<el-input-number v-model="state.ruleForm.planNumber" 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="月份" prop="month">
							<el-date-picker v-model="monthvalue" type="month"  placeholder="请输入月份" readonly="true"  :default-value="new Date(2024,10)"  show-word-limit clearable />
							<!-- maxlength="32" 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="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"  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>
		<!-- <choose-material title="选择产品" ref="chooseMaterialRef" :is-any="false" @finish="onChooseMaterialFinish"></choose-material> -->
		<ChooseMaterialApplyType title="选择销售" ref="chooseMaterialRefApplyType" :is-any="false" @finish="onChooseMaterialFinishApplyType"></ChooseMaterialApplyType>
	</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 { addMesMonthProducePlan, updateMesMonthProducePlan, detailMesMonthProducePlan } from "/@/api/simple/mesMonthProducePlan";
	import { pageMesMonthProducePlan, deleteMesMonthProducePlan } from '/@/api/simple/mesMonthProducePlan';
	import ChooseMaterial from "/@/businessComponents/chooseMaterial.vue";
	import ChooseMaterialApplyType from "/@/views/simple/mesMonthProducePlan/component/chooseSalePlan.vue";
  	import { formatDate } from '/@/utils/formatTime';
    import ChooseSalePlan from "./chooseSalePlan.vue";
    import { result } from "lodash-es";
	const monthvalue=ref(new Date());
	//父级传递来的参数
	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>({
		//partCode: [{required: true, message: '请选择产品ERP编码！', trigger: 'blur',},],
		//monthvalue: [{required: true, message: '月份不可以为空！', trigger: 'blur',},],
		planNumber: [{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 complentNum=ref();//用来存单独接受的完工数
	const chooseMaterialRefApplyType = ref();
    const openChooseApplyType = ()=>{
		var monthvalues=monthvalue.value.getFullYear()+"-"+('0' + (monthvalue.value.getMonth() + 1)).slice(-2);
	    state.ruleForm.month=monthvalues;
		if(!state.ruleForm.month) return ElMessage.warning('请选择月份');
		chooseMaterialRefApplyType.value.openDialog({
			month:state.ruleForm.month
		})
    }
    const onChooseMaterialFinishApplyType = (row)=>{
      
      state.ruleForm.partCode = row?.partCode;
	  state.ruleForm.partName = row?.partName;
      state.ruleForm.partModel = row?.partModel;	  
	  state.ruleForm.planNumber = row?.compleNumber;
	  complentNum.value=row?.compleNumber;
    }


	// 打开弹窗
	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 detailMesMonthProducePlan(rowData.id)).data.result;
			monthvalue.value=new Date(state.ruleForm.month+"-01");
			state.ruleForm.isPlanNumber=rowData.isPlanNumber;
		}
		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 (isValid) {
				let values = state.ruleForm;
				if(state.ruleForm.planNumber==undefined)
	            {
	            	return ElMessage({
                     message: `数量不可以为空！`,
                     type: "error",
                });
	            }
				if(state.ruleForm.planNumber<=0)
	            {
	            	return ElMessage({
                     message: `数量不可以为小于等于0！`,
                     type: "error",
                });
	            }
				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.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
					if(parseFloat(complentNum.value)*1.1<state.ruleForm.planNumber){
						ElMessage({
					        message: `计划数量不可以超过产品预投未完工数上限10%`,
					        type: "error",
			               	});
					}
					else{
						await addMesMonthProducePlan(values);
						closeDialog();
					}
				} else {
					var result=(await updateMesMonthProducePlan(values)).data.result;
					if(result=="修改成功")
					 closeDialog();
					else
					{
						ElMessage({
					message: `${result}`,
					type: "error",
				});
					}
				}
				
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






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




