<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="560px" @close="close"
			@keyup.enter.native="doSubmit" v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading"
				:class="method === 'view' ? 'readonly' : ''" :disabled="method === 'view'" label-width="100px"
				@submit.native.prevent>
				<el-row :gutter="15">
					<el-col :span="22">
						<el-form-item label="上级分类" prop="parentId">
							<SelectTree ref="dishTypeTree" v-if="visible" :props="{
								value: 'id',
								label: 'name',
								children: 'children'
							}" url="/psi-management-server/psi/dishType/treeData" :value="inputForm.parentId"
								:clearable="true" :accordion="true" size="default"
								@getValue="(value) => { inputForm.parentId = value }" />
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<el-form-item label="分类名称" prop="name"
							:rules="[{ required: true, message: '分类名称不能为空', trigger: 'blur' }]">
							<el-input v-model="inputForm.name" placeholder="请填写分类名称"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<el-form-item label="排序" prop="sort">
							<el-input-number v-model="inputForm.sort" :min="0" style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<el-form-item label="状态" prop="status">
							<el-radio-group v-model="inputForm.status">
								<el-radio label="0">启用</el-radio>
								<el-radio label="1">停用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<el-form-item label="备注" prop="remarks">
							<el-input v-model="inputForm.remarks" type="textarea" :rows="3" maxlength="500"
								placeholder="请输入备注" show-word-limit></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button type="primary" v-if="method !== 'view'" @click="doSubmit()"
						icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import DishTypeService from '@/api/psi/DishTypeService'
import SelectTree from '@/components/treeSelect/treeSelect.vue'
export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: {
				name: '',
				parentId: '',
				sort: 0,
				status: '0',
				remarks: ''
			}
		}
	},
	dishTypeService: null,
	created() {
		this.dishTypeService = new DishTypeService()
	},
	components: {
		SelectTree
	},
	methods: {
		init(method, id) {
			this.method = method
			this.inputForm = {
				name: '',
				parentId: '',
				sort: 0,
				status: '0',
				remarks: ''
			}
			if (method === 'add') {
				this.title = '新建菜品分类'
			} else if (method === 'addChild') {
				this.title = '添加下级分类'
				this.inputForm.parentId = id
			} else if (method === 'edit') {
				this.title = '修改菜品分类'
				this.inputForm.id = id
			} else if (method === 'view') {
				this.title = '查看菜品分类'
				this.inputForm.id = id
			}
			this.visible = true
			this.loading = false
			this.$nextTick(() => {
				if (method === 'edit' || method === 'view') {
					this.loading = true
					this.$refs.inputForm.resetFields()
					this.dishTypeService.findById(this.inputForm.id).then((data) => {
						this.inputForm = this.recover(this.inputForm, data)
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		doSubmit() {
			this.$refs.inputForm.validate((valid) => {
				if (valid) {
					this.loading = true
					this.dishTypeService.save(this.inputForm).then((data) => {
						if (data && data.indexOf('重复') > -1) {
							this.$message.error(data)
						} else {
							this.$message.success(data)
							this.close()
							this.$emit('refreshList')
						}
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.inputForm && this.$refs.inputForm.resetFields()
			this.visible = false
		}
	}
}
</script>
