<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="父编码" prop="parentCode">
        <el-input
          v-model="queryParams.parentCode"
          placeholder="请输入父编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          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-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          @click="openForm('create')"-->
<!--          v-hasPermi="['hr:salary-option:create']"-->
<!--        >-->
<!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          @click="handleExport"-->
<!--          :loading="exportLoading"-->
<!--          v-hasPermi="['hr:salary-option:export']"-->
<!--        >-->
<!--          <Icon icon="ep:download" class="mr-5px" /> 导出-->
<!--        </el-button>-->
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            :disabled="isEmpty(checkedIds)"-->
<!--            @click="handleDeleteBatch"-->
<!--            v-hasPermi="['hr:salary-option:delete']"-->
<!--        >-->
<!--          <Icon icon="ep:delete" 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 label="编码" align="center" prop="code" width="80" />
<!--      <el-table-column label="父编码" align="center" prop="parentCode" />-->
      <el-table-column label="名称" align="center" prop="name" width="200"/>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="是否固定" align="center" prop="isFixed" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isFixed" />
        </template>
      </el-table-column>
      <el-table-column label="是否加项" align="center" prop="isPlus" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isPlus" />
        </template>
      </el-table-column>
      <el-table-column label="是否计税" align="center" prop="isTax" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isTax" />
        </template>
      </el-table-column>
      <el-table-column label="是否展示" align="center" prop="isShow" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isShow" />
        </template>
      </el-table-column>
      <el-table-column label="是否参与薪资计算" align="center" prop="isCompute" width="140">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isCompute" />
        </template>
      </el-table-column>
      <el-table-column label="是否打开" align="center" prop="isOpen" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isOpen" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
<!--      <el-table-column label="操作" align="center" min-width="120px">-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['hr:salary-option:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            link-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.row.id)"-->
<!--            v-hasPermi="['hr:salary-option:delete']"-->
<!--          >-->
<!--            删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SalaryOptionForm 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 { SalaryOptionApi, SalaryOption } from '@/api/hr/salaryoption'
import SalaryOptionForm from './SalaryOptionForm.vue'
import {DICT_TYPE} from "@/utils/dict";

/** 系统薪资项 列表 */
defineOptions({ name: 'HrSalaryOption' })

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

const loading = ref(true) // 列表的加载中
const list = ref<SalaryOption[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  parentCode: undefined,
  name: undefined,
  remarks: undefined,
  isFixed: undefined,
  isPlus: undefined,
  isTax: undefined,
  isShow: undefined,
  isCompute: undefined,
  isOpen: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SalaryOptionApi.getSalaryOptionPage(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 SalaryOptionApi.deleteSalaryOption(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除系统薪资项 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await SalaryOptionApi.deleteSalaryOptionList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

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

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

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
