<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="460px" @close="close"
			@keyup.enter.native="doSubmit" v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading" :rules="rules" label-width="80px"
				@submit.native.prevent>
				<el-form-item label="选项名称" prop="optionName">
					<el-input v-model="inputForm.optionName" placeholder="如：三分熟、黑椒汁" clearable></el-input>
				</el-form-item>
				<el-form-item label="加价金额" prop="priceExtra">
					<el-input-number v-model="inputForm.priceExtra" :min="0" :max="9999" :precision="2"
						style="width: 100%"></el-input-number>
				</el-form-item>
				<el-form-item label="排序号" prop="sortOrder">
					<el-input-number v-model="inputForm.sortOrder" :min="0" :max="999" style="width: 100%"></el-input-number>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button type="primary" @click="doSubmit()" icon="el-icon-circle-check"
						v-noMoreClick>确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import HotelDishSpecService from '@/api/hotel/HotelDishSpecService'

export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			groupId: '',
			rules: {
				optionName: [{ required: true, message: '请输入选项名称', trigger: 'blur' }]
			},
			inputForm: {
				optionName: '',
				priceExtra: 0,
				sortOrder: 0
			}
		}
	},
	hotelDishSpecService: null,
	created() {
		this.hotelDishSpecService = new HotelDishSpecService()
	},
	methods: {
		init(method, id, groupId) {
			this.method = method
			this.groupId = groupId
			this.inputForm = {
				optionName: '',
				priceExtra: 0,
				sortOrder: 0
			}
			if (method === 'add') {
				this.title = '新增规格选项'
				this.inputForm.groupId = groupId
			} else if (method === 'edit') {
				this.title = '编辑规格选项'
				this.inputForm.id = id
			}
			this.visible = true
			this.loading = false
			this.$nextTick(() => {
				this.$refs.inputForm.resetFields()
				if (method === 'edit') {
					this.loading = true
					this.hotelDishSpecService.queryOptionById(id).then((data) => {
						this.inputForm = {
							id: data.id,
							groupId: data.groupId,
							optionName: data.optionName,
							priceExtra: data.priceExtra || 0,
							sortOrder: data.sortOrder || 0
						}
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		doSubmit() {
			this.$refs.inputForm.validate((valid) => {
				if (valid) {
					this.loading = true
					this.hotelDishSpecService.saveOption(this.inputForm).then(() => {
						this.$message.success('操作成功')
						this.close()
						this.$emit('refreshOptions')
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.inputForm && this.$refs.inputForm.resetFields()
			this.visible = false
		}
	}
}
</script>
