<template>
	<div class="page">
		<el-form :inline="true" v-if="searchVisible" class="query-form m-b-10" ref="searchForm" :model="searchForm"
			@keyup.enter.native="refreshList()" @submit.native.prevent>
			<el-form-item label="类型名称" prop="name">
				<el-input v-model="searchForm.name" placeholder="请输入类型名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 120px">
					<el-option label="启用" value="ENABLED"></el-option>
					<el-option label="禁用" value="DISABLED"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="refreshList()" icon="el-icon-search">查询</el-button>
				<el-button @click="resetSearch()" icon="el-icon-refresh-right">重置</el-button>
			</el-form-item>
		</el-form>

		<div class="jp-table top">
			<vxe-toolbar :refresh="{ query: refreshList }" custom>
				<template #buttons>
					<el-button v-if="hasPermission('hotel:admin:roomType:add')" type="primary" icon="el-icon-plus"
						@click="add()">新增类型</el-button>
				</template>
				<template #tools>
					<vxe-button text type="primary" :title="searchVisible ? '收起检索' : '展开检索'" icon="vxe-icon-search"
						class="tool-btn" @click="searchVisible = !searchVisible"></vxe-button>
				</template>
			</vxe-toolbar>
			<div style="height: calc(100% - 90px)">
				<vxe-table border="inner" auto-resize resizable height="auto" :loading="loading" ref="dataTable"
					show-header-overflow show-overflow highlight-hover-row :menu-config="{}" :data="dataList"
					:checkbox-config="{}">
					<vxe-column type="seq" width="60" title="序号"></vxe-column>
					<vxe-column title="类型名称" field="name" min-width="200" align="center"></vxe-column>
					<vxe-column title="排序号" field="sortOrder" width="100" align="center"></vxe-column>
					<vxe-column title="状态" field="status" width="100" align="center">
						<template #default="scope">
							<el-tag :type="scope.row.status === 'ENABLED' ? 'success' : 'info'" effect="plain">
								{{ scope.row.status === 'ENABLED' ? '启用' : '禁用' }}
							</el-tag>
						</template>
					</vxe-column>
					<vxe-column title="创建时间" field="createTime" min-width="170" align="center"></vxe-column>
					<vxe-column title="操作" width="200" fixed="right" align="center">
						<template #default="scope">
							<el-button v-if="hasPermission('hotel:admin:roomType:edit')" text type="primary" size="small"
								@click="edit(scope.row.id)">编辑</el-button>
							<el-button v-if="hasPermission('hotel:admin:roomType:edit')" text type="primary" size="small"
								@click="doToggleStatus(scope.row)">{{ scope.row.status === 'ENABLED' ? '禁用' : '启用' }}</el-button>
							<el-button v-if="hasPermission('hotel:admin:roomType:delete')" text type="danger" size="small"
								@click="doDelete(scope.row.id)">删除</el-button>
						</template>
					</vxe-column>
				</vxe-table>
				<vxe-pager background :current-page="tablePage.currentPage" :page-size="tablePage.pageSize"
					:total="tablePage.total" :page-sizes="[10, 20, 100, 1000, { label: '全量数据', value: 1000000 }]"
					:layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
					@page-change="currentChangeHandle">
				</vxe-pager>
			</div>
		</div>
		<RoomTypeForm ref="roomTypeForm" @refreshList="refreshList"></RoomTypeForm>
	</div>
</template>

<script>
import HotelRoomTypeService from '@/api/hotel/HotelRoomTypeService'
import RoomTypeForm from './RoomTypeForm'
export default {
	data() {
		return {
			searchVisible: true,
			searchForm: {
				name: '',
				status: ''
			},
			dataList: [],
			tablePage: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				orders: []
			},
			loading: false
		}
	},
	hotelRoomTypeService: null,
	created() {
		this.hotelRoomTypeService = new HotelRoomTypeService()
	},
	components: {
		RoomTypeForm
	},
	mounted() {
		this.refreshList()
	},
	activated() {
		this.refreshList()
	},
	methods: {
		add() {
			this.$refs.roomTypeForm.init('add', '')
		},
		edit(id) {
			this.$refs.roomTypeForm.init('edit', id)
		},
		refreshList() {
			this.loading = true
			this.hotelRoomTypeService.page({
				'current': this.tablePage.currentPage,
				'size': this.tablePage.pageSize,
				'orders': this.tablePage.orders,
				...this.searchForm
			}).then((data) => {
				this.dataList = data.records || []
				this.tablePage.total = data.total || 0
				this.tablePage.currentPage = data.current || 1
				this.loading = false
			}).catch(() => {
				this.loading = false
			})
		},
		currentChangeHandle({ currentPage, pageSize }) {
			this.tablePage.currentPage = currentPage
			this.tablePage.pageSize = pageSize
			this.refreshList()
		},
		resetSearch() {
			this.$refs.searchForm.resetFields()
			this.tablePage.currentPage = 1
			this.refreshList()
		},
		doToggleStatus(row) {
			const action = row.status === 'ENABLED' ? '禁用' : '启用'
			this.$confirm(`确定${action}该房间类型吗？`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.hotelRoomTypeService.toggleStatus(row.id).then(() => {
					this.$message.success('操作成功')
					this.refreshList()
				})
			})
		},
		doDelete(id) {
			this.$confirm('确定删除该房间类型吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.loading = true
				this.hotelRoomTypeService.delete(id).then(() => {
					this.$message.success('删除成功')
					this.refreshList()
					this.loading = false
				}).catch(() => {
					this.loading = false
				})
			})
		}
	}
}
</script>
