<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="员工姓名" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column prop="employeeName" label="员工姓名">
        <template #default="scope">
          {{ employeeList.find((user) => user.id === scope.row.employeeId)?.employeeName }}
        </template>
      </el-table-column>
<!--      <el-table-column label="记录类型 1 定薪 2 调薪" align="center" prop="recordType" />-->
<!--      <el-table-column label="调薪原因 1 入职核定 2 转正 3 晋升 4 调动 5 年中调薪 6 年度调薪 7 特别调薪 8 其他" align="center" prop="changeReason" />-->
      <el-table-column label="调薪原因" align="center" prop="changeReason">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HR_CHANGE_REASON" :value="scope.row.changeReason" />
        </template>
      </el-table-column>
      <el-table-column label="生效时间" align="center" prop="enableDate" />
      <el-table-column label="试用期调整前工资" align="center" prop="proBeforeSum" />
      <el-table-column label="试用期调整后工资" align="center" prop="proAfterSum" />
      <el-table-column label="正式调整前工资" align="center" prop="beforeSum" />
      <el-table-column label="正式调整后工资" align="center" prop="afterSum" />
<!--      <el-table-column label="状态 0 未生效 1 已生效 2 已取消" align="center" prop="status" />-->
      <el-table-column label="调整前总薪资" align="center" prop="beforeTotal" />
      <el-table-column label="调整后总薪资" align="center" prop="afterTotal" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SalaryChangeRecordForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SalaryChangeRecordApi, SalaryChangeRecord } from '@/api/hr/salarychangerecord'
import SalaryChangeRecordForm from './SalaryChangeRecordForm.vue'
import * as EmployeeApi from '@/api/hr/employee'
import { EmployeeVO } from '@/api/hr/employee'
import {DICT_TYPE} from "@/utils/dict";
import {InsuranceMonthRecord} from "@/api/hr/insurancemonthrecord";

/** 定薪调薪记录 列表 */
defineOptions({ name: 'HrSalaryChangeRecord' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SalaryChangeRecord[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  employeeId: undefined,
  employeeName: undefined,
  recordType: undefined,
  changeReason: undefined,
  enableDate: [],
  proBeforeSum: undefined,
  proAfterSum: undefined,
  proSalary: undefined,
  beforeSum: undefined,
  afterSum: undefined,
  salary: undefined,
  status: undefined,
  employeeStatus: undefined,
  beforeTotal: undefined,
  afterTotal: undefined,
  remarks: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SalaryChangeRecordApi.getSalaryChangeRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SalaryChangeRecordApi.deleteSalaryChangeRecord(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除定薪调薪记录 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await SalaryChangeRecordApi.deleteSalaryChangeRecordList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: SalaryChangeRecord[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SalaryChangeRecordApi.exportSalaryChangeRecord(queryParams)
    download.excel(data, '定薪调薪记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const employeeList = ref<EmployeeApi.EmployeeVO[]>([]) // 员工列表

/** 初始化 **/
onMounted(async () => {
  await getList()
  // 获取用户列表
  employeeList.value = await EmployeeApi.getSimpleUserList()
})
</script>
