<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		width="700"
		destroy-on-close
		:close-on-click-modal="false"
		@closed="$emit('closed')"
	>
		<el-main>
			<el-form
				ref="dialogForm"
				:model="form"
				:rules="rules"
				label-width="120px"
			>
				<el-form-item label="会员等级" prop="level_id">
					<sc-select
						v-model="form.level_id"
						placeholder="请选择会员等级"
						:apiObj="$API.member.level.list"
						:params="{ status: 1 }"
						:props="{ label: 'title', value: 'id' }"
						clearable
						style="width: 240px"
					/>
				</el-form-item>
				<el-form-item label="到期时间" prop="level_expire_time">
					<el-date-picker
						v-model="form.level_expire_time"
						type="date"
						placeholder="选择到期时间"
						format="YYYY-MM-DD"
						value-format="YYYY-MM-DD"
						style="width: 240px"
					/>
				</el-form-item>
				<el-form-item label="审核状态" prop="status">
					<el-select
						v-model="form.status"
						placeholder="请选择审核状态"
						clearable
						style="width: 240px"
					>
						<el-option
							v-for="(item, index) in [
								{ label: '通过', value: 1 },
								{ label: '拒绝', value: 2 },
								{ label: '未审核', value: 0 },
							]"
							:key="index"
							:label="item.label"
							:value="item.value"
						/>
					</el-select>
				</el-form-item>
			</el-form>
		</el-main>
		<template #footer>
			<el-button type="primary" @click="onSubmit">确认提交</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ["success", "closed"],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
				audit: "审核",
			},
			visible: false,
			form: {},
			rules: {},
		};
	},
	mounted() {},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		onSubmit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let res;
					if (this.mode == "edit") {
						res = await this.$API.member.extend.edit.post(
							this.form,
						);
					} else if (this.mode == "audit") {
						res = await this.$API.member.extend.audit.post(
							this.form,
						);
					} else {
						res = await this.$API.member.extend.add.post(this.form);
					}

					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit("success", res.data);
						this.visible = false;
						this.$message.success("操作成功");
					} else {
						this.$message.error(res.message);
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			this.form = data;
			this.form.level_id = data.member?.level_id;
			this.form.level_expire_time = data.member?.level_expire_time;
		},
	},
};
</script>

<style scoped></style>
