<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 搜索表单 -->
      <search-form 
        v-show="showSearch" 
        :model="search" 
        ref="searchFormRef"
        @keyup-enter="handleFilter"
      >
        <template #default="{ visible }">
          <template v-if="visible">
            <el-form-item label="薪资年份" prop="salaryYear">
              <el-date-picker
                v-model="search.salaryYear"
                type="year"
                format="YYYY"
                value-format="YYYY"
                placeholder="请选择薪资年份"
                clearable
              />
            </el-form-item>

            <el-form-item label="薪资月份" prop="salaryMonth">
              <el-date-picker
                v-model="search.salaryMonth"
                type="month"
                format="M"
                value-format="M"
                placeholder="请选择薪资月份"
                clearable
              />
            </el-form-item>
          </template>
        </template>

        <!-- 查询和重置按钮 -->
        <template #actions>
          <el-form-item>
            <el-button type="primary" @click="handleFilter" icon="Search">查询</el-button>
            <el-button @click="resetQuery" icon="Refresh">重置</el-button>
          </el-form-item>
        </template>
      </search-form>

      <!-- 表格 -->
      <el-table
        ref="tableRef"
        :data="state.dataList"
        v-loading="state.loading"
        border
        row-key="id"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
        class="data-table"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        
        <el-table-column prop="createTime" label="导出时间" min-width="180" align="center" />
        
        <el-table-column prop="salaryYear" label="薪资年份" min-width="120" align="center" />
        
        <el-table-column prop="salaryMonth" label="薪资月份" min-width="120" align="center" />
        
        <el-table-column prop="confirm" label="劳务日期锁定" min-width="150" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.confirm === '1' ? 'success' : 'info'">
              {{ scope.row.confirm === '1' ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" min-width="80" align="center" fixed="right">
          <template #default="scope">
            <el-button
                v-if="hasAuth('professional_salaryexportrecord_del')"
                icon="delete"
                link
                type="primary"
                @click="handleDel(scope.row)">删除
              </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-bind="state.pagination"
        @current-change="currentChangeHandle"
        @size-change="sizeChangeHandle"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { useMessage, useMessageBox } from '/@/hooks/message'
import { fetchList, delObj } from '/@/api/professional/salaries/salaryexportrecord'
import { useAuth } from '/@/hooks/auth'

const { hasAuth } = useAuth()
// 消息提示 hooks
const message = useMessage()
const messageBox = useMessageBox()

// 表格引用
const tableRef = ref()
const searchFormRef = ref()

// 搜索表单显示状态
const showSearch = ref(true)

// 搜索表单数据
const search = reactive({
  salaryYear: '',
  salaryMonth: ''
})

// 配置 useTable
const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: async (params: any) => {
    const response = await fetchList(params)
    return {
      data: {
        records: response.data.records || [],
        total: response.data.total || 0
      }
    }
  },
  queryForm: search
})

const { getDataList, currentChangeHandle, sizeChangeHandle, tableStyle } = useTable(state)

// 查询
const handleFilter = () => {
  getDataList() // 查询后跳转到第一页
}

// 重置
const resetQuery = () => {
  searchFormRef.value?.formRef?.resetFields()
  Object.assign(search, {
    salaryYear: '',
    salaryMonth: ''
  })
  handleFilter()
}

// 删除
const handleDel = (row: any) => {
  messageBox.confirm('确认删除?').then(async () => {
    try {
      await delObj(row.id)
      message.success('删除成功')
      getDataList(false) // 删除后保持当前页
    } catch (error: any) {
      // 错误处理已在数据请求层统一处理，此处不需要提示
    }
  }).catch(() => {
    // 用户取消
  })
}
</script>

<style lang="scss" scoped>
.data-table {
  width: 100% !important;
}
</style>
