<template>
  <el-row :gutter="20">
    <!-- 左侧部门树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <DeptTree @node-click="handleDeptNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <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 label="手机" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="工号" prop="workNo">
            <el-input
              v-model="queryParams.workNo"
              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 prop="workNo" label="员工工号">
            <template #default="scope">
              {{ employeeList.find((user) => user.id === scope.row.employeeId)?.workNo }}
            </template>
          </el-table-column>
          <el-table-column prop="name" label="部门">
            <template #default="scope">
              {{ deptList.find((dept) => dept.id === scope.row.deptId)?.name }}
            </template>
          </el-table-column>
          <el-table-column label="岗位" align="center" prop="postName" />
          <el-table-column label="入职日期" align="center" prop="entryTime" width="100" />
          <el-table-column label="转正日期" align="center" prop="becomeTime" width="100" />
          <el-table-column label="最近调整日期" align="center" prop="changeData" width="120" />
          <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="total" />

          <el-table-column label="操作" align="center" min-width="120px" fixed="right">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row)"
                v-hasPermi="['hr:salary-archives:update']"
              >
                {{ scope.row.changeType == 0 ? '定薪' : '调薪' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>

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

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SalaryArchivesApi, SalaryArchives } from '@/api/hr/salaryarchives'
import SalaryArchivesForm from './SalaryArchivesForm.vue'
import SalaryArchivesForm2 from './SalaryArchivesForm2.vue'
import * as EmployeeApi from '@/api/hr/employee'
import { EmployeeVO } from '@/api/hr/employee'
import { handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'
import { DICT_TYPE } from '@/utils/dict'
import { DeptVO } from '@/api/hr/dept'
import DeptTree from '@/views/hr/employee/DeptTree.vue'

/** 薪资档案 列表 */
defineOptions({ name: 'HrSalaryArchives' })

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

const loading = ref(true) // 列表的加载中
const list = ref<SalaryArchives[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const employeeList = ref<EmployeeApi.EmployeeVO[]>([]) // 员工列表
const deptList = ref<DeptApi.DeptVO[]>([]) // 员工列表
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  changeReason: undefined,
  changeData: undefined,
  employeeId: undefined,
  changeType: undefined,
  remarks: undefined,
  employeeName: undefined,
  mobile: undefined,
  workNo: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SalaryArchivesApi.getSalaryArchivesPage(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 handleDeptNodeClick = async (row: any) => {
  if (row === undefined) {
    queryParams.deptId = undefined
    await getList()
  } else {
    queryParams.deptId = row.id
    await getList()
  }
}
/** 添加/修改操作 */
const formRef = ref()
const formRef2 = ref()
const openForm = (type: string, row?: any) => {
  if (row.changeType == 0) {
    formRef2.value.open(type, row)
    return
  }
  formRef.value.open(type, row)
}

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

/** 批量删除薪资档案 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await SalaryArchivesApi.deleteSalaryArchivesList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SalaryArchivesApi.exportSalaryArchives(queryParams)
    download.excel(data, '薪资档案.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  // 获取用户列表
  employeeList.value = await EmployeeApi.getSimpleUserList()
  // 加载部门树
  deptList.value = await DeptApi.getSimpleDeptCompanyList()
})
</script>
