<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="name">
					<el-input v-model="inputForm.name" placeholder="请输入类型名称" clearable></el-input>
				</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 HotelRoomTypeService from '@/api/hotel/HotelRoomTypeService'
export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			rules: {
				name: [{ required: true, message: '类型名称不能为空', trigger: 'blur' }]
			},
			inputForm: {
				name: '',
				sortOrder: 0
			}
		}
	},
	hotelRoomTypeService: null,
	created() {
		this.hotelRoomTypeService = new HotelRoomTypeService()
	},
	methods: {
		init(method, id) {
			this.method = method
			this.inputForm = {
				name: '',
				sortOrder: 0
			}
			if (method === 'add') {
				this.title = '新增房间类型'
			} 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.hotelRoomTypeService.queryById(id).then((data) => {
						this.inputForm = {
							id: data.id,
							name: data.name,
							sortOrder: data.sortOrder || 0
						}
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		doSubmit() {
			this.$refs.inputForm.validate((valid) => {
				if (valid) {
					this.loading = true
					this.hotelRoomTypeService.save(this.inputForm).then(() => {
						this.$message.success('操作成功')
						this.close()
						this.$emit('refreshList')
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.inputForm && this.$refs.inputForm.resetFields()
			this.visible = false
		}
	}
}
</script>
