<template>
    <hc-new-card id-ref="hc-certificate-management" div-p="12px">
        <template #header>
            <div class="w-100">
                <el-select v-model="searchForm.projectId" filterable clearable block placeholder="选择项目" @change="searchClick">
                    <el-option v-for="item in projectData" :key="item.id" :label="item.projectName" :value="item.id" />
                </el-select>
            </div>
        </template>
        <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" :is-index="false" is-new>
            <template #action="{ row }">
                <el-link type="primary" @click="rowClick(row)">查看</el-link>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>
        <!-- 查看 -->
        <hc-new-drawer v-model="isRowDrawer" is-close to-id="hc-certificate-management">
            <HcAdminSee v-if="isRowDrawer" :cid="tableRowItem.contractId" />
        </hc-new-drawer>
    </hc-new-card>
</template>

<script setup>
import { nextTick, onActivated, ref } from 'vue'
import { getArrValue } from 'js-fast-way'
import HcAdminSee from './modules/admin/see.vue'
import mainApi from '~api/certificate/admin'

defineOptions({
    name: 'CertificateAdmin',
})

//激活
onActivated(() => {
    getDataApi()
})

const getDataApi = async () => {
    await getProjectData()
    searchClick()
}

//项目列表
const projectData = ref([])
const getProjectData = async () => {
    const { data } = await mainApi.queryProjectList()
    projectData.value = getArrValue(data)
}

//搜索条件
const searchForm = ref({ projectId: '', current: 1, size: 20, total: 0 })

//搜索
const searchClick = () => {
    searchForm.value.current = 1
    getTableData()
}

//分页
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}

//表格数据
const tableColumn = ref([
    { key: 'projectName', name: '项目名称' },
    { key: 'personalCount', name: '个人证书（个）', align: 'center' },
    { key: 'enterpriseCount', name: '企业证书（个）', align: 'center' },
    { key: 'action', name: '操作', width: 80, align: 'center' },
])
const tableData = ref([])

//获取表格数据
const tableLoading = ref(true)
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { error, code, data } = await mainApi.page({
        ...searchForm.value,
        total: null,
    })
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        searchForm.value.total = data['total']
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}

//点击查看
const isRowDrawer = ref(false)
const tableRowItem = ref({})
const rowClick = (row) => {
    tableRowItem.value = row
    nextTick(() => {
        isRowDrawer.value = true
    })
}
</script>
