<template>
	<el-dialog title="修改价格" :close-on-click-modal="false" width="420px" v-model="visible">
		<el-form ref="priceForm" :model="form" label-width="100px" @submit.native.prevent>
			<el-form-item label="菜品编码">
				<el-input v-model="form.dishCode" disabled></el-input>
			</el-form-item>
			<el-form-item label="菜品名称">
				<el-input v-model="form.dishName" disabled></el-input>
			</el-form-item>
			<el-form-item label="菜品库价格">
				<el-input v-model="form.librarySalePrice" disabled></el-input>
			</el-form-item>
			<el-form-item label="菜单价格" prop="salePrice">
				<el-input-number v-model="form.salePrice" :min="0" :precision="2" :controls="false"
					style="width: 100%"></el-input-number>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false" icon="el-icon-circle-close">取消</el-button>
				<el-button type="primary" :loading="submitting" @click="savePrice()"
					icon="el-icon-circle-check">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script>
import DishMenuService from '@/api/psi/DishMenuService'

export default {
	emits: ['refreshList'],
	data() {
		return {
			visible: false,
			submitting: false,
			form: {
				id: '',
				dishCode: '',
				dishName: '',
				librarySalePrice: '',
				salePrice: 0
			}
		}
	},
	dishMenuService: null,
	created() {
		this.dishMenuService = new DishMenuService()
	},
	methods: {
		init(row) {
			this.form = {
				id: row.id,
				dishCode: row.dishCode,
				dishName: row.dishName,
				librarySalePrice: row.librarySalePrice,
				salePrice: row.salePrice === null || row.salePrice === undefined || row.salePrice === '' ? 0 : Number(row.salePrice)
			}
			this.visible = true
		},
		savePrice() {
			if (!this.form.id) {
				this.$message.warning('请选择要修改的菜品')
				return
			}
			if (this.form.salePrice === null || this.form.salePrice === undefined || this.form.salePrice === '') {
				this.$message.warning('请输入菜单价格')
				return
			}
			this.submitting = true
			this.dishMenuService.updatePrice({
				id: this.form.id,
				salePrice: this.form.salePrice
			}).then((data) => {
				this.$message.success(data)
				this.visible = false
				this.$emit('refreshList')
			}).finally(() => {
				this.submitting = false
			})
		}
	}
}
</script>
