<!-- 印章使用日志 -->
<script name="SealLogList" lang="ts" setup>
import dayjs from 'dayjs'
import type { DateModelType } from 'element-plus'
import type { ILogListQuery, ISealLogInfo } from './seal-note'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getSealLogList } from '@/api/resource/seal'

// 查询条件
const searchQuery = ref<ILogListQuery>({
  sealName: '', // 印章名
  useTimeStart: '',
  useTimeEnd: '',
  offset: 1,
  limit: 20,
})
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据

// 表头
const columns = ref<TableColumn[]>([
  { text: '印章名称', value: 'sealName', align: 'center', width: '240' },
  { text: '印章类型', value: 'sealTypeName', align: 'center', width: '240' },
  { text: '使用业务场景', value: 'businessScene', align: 'center' },
  { text: '业务发起人', value: 'businessSponsorName', align: 'center', width: '240' },
  { text: '使用时间', value: 'useTime', align: 'center', width: '180' },
])
const sealLogList = ref<Array<ISealLogInfo>>([]) // 表格数据

// 逻辑

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    searchQuery.value.offset = 1
  }
  getSealLogList(searchQuery.value).then((response) => {
    if (response.code === 200) {
      sealLogList.value = response.data.rows.map((item: ISealLogInfo) => {
        return {
          ...item,
        }
      })
      total.value = parseInt(response.data.total)
    }
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}

const searchList = () => {
  fetchData(true)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  fetchData(true)
}

// 重置
const reset = () => {
  searchQuery.value = {
    sealName: '', // 印章名
    useTimeStart: '',
    useTimeEnd: '',
    offset: 1,
    limit: 20,
  }
  dateRange.value = ['', '']
  fetchData(true)
}

watch(dateRange, (val) => {
  if (val) {
    searchQuery.value.useTimeStart = dayjs(val[0]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[0]).format('YYYY-MM-DD')
    searchQuery.value.useTimeEnd = dayjs(val[1]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[1]).format('YYYY-MM-DD')
  }
  else {
    searchQuery.value.useTimeStart = ''
    searchQuery.value.useTimeEnd = ''
  }
})

onMounted(() => {
  searchList()
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.sealName" placeholder="印章名称" clearable />
      </search-item>
      <search-item>
        <el-date-picker v-model="dateRange" type="daterange" start-placeholder="创建时间（开始）" end-placeholder="创建时间（结束）" />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container>
      <!-- 表格区域 -->
      <normal-table
        :data="sealLogList" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>

        <template #columns />
      </normal-table>
    </table-container>
  </app-container>
</template>

