<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="mealType">
				<el-select v-model="searchForm.mealType" placeholder="请选择餐段" clearable style="width: 120px">
					<el-option label="早餐" value="breakfast"></el-option>
					<el-option label="午餐" value="lunch"></el-option>
					<el-option label="晚餐" value="dinner"></el-option>
					<el-option label="夜宵" value="night"></el-option>
				</el-select>
			</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:businessHours: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="mealName" min-width="120" align="center"></vxe-column>
					<vxe-column title="星期" width="120" align="center">
						<template #default="scope">
							{{ scope.row.dayOfWeek !== null && scope.row.dayOfWeek !== undefined ? weekDays[scope.row.dayOfWeek] : '每天' }}
						</template>
					</vxe-column>
					<vxe-column title="开始时间" field="startTime" width="120" align="center"></vxe-column>
					<vxe-column title="结束时间" field="endTime" width="120" 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="sortOrder" width="100" align="center"></vxe-column>
					<vxe-column title="操作" width="200" fixed="right" align="center">
						<template #default="scope">
							<el-button v-if="hasPermission('hotel:admin:businessHours:edit')" text type="primary" size="small"
								@click="edit(scope.row.id)">编辑</el-button>
							<el-button v-if="hasPermission('hotel:admin:businessHours:edit')" text type="primary" size="small"
								@click="doToggle(scope.row)">{{ scope.row.status === 'ENABLED' ? '禁用' : '启用' }}</el-button>
							<el-button v-if="hasPermission('hotel:admin:businessHours: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>
		<BusinessHoursForm ref="businessHoursForm" @refreshList="refreshList"></BusinessHoursForm>
	</div>
</template>

<script>
import HotelBusinessHoursService from '@/api/hotel/HotelBusinessHoursService'
import BusinessHoursForm from './BusinessHoursForm'

export default {
	data() {
		return {
			searchVisible: true,
			searchForm: {
				mealType: '',
				status: ''
			},
			dataList: [],
			tablePage: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				orders: []
			},
			loading: false,
			weekDays: ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
		}
	},
	hotelBusinessHoursService: null,
	created() {
		this.hotelBusinessHoursService = new HotelBusinessHoursService()
	},
	components: {
		BusinessHoursForm
	},
	mounted() {
		this.refreshList()
	},
	activated() {
		this.refreshList()
	},
	methods: {
		add() {
			this.$refs.businessHoursForm.init('add', '')
		},
		edit(id) {
			this.$refs.businessHoursForm.init('edit', id)
		},
		refreshList() {
			this.loading = true
			this.hotelBusinessHoursService.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()
		},
		doToggle(row) {
			const action = row.status === 'ENABLED' ? '禁用' : '启用'
			this.$confirm(`确定${action}该营业时段吗？`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.hotelBusinessHoursService.toggle(row.id).then(() => {
					this.$message.success('操作成功')
					this.refreshList()
				})
			})
		},
		doDelete(id) {
			this.$confirm('确定删除该营业时段吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.loading = true
				this.hotelBusinessHoursService.delete(id).then(() => {
					this.$message.success('删除成功')
					this.refreshList()
					this.loading = false
				}).catch(() => {
					this.loading = false
				})
			})
		}
	}
}
</script>
