<!--
  Description: 场景数据
  Author: 李亚光
  Date: 2024-11-07
 -->
<script lang="ts" setup name="SceneManagePage">
import { ElMessage, ElMessageBox } from 'element-plus'
import addDialog from './components/addDialog.vue'
import { getSceneList, removeScene } from '@/api/page/scene'
// 获取页面高度
const pageHeight = ref(window.innerHeight - 50 - 60 - 20 - 20 - 10 - 30 - 15 - 15 - 20)
window.addEventListener('resize', () => {
  pageHeight.value = window.innerHeight - 50 - 60 - 20 - 20 - 10 - 30 - 15 - 15 - 20
})
const loadingTable = ref(true)
const total = ref(0)
const list = ref<any[]>([])
const columns = ref([
  { text: '场景名称', value: 'name', align: 'center' },
  { text: '场景ID', value: 'id', align: 'center' },
  { text: '版本号', value: 'version', align: 'center' },
  { text: '场景简述', value: 'remark', align: 'center' },
  { text: '使用状态', value: 'usage_status', align: 'center' },
])
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
  name: '',
  // end_time: '',
})
// 获取数据
const fetchData = () => {
  loadingTable.value = true
  getSceneList(listQuery.value).then((res) => {
    console.log(res.data, '场景数据')
    list.value = res.data.items
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
fetchData()
// 重置
const reset = () => {
  // datetimerange.value = []
  listQuery.value = {
    limit: 20,
    offset: 1,
    name: '',
  }
  fetchData()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData()
}
const addRef = ref()
const addRow = (row: any, type: string) => {
  addRef.value.initDialog(row, type)
}

// 删除
const deleteRow = (row: any) => {
  ElMessageBox.confirm(
    '确定要该数据吗？',
    '确认操作',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    removeScene(row.id).then(() => {
      ElMessage.success('操作成功')
      fetchData()
    })
  })
}
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <add-dialog ref="addRef" @refresh="fetchData" />
    <search-area :need-clear="true" @search="fetchData" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.name" placeholder="场景名称" clearable />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <!-- 操作 -->
        <div>
          <el-button type="primary" @click="addRow({}, 'create')">
            新建
          </el-button>
        </div>
      </template>
      <!-- 查询结果Table显示 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="loadingTable"
        :height="pageHeight" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="90" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <el-button type="primary" link size="small" @click="addRow(scope.row, 'update')">
                编辑
              </el-button>
              <el-button type="danger" link size="small" @click="deleteRow(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
