<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="900px" @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="110px" @submit.native.prevent>
				<el-row :gutter="20">
					<el-col :span="12">
						<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="12">
						<el-form-item label="分类" prop="categoryId"
							:rules="[{ required: true, message: '请选择分类', trigger: 'change' }]">
							<el-select v-model="inputForm.categoryId" placeholder="请选择分类" style="width: 100%">
								<el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="价格（元）" prop="price"
							:rules="[{ required: true, message: '价格不能为空', trigger: 'blur' }]">
							<el-input-number v-model="inputForm.price" :min="0" :precision="2"
								style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="制作时长（分钟）" prop="prepTime">
							<el-input-number v-model="inputForm.prepTime" :min="0" style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="标签" prop="tags">
							<el-input v-model="inputForm.tags" placeholder="多个标签用逗号分隔，如：辣,推荐,新品"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="排序号" prop="sortOrder">
							<el-input-number v-model="inputForm.sortOrder" :min="0" style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="主厨推荐" prop="isRecommended">
							<el-switch v-model="inputForm.isRecommended" :active-value="1" :inactive-value="0"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="状态" prop="status">
							<el-radio-group v-model="inputForm.status">
								<el-radio label="ON_SALE">在售</el-radio>
								<el-radio label="OFF_SHELF">下架</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="描述" prop="description">
							<el-input v-model="inputForm.description" type="textarea" :rows="3"
								placeholder="请输入菜品描述"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="菜品图片" prop="image">
							<DishImageUpload ref="dishImageUpload" v-model="inputForm.image" :auth="method"
								directory="hotel-dish" :max-size="10" :limit="1">
							</DishImageUpload>
						</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 HotelDishService from '@/api/hotel/HotelDishService'
import DishImageUpload from './DishImageUpload'
export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			categoryList: [],
			inputForm: {
				id: '',
				name: '',
				categoryId: '',
				price: 0,
				tags: '',
				prepTime: null,
				image: '',
				description: '',
				isRecommended: 0,
				sortOrder: 0,
				status: 'ON_SALE'
			}
		}
	},
	hotelDishService: null,
	created() {
		this.hotelDishService = new HotelDishService()
	},
	components: {
		DishImageUpload
	},
	methods: {
		init(method, id) {
			this.method = method
			this.inputForm = {
				id: '',
				name: '',
				categoryId: '',
				price: 0,
				tags: '',
				prepTime: null,
				image: '',
				description: '',
				isRecommended: 0,
				sortOrder: 0,
				status: 'ON_SALE'
			}
			if (method === 'add') {
				this.title = '新建菜品'
			} 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.loadCategories()
			this.$nextTick(() => {
				if (method === 'edit' || method === 'view') {
					this.loading = true
					this.$refs.inputForm.resetFields()
					this.hotelDishService.queryById(this.inputForm.id).then((data) => {
						const dish = data || {}
						this.inputForm = { ...this.inputForm, ...dish }
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		loadCategories() {
			this.hotelDishService.categoryAll().then((data) => {
				this.categoryList = Array.isArray(data) ? data : (data.list || [])
			})
		},
		doSubmit() {
			this.$refs.inputForm.validate((valid) => {
				if (valid) {
					if (this.$refs.dishImageUpload && this.$refs.dishImageUpload.checkProgress()) {
						return
					}
					this.loading = true
					this.hotelDishService.save(this.inputForm).then((data) => {
						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.$refs.dishImageUpload && this.$refs.dishImageUpload.clearUpload()
			this.visible = false
		}
	}
}
</script>
