﻿<template>
	<div class="mesequipmentpartstock-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="warehouseCode">
              <el-input v-model="state.ruleForm.warehouseCode" readonly="true" placeholder="请选择仓库" maxlength="100" show-word-limit clearable >
                <template #append v-if="props.title=='编辑备件库存'==false">
                  <el-button @click="openChooseStock">选择</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="warehouseName">
							<el-input v-model="state.ruleForm.warehouseName" 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="partCode">
              <el-input v-model="state.ruleForm.partCode" readonly="true"  placeholder="请选择备件" 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"  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="partBarCode">
							<el-input v-model="state.ruleForm.partBarCode" placeholder="请输入备件批次" maxlength="150" 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="stockNum">
							<el-input-number v-model="state.ruleForm.stockNum" min="0" placeholder="请输入在库数量" :precision="0" 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="createOn">
							<el-date-picker v-model="state.ruleForm.createOn" type="datetime" placeholder="创建时间" />
							
						</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>
		<choose-material-stock title="选择仓库" ref="chooseMaterialRefStock" :is-any="false" @finish="onChooseMaterialFinishStock"></choose-material-stock>
	</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 { addMesequipmentpartstock, updateMesequipmentpartstock, detailMesequipmentpartstock } from "/@/api/simple/mesequipmentpartstock";
	import { pageMesequipmentpartstock, deleteMesequipmentpartstock } from '/@/api/simple/mesequipmentpartstock';
    //import ChooseMaterial from "/@/businessComponents/chooseMaterial.vue";
	import ChooseMaterial from "/@/views/simple/mesequipmentpartstock/component/chooseMaterial.vue";
	import ChooseMaterialStock from "/@/businessComponents/chooseStock.vue";
  	import { formatDate } from '/@/utils/formatTime';
	import { scanlist } from '/@/api/simple/dmsstockinfo';

	//父级传递来的参数
	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>({
		//remark: [{required: true, message: '请输入描述！', trigger: 'blur',},],
		 partCode: [{required: true, message: '请选择备件！', trigger: 'blur',},],
		 warehouseCode: [{required: true, message: '请选择仓库！', trigger: 'blur',},],
		 partBarCode: [{required: true, message: '请输入备件批次！', trigger: 'blur',},],
		 stockNum: [{required: true, message: '请输入在库数！', trigger: 'blur',},],
		 createOn: [{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.partId = row?.id;
      // state.ruleForm.productNo = row?.materialNo;
    }

	const chooseMaterialRefStock = ref();
    const openChooseStock = ()=>{
		chooseMaterialRefStock.value.openDialog({})
    }
    const onChooseMaterialFinishStock = (row)=>{
      state.ruleForm.warehouseCode = row?.code;
      state.ruleForm.warehouseName = row?.name;
	  state.ruleForm.warehouseId = 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 detailMesequipmentpartstock(rowData.id)).data.result;
		else
		{
			state.ruleForm = rowData;
			let  res = await scanlist();
            let datas = res.data.result || {};
			console.log(datas);
			if(datas.code!=undefined)
			{
				state.ruleForm.warehouseId=datas.id;
				state.ruleForm.warehouseCode=datas.code;
				state.ruleForm.warehouseName=datas.name;
			}
	    }
		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.partCode==undefined)
	        {
	    	  return ElMessage({
              message: `请选择备件！`,
              type: "error",
            });
	        }
			if(state.ruleForm.warehouseCode==undefined)
	        {
	    	  return ElMessage({
              message: `请选择仓库！`,
              type: "error",
            });
	        }
			if(state.ruleForm.partBarCode==undefined)
	        {
	    	  return ElMessage({
              message: `请输入备件批次！`,
              type: "error",
            });
	        }
			if(state.ruleForm.stockNum==undefined || state.ruleForm.stockNum<=0)
	        {
	    	  return ElMessage({
              message: `在库数不可以是0或者负数！`,
              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 addMesequipmentpartstock(values);
				} else {
					await updateMesequipmentpartstock(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






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




