<template>
<el-drawer :title="titleMap[mode]" v-model="visible" :size="1000" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')">
	<el-main>
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="160px">
			<el-form-item label="分类名称" prop="title">
				<el-input v-model="form.title" placeholder="请输入分类名称" clearable />
			</el-form-item>
			<el-form-item label="分类图标">
				<sc-upload v-model="form.cover" :max-count="1" :disabled="mode == 'view'" />
			</el-form-item>
			<el-form-item label="分类排序" prop="sort">
				<el-input v-model="form.sort" placeholder="请输入分类排序" clearable />
			</el-form-item>
			<el-form-item label="分类描述">
				<scEditor v-model="form.description" height="200px" :disabled="mode == 'view'" />
			</el-form-item>
		</el-form>
	</el-main>
	<template #footer>
		<el-button type="primary" @click="onSubmit">确认提交</el-button>
	</template>
</el-drawer>
</template>

<script>
import scEditor from '@/components/scEditor'

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {add: '新增', edit: '编辑'},
			form: {type_id: 0, operate: []},
			rules: {
				title: [{required: true, message: '请输入分类名称'}]
			},
			visible: false,
			isSaveing: false,
			cache: {}
		}
	},
	components: {
		scEditor
	},
	watch: {
		"form.operate"(value){
			this.priceTab = value[0]
		}
	},
	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.form.id){
						res = await this.$API.store.category.edit.post(this.form);
					}else{
						res = await this.$API.store.category.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.loading = true
			Object.assign(this.form, data)
		}
	}
}
</script>
