<template>
    <MyLayout>
      <el-card v-show="state.showQuery" class="my-query-box mt8" shadow="never" :body-style="{ paddingBottom: '0' }">
        <el-form :inline="true" label-width="auto" @submit.stop.prevent>
          <el-form-item label="关键词">
            <el-input v-model="state.filter.keyWord" placeholder="房间编号、房间名称" @keyup.enter="onQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
            <el-button v-auth="'api:admin:room:add'" type="primary" icon="ele-Plus" @click="onAdd"> 新增 </el-button>
          </el-form-item>
        </el-form>
      </el-card>
  
      <el-card class="my-fill mt8" shadow="never">
        <div class="my-tools-box mb8 my-flex my-flex-between">
          <div>
            </div>
        </div>
        <el-table
          v-if="state.showRoomList"
          :data="state.roomListData"
          style="width: 100%"
          v-loading="state.loading"
          row-key="id"
          default-expand-all
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="roomName" label="房间名称" min-width="120" show-overflow-tooltip />
          <el-table-column prop="roomNo" label="房间编码" min-width="120" show-overflow-tooltip />
          <el-table-column prop="sort" label="排序" width="80" align="center" show-overflow-tooltip />
          <el-table-column label="状态" width="80" align="center" show-overflow-tooltip>
            <template #default="{ row }">
              <el-tag type="success" v-if="row.enabled">启用</el-tag>
              <el-tag type="danger" v-else>禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" fixed="right" header-align="center" align="center">
            <template #default="{ row }">
               <el-button
                v-if="auth('api:admin:room:update')"
                icon="ele-EditPen"
                size="small"
                text
                type="primary"
                @click="onEdit(row)"
                >编辑</el-button
              >
              <el-button
                v-if="auth('api:admin:room:delete')"
                icon="ele-Delete"
                size="small"
                text
                type="danger"
                @click="onDelete(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <div class="my-flex my-flex-end" style="margin-top: 10px">
        <el-pagination
          v-model:currentPage="state.pageInput.currentPage"
          v-model:page-size="state.pageInput.pageSize"
          :total="state.total"
          :page-sizes="[10, 20, 50, 100]"
          size="small"
          background
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
      </el-card>
  
      <room-form ref="roomFormRef" :title="state.roomFormTitle"></room-form>
    </MyLayout>
  </template>
  
  <script lang="ts" setup name="admin/room">
  import { ref, reactive, onMounted, getCurrentInstance, onBeforeMount, defineAsyncComponent } from 'vue'
  import { RoomGetListOutput, PageInputRoomGetPageInput, RoomGetPageOutput } from '/@/api/admin/data-contracts'
  import { RoomApi } from '/@/api/admin/Room'
  import eventBus from '/@/utils/mitt'
  import { auth } from '/@/utils/authFunction'
  
  // 引入组件
  const RoomForm = defineAsyncComponent(() => import('./components/room-form.vue'))
  
  const { proxy } = getCurrentInstance() as any
  
  const roomFormRef = ref()
  
  const state = reactive({
    loading: false,
    roomFormTitle: '',
    filter: {
      keyWord: '',
    },
    total: 0,
    pageInput: {
        currentPage: 1,
        pageSize: 20,
    } as PageInputRoomGetPageInput,
    roomListData: [] as Array<RoomGetPageOutput>,
    showQuery: true,
    showRoomList: true,
  })
  
  onMounted(() => {
    Query()
    eventBus.off('refreshRoom')
    eventBus.on('refreshRoom', () => {
      Query()
    })
  })
  
  onBeforeMount(() => {
    eventBus.off('refreshRoom')
  })
  
  const onChangeRoomList = () => {
    state.showRoomList = !state.showRoomList
    if (state.showRoomList) {
      Query()
    }
  }
  
  const onQuery = () => {
      Query()
  }
  
  const Query = async () => {
    state.loading = true
        state.pageInput.filter = state.filter
        const res = await new RoomApi().getPage(state.pageInput).catch(() => {
        state.loading = false
    })

    state.roomListData = res?.data?.list ?? []
    state.total = res?.data?.total ?? 0
    state.loading = false
  }
  
  const onAdd = () => {
    state.roomFormTitle = '新增房间'
    roomFormRef.value.open(
        { id: 0,    
         enabled: true,   
             roomName: '',     
                roomNo: '',       
                 sort: 0})
    }
  
  const onEdit = (row: RoomGetListOutput) => {
    state.roomFormTitle = '编辑房间'
    roomFormRef.value.open(row)
  }
  
  const onDelete = (row: RoomGetListOutput) => {
    proxy.$modal
      .confirmDelete(`确定要删除房间【${row.roomName}】?`)
      .then(async () => {
        await new RoomApi().delete({ id: row.id }, { loading: true })
        Query()
      })
      .catch(() => {})
  }

  
  const onSizeChange = (val: number) => {
    state.pageInput.currentPage = 1
    state.pageInput.pageSize = val
    onQuery()
  }

  const onCurrentChange = (val: number) => {
    state.pageInput.currentPage = val
    onQuery()
  }

  </script>
  <style scoped lang="scss"></style>
  