<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="560px" @close="close"
			@keyup.enter.native="doSubmit" v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading" :rules="rules" label-width="100px"
				@submit.native.prevent>
				<el-row :gutter="15">
					<el-col :span="22">
						<el-form-item label="房间号" prop="roomNo">
							<el-input v-model="inputForm.roomNo" placeholder="请输入房间号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<el-form-item label="房间类型" prop="roomTypeId">
							<el-select v-model="inputForm.roomTypeId" placeholder="请选择房间类型" clearable style="width: 140px">
								<el-option v-for="item in roomTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<el-form-item label="楼层" prop="floor">
							<el-input-number v-model="inputForm.floor" :min="1" :max="99" style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="22">
						<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 label="2">停用</el-radio>
							</el-radio-group>
						</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" @click="doSubmit()" icon="el-icon-circle-check"
						v-noMoreClick>确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import HotelRoomService from '@/api/hotel/HotelRoomService'
import HotelRoomTypeService from '@/api/hotel/HotelRoomTypeService'
export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			rules: {
				roomNo: [{ required: true, message: '房间号不能为空', trigger: 'blur' }]
			},
			roomTypeList: [],
			inputForm: {
				roomNo: '',
				roomTypeId: '',
				floor: 1,
				status: '0'
			}
		}
	},
	hotelRoomService: null,
	hotelRoomTypeService: null,
	created() {
		this.hotelRoomService = new HotelRoomService()
		this.hotelRoomTypeService = new HotelRoomTypeService()
		this.loadRoomTypes()
	},
	methods: {
		init(method, id) {
			this.method = method
			this.inputForm = {
				roomNo: '',
				roomTypeId: '',
				floor: 1,
				status: '0'
			}
			if (method === 'add') {
				this.title = '新增房间'
			} else if (method === 'edit') {
				this.title = '编辑房间'
				this.inputForm.id = id
			}
			this.visible = true
			this.loading = false
			this.loadRoomTypes()
			this.$nextTick(() => {
				this.$refs.inputForm.resetFields()
				if (method === 'edit') {
					this.loading = true
					this.hotelRoomService.queryById(id).then((data) => {
						this.inputForm = {
							id: data.id,
							roomNo: data.roomNo,
							roomTypeId: data.roomTypeId,
							floor: data.floor,
							status: data.status || '0'
						}
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		doSubmit() {
			this.$refs.inputForm.validate((valid) => {
				if (valid) {
					this.loading = true
					this.hotelRoomService.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
		},
		loadRoomTypes() {
			this.hotelRoomTypeService.all().then((data) => {
				this.roomTypeList = Array.isArray(data) ? data : (data.list || [])
			})
		}
	}
}
</script>
