<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" draggable destroy-on-close @closed="$emit('closed')">
		<el-form class="form" :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="备件SN" prop="sn">
				<el-input type="text" v-model="form.sn" placeholder="请填写物料编码" clearable></el-input>
			</el-form-item>
			<el-form-item label="工厂PN" prop="erp_pn">
				<el-input type="text" v-model="form.erp_pn" placeholder="请填写工厂PN"  clearable></el-input>
			</el-form-item>
			<el-form-item label="收货城市" prop="receive_city">
				<el-input type="text" v-model="form.receive_city" placeholder="请填写收货城市" clearable></el-input>
			</el-form-item>
			<el-form-item label="收货日期" prop="receive_date">
				<el-date-picker class="input" type="date" v-model="form.receive_date" value-format="YYYY-MM-DD" placeholder="请选择收货日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="到货类型" prop="arrival_type">
				<el-input type="text" v-model="form.arrival_type" placeholder="请填写到货类型" clearable></el-input>
			</el-form-item>
			<el-form-item label="单号" prop="order_number">
				<el-input type="text" :disabled="mode=='edit'?true:false" v-model="form.order_number" placeholder="请填写单号" clearable></el-input>
			</el-form-item>
			<el-form-item label="库房" prop="warehouse">
				<el-input type="text" v-model="form.warehouse" placeholder="请填写库房" clearable></el-input>
			</el-form-item>
			<el-form-item label="FW" prop="fw">
				<el-input type="text" v-model="form.fw" placeholder="请填写FW" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remarks">
				<el-input type="textarea" v-model="form.remarks" :row="2" placeholder="请填写备注" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSave" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	name:"librarySave",
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增备件',
				edit: '编辑备件',
				show: '查看'
			},
			visible: false,
			isSave: false,
			//表单数据
			form: {
				id:"",
				sn: "",
				erp_pn:"",
				receive_city:"",
				receive_date:"",
				arrival_type:"",
				warehouse: "",
				fw:"",
				remarks: "",
			},
			//验证规则
			rules: {
				sn:[
					{required: true, message: 'SN不能为空'}
				],
				erp_pn: [
					{required: true, message: '工厂PN不能为空'}
				],
				receive_city: [
					{required: true, message: '收货城市不能为空'}
				],
				receive_date:[
					{required: true, message: '收货日期不能为空'}
				],
				arrival_type: [
					{required: true, message: '到货类型不能为空'}
				],
				warehouse: [
					{required: true, message: '库房不能为空'}
				],
				order_number:[
					{required: true, message: '单号不能为空'}
				],
				fw: [
					{required: true, message: 'FW不能为空'}
				],
			},

		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},

		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSave = true;
					const res = await this.$API.system.spare.add.post(this.form);
					this.isSave = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}
				}else{
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			let params = {
				id: data.id
			}
			const res = await this.$API.system.spare.info.post(params);
			if (res.code == 200) {
				// this.form = res.data;
				this.form.id= res.data.id;
				this.form.sn= res.data.sn;
				this.form.erp_pn= res.data.erp_pn;
				this.form.receive_city= res.data.receive_city;
				this.form.receive_date= res.data.receive_date;
				this.form.arrival_type= res.data.arrival_type;
				this.form.warehouse= res.data.warehouse;
				this.form.fw= res.data.fw;
				this.form.remarks= res.data.remarks;
			}
		}
	}
}
</script>

<style scoped lang="scss">
.form{
	::v-deep .input{width: 100%;}
}
</style>
