<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-position="right"
					label-width="100px"
					style="padding: 0 10px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="名称" prop="name">
								<el-input
									v-model="form.name"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="字段类型">
								<el-select
									v-model="form.field_type"
									placeholder="字段类型"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in options"
										:key="item.value"
										:label="item.name"
										:value="item.value"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="编号">
								<el-input
									v-model="form.number"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="描述">
								<el-input
									v-model="form.description"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="检验类型">
								<el-checkbox-group v-model="form.tags">
									<el-checkbox 
										v-for="item in  tagsOptions"
										:key="item.value"
										:label="item.name"
										:value="item.value"/>
								</el-checkbox-group>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="排序">
								<el-input-number
									v-model="form.sort"
									clearable
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :span="24" v-if="mcateTagsOptions.length>0">
							<el-form-item label="物料系列标签">
								<el-checkbox-group v-model="form.mcate_tags">
									<el-checkbox 
										v-for="item in  mcateTagsOptions"
										:key="item"
										:label="item"
										:value="item"/>
								</el-checkbox-group>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row v-if="form.field_type == 'select'||form.field_type == 'selects'">
						<el-col :span="24">
							<el-form-item label="选项">
								<el-button icon="el-icon-circle-plus" text @click="addComponent('choices')">
									添加
								</el-button>
							</el-form-item>
						</el-col>
						<template
							v-for="(item, $index) in form.choices"
							:key="$index"
						>
							<el-col :md="7" :sm="10">
								<el-form-item label-width="80px">
									<el-input
										v-model="form.choices[$index]"
										clearable
									></el-input>
								</el-form-item>
							</el-col>
							<el-col :md="1" :sm="2">
								<div style="text-align: center">
									<el-button type="danger" icon="el-icon-remove" text @click="delComponent($index,'choices')"></el-button>
								</div>
							</el-col>
						</template>
					</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>
const defaultForm = {
	name: "",
	description: "",
	sort: 1,
	tags: [],
	mcate_tags: [],
	choices: [""],
};
export default {
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
				show: "查看",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				name: [
					{ required: true, message: "请输入名称", trigger: "blur" },
				],
			},
			visible: false,
			isSaveing: false,
			options: [
				{value:"input-int",name:"整数"},
				{value:"input-number",name:"小数"},
				{value:"input-text",name:"文本"},
				{value:"select",name:"单选"},
				{value:"selects",name:"多选"},
			],
			tagsOptions: [
				{value:"first",name:"首件检验"},
				{value:"prod",name:"成品检验"},
				{value:"performance",name:"性能检验"},
			],
			mcateTagsOptions: [],
			processOptions: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getmcateTagsOptions();
	},
	methods: {
		getmcateTagsOptions() {
			this.$API.mtm.material.cates.req().then((res) => {
				this.mcateTagsOptions = res;
			});
		},
		addComponent(type) {
			this.form[type].push("");
		},
		delComponent(index,type) {
			this.form[type].splice(index, 1);
		},
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					if (that.mode === "add") {
						that.$API.qm.testitem.create
							.req(that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success");
								that.visible = false;
							})
							.catch((res) => {
								that.isSaveing = false;
							});
					} else {
						res = that.$API.qm.testitem.update
							.req(that.form.id, that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success");
								that.visible = false;
							})
							.catch((res) => {
								that.isSaveing = false;
							});
					}
				}
			});
		},

		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
