<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-container v-loading="loading">
			<el-main style="padding: 0 20px 20px 20px">
				<el-form
					ref="dialogForm"
					:model="form"
					:rules="rules"
					label-width="100px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="物料" prop="material">
								<el-select
									v-model="form.material"
									placeholder="物料"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in options"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="所需数量" prop="place">
								<el-input-number
									v-model="form.count"
									:precision="0"
									:min="0"
									controls-position="right"
									placeholder="所需数量"
									style="width: 100%"
									@change="handleChange"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="物料单价" prop="unit_price">
								<el-input-number
									v-model="form.unit_price"
									:precision="2"
									:min="0"
									controls-position="right"
									placeholder="物料单价"
									style="width: 100%"
									@change="handleChange"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="物料总价">
								<el-input-number
									v-model="form.total_price"
									:precision="2"
									:min="0"
									controls-position="right"
									placeholder="物料总价"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit"
					>保存</el-button
				>
				<el-button @click="visible = false">取消</el-button>
			</el-footer>
		</el-container>
	</el-dialog>
</template>
<script>
export default {
	emits: ["success", "closed"],
	props: {
		puOrder: { type: String, default: "" },
	},
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增订单详情",
				edit: "编辑订单详情",
				show: "查看订单详情",
			},
			form: {},
			rules: {
				material: [
					{ required: true, message: "请选择物料", trigger: "blur" },
				],
				unit_price: [
					{
						required: true,
						message: "请输入物料单价",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
			options: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getList();
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		getList() {
			this.$API.mtm.material.list.req({ page: 0 }).then((res) => {
				this.options = res;
			});
		},
		handleChange() {
			if (
				this.form.unit_price !== 0 &&
				this.form.unit_price !== "" &&
				this.form.unit_price !== null
			) {
				if (
					this.form.count !== 0 &&
					this.form.count !== "" &&
					this.form.count !== null
				) {
					this.form.total_price =
						Number(this.form.count) * Number(this.form.unit_price);
				}
			} else {
				this.form.total_price = "";
			}
		},
		//提交
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					try {
						var res;
						this.form.pu_order = this.puOrder;
						if (this.mode == "add") {
							res = await this.$API.pum.orderitem.create.req(
								this.form
							);
						} else if (this.mode == "edit") {
							res = await this.$API.pum.orderitem.update.req(
								this.form.id,
								this.form
							);
						}
						this.isSaveing = false;
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					} catch (err) {
						//可以处理校验错误
						this.isSaveing = false;
						return err;
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
