<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="650px" @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="roomNo">
							<el-input v-model="inputForm.roomNo" placeholder="不填写则自动生成"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="包房名称" prop="roomName"
							:rules="[{ required: true, message: '包房名称不能为空', trigger: 'blur' }]">
							<el-input v-model="inputForm.roomName" placeholder="请填写包房名称"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="容纳人数" prop="capacity">
							<el-input-number v-model="inputForm.capacity" :min="0" :precision="0"
								style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="最低消费" prop="minConsumption">
							<el-input-number v-model="inputForm.minConsumption" :min="0" :precision="2"
								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="sort">
							<el-input-number v-model="inputForm.sort" :min="0" style="width: 100%"></el-input-number>
						</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="0">启用</el-radio>
								<el-radio label="1">停用</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="remarks">
							<el-input v-model="inputForm.remarks" type="textarea" :rows="4" 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 DishRoomService from '@/api/psi/DishRoomService'
export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: {
				roomNo: '',
				roomName: '',
				capacity: 0,
				minConsumption: 0,
				status: '0',
				sort: 0,
				remarks: ''
			}
		}
	},
	dishRoomService: null,
	created() {
		this.dishRoomService = new DishRoomService()
	},
	methods: {
		init(method, id) {
			this.method = method
			this.inputForm = {
				roomNo: '',
				roomName: '',
				capacity: 0,
				minConsumption: 0,
				status: '0',
				sort: 0,
				remarks: ''
			}
			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.$nextTick(() => {
				if (method === 'edit' || method === 'view') {
					this.loading = true
					this.$refs.inputForm.resetFields()
					this.dishRoomService.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.dishRoomService.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>
