<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="120px">
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="合同名称" prop="name">
								<el-input v-model="form.name" placeholder="合同名称" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="合同编号" prop="number">
								<el-input v-model="form.number" placeholder="合同编号" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="合同金额" prop="contract_amount">
								<el-input-number
									v-model="form.contract_amount"
									:precision="2"
									:min="0"
									controls-position="right"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="签订日期" prop="sign_date">
								<el-date-picker
									v-model="form.sign_date"
									type="date"
									placeholder="签订日期"
									value-format="YYYY-MM-DD"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="生效日期">
								<el-date-picker
									v-model="form.effective_date"
									type="date"
									placeholder="生效日期"
									value-format="YYYY-MM-DD"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="截止日期">
								<el-date-picker
									v-model="form.end_date"
									type="date"
									placeholder="截止日期"
									value-format="YYYY-MM-DD"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="供应商" prop="supplier">
								<el-select v-model="form.supplier" placeholder="供应商" clearable style="width: 100%">
									<el-option
										v-for="item in supplierOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									/>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="合同状态" prop="status">
								<el-select v-model="form.status" placeholder="合同状态" clearable style="width: 100%">
									<el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="24" :sm="24">
							<el-form-item label="描述">
								<el-input v-model="form.description" type="textarea" :rows="3" placeholder="描述" />
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaving" @click="submit">保存</el-button>
				<el-button @click="visible = false">取消</el-button>
			</el-footer>
		</el-container>
	</el-dialog>
</template>

<script>
export default {
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			visible: false,
			isSaving: false,
			form: {
				status: 10,
			},
			titleMap: {
				add: "新增采购合同",
				edit: "编辑采购合同",
				show: "查看采购合同",
			},
			supplierOptions: [],
			statusOptions: [
				{ value: 10, label: "草稿" },
				{ value: 20, label: "执行中" },
				{ value: 30, label: "已完成" },
				{ value: 40, label: "已终止" },
			],
			rules: {
				name: [{ required: true, message: "请输入合同名称", trigger: "blur" }],
				number: [{ required: true, message: "请输入合同编号", trigger: "blur" }],
				supplier: [{ required: true, message: "请选择供应商", trigger: "change" }],
				sign_date: [{ required: true, message: "请选择签订日期", trigger: "change" }],
			},
		};
	},
	mounted() {
		this.getSuppliers();
	},
	methods: {
		getSuppliers() {
			this.$API.pum.supplier.list.req({ page: 0 }).then((res) => {
				this.supplierOptions = res;
			});
		},
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			if (mode === "add") {
				this.form = {
					status: 10,
				};
			}
			return this;
		},
		setData(data) {
			this.form = Object.assign({}, data);
			return this;
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (!valid) {
					return;
				}
				this.isSaving = true;
				try {
					if (this.mode === "add") {
						await this.$API.pum.contract.create.req(this.form);
					} else {
						await this.$API.pum.contract.update.req(this.form.id, this.form);
					}
					this.$message.success("操作成功");
					this.$emit("success", this.form, this.mode);
					this.visible = false;
				} finally {
					this.isSaving = false;
				}
			});
		},
	},
};
</script>
