<template>
  <div
    class="grid grid-cols-[auto_1fr] grid-rows-[auto_auto_minmax(0,1fr)] gap-0 gap-x-4 h-[calc(100vh-20px-var(--top-tool-height)-var(--tags-view-height)-var(--app-footer-height))]">
    <DeptTreeSelect
      class="row-span-4"
      :top-id="rootDeptId"
      :deptId="deptId"
      v-model="queryParams.deptId"
      :init-select="false"
      :show-title="false"
      request-api="getSimpleDeptList"
      @node-click="handleDeptNodeClick" />

    <div class="mb-1">
      <!-- 搜索工作栏 -->
      <el-form
        class="bg-white dark:bg-[#1d1e1f] rounded-lg shadow px-8 pt-4 flex items-center flex-wrap min-w-0"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        @submit.prevent
        border>
        <el-form-item label="人员名称" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入人员名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-150px" />
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
          <el-input
            v-model="queryParams.nickname"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-150px" />
        </el-form-item>

        <el-form-item label="岗位" prop="postName">
          <el-input
            v-model="queryParams.postName"
            placeholder="请输入岗位名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-150px" />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery" v-hasPermi="['rq:qhse-cert-person:query']"
            ><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="['rq:qhse-cert-person:create']">
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['rq:qhse-cert-person:export']">
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="min-w-0"></div>

    <div class="bg-white dark:bg-[#1d1e1f] shadow rounded-lg flex flex-col p-2 pt-4 min-w-0">
      <div class="flex-1 relative min-h-0">
        <el-auto-resizer class="absolute">
          <template #default="{ width, height }">
            <zm-table
              :loading="loading"
              :data="list"
              :stripe="true"
              :show-overflow-tooltip="true"
              :width="width"
              :height="height"
              :max-height="height">
              <el-table-column :label="t('monitor.serial')" width="70" align="center">
                <template #default="scope">
                  {{ scope.$index + 1 }}
                </template>
              </el-table-column>

              <zm-table-column label="人员名称" align="center" prop="username" />
              <zm-table-column label="姓名" align="center" prop="nickname" />
              <zm-table-column label="岗位" align="center" prop="postName" />
              <zm-table-column label="手机号" align="center" prop="mobile" />
              <zm-table-column label="部门名称" align="center" prop="deptName" />
              <zm-table-column label="三小证" align="center">
                <zm-table-column label="硫化氢" align="center">
                  <template #default="scope">
                    <el-button
                      circle
                      type="success"
                      style="border: none"
                      plain
                      v-if="scope.row.lsh">
                      <span class="text-[#259644]">
                        {{ '✔' }}
                      </span>
                    </el-button>
                    <span v-else></span>
                  </template>
                </zm-table-column>
                <zm-table-column label="井控（A2）" align="center">
                  <template #default="scope">
                    <el-button circle type="success" style="border: none" plain v-if="scope.row.jk">
                      <span class="text-[#259644]">
                        {{ '✔' }}
                      </span>
                    </el-button>
                    <span v-else></span>
                  </template>
                </zm-table-column>
                <zm-table-column label="HSE" align="center">
                  <template #default="scope">
                    <el-button
                      circle
                      type="success"
                      style="border: none"
                      plain
                      v-if="scope.row.hse">
                      <span class="text-[#259644]">
                        {{ '✔' }}
                      </span>
                    </el-button>
                    <span v-else></span>
                  </template>
                </zm-table-column>
              </zm-table-column>

              <zm-table-column label="司钻证" align="center">
                <template #default="scope">
                  <el-button circle type="success" style="border: none" plain v-if="scope.row.sz">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="高处作业证" align="center">
                <template #default="scope">
                  <el-button circle type="success" style="border: none" plain v-if="scope.row.gczy">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="电工证" align="center">
                <template #default="scope">
                  <el-button circle type="success" style="border: none" plain v-if="scope.row.dg">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="连续油管主操证" align="center" min-width="120px">
                <template #default="scope">
                  <el-button
                    circle
                    type="success"
                    style="border: none"
                    plain
                    v-if="scope.row.lxygzc">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="吊装证" align="center">
                <template #default="scope">
                  <el-button circle type="success" style="border: none" plain v-if="scope.row.dz">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="安全管理证" align="center">
                <template #default="scope">
                  <el-button circle type="success" style="border: none" plain v-if="scope.row.aqgl">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="国际井控证" align="center">
                <template #default="scope">
                  <el-button circle type="success" style="border: none" plain v-if="scope.row.gjjk">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="消防证" align="center">
                <template #default="scope">
                  <el-button
                    circle
                    type="success"
                    style="border: none"
                    plain
                    v-if="scope.row.xiaofang">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="急救证" align="center">
                <template #default="scope">
                  <el-button
                    circle
                    type="success"
                    style="border: none"
                    plain
                    v-if="scope.row.jijiu">
                    <span class="text-[#259644]">
                      {{ '✔' }}
                    </span>
                  </el-button>
                  <span v-else></span>
                </template>
              </zm-table-column>

              <zm-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />

              <zm-table-column label="操作" align="center" min-width="120px" action fixed="right">
                <template #default="scope">
                  <el-button
                    link
                    type="primary"
                    @click="openForm('update', scope.row.id)"
                    v-hasPermi="['rq:qhse-cert-person:update']">
                    编辑
                  </el-button>
                  <el-button
                    link
                    type="danger"
                    @click="handleDelete(scope.row.id)"
                    v-hasPermi="['rq:qhse-cert-person:delete']">
                    删除
                  </el-button>
                </template>
              </zm-table-column>
            </zm-table>
          </template>
        </el-auto-resizer>
      </div>

      <div class="h-8 mt-2 flex items-center justify-end">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </div>
    </div>
  </div>

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

<script setup lang="ts">
import download from '@/utils/download'
import { CertPersonApi } from '@/api/pms/qhse/index'
import CertPersonForm from './CertPersonForm.vue'

import { useUserStore } from '@/store/modules/user'

defineOptions({ name: 'CertPerson' })
const rootDeptId = 156
const deptId = useUserStore().getUser.deptId || rootDeptId

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

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  username: undefined,
  nickname: undefined,
  postName: undefined,
  deptId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

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

const handleDeptNodeClick = async (row) => {
  queryParams.deptId = row.id
  queryParams.pageNo = 1
  getList()
}

/** 搜索按钮操作 */
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 CertPersonApi.deleteCertPerson(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

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

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