<template xmlns="">
    <HcCard>
        <template #header>
            <div class="w-36 ml-2">
                <el-select v-model="searchForm.userId" block clearable filterable placeholder="员工姓名" size="large">
                    <el-option v-for="item in userList" :label="item.name" :value="item.id" />
                </el-select>
            </div>
            <div class="ml-4">
                <el-button type="primary" @click="searchClick" size="large">
                    <HcIcon name="search-2"/>
                    <span>搜索</span>
                </el-button>
            </div>
            <div class="ml-2">
                <el-button size="large" @click="resetClick">
                    <HcIcon name="close-circle"/>
                    <span>重置</span>
                </el-button>
            </div>
        </template>
        <HcTable :loading="tableLoading" :column="tableColumn" :datas="tableData">
            <template #conFileUrl="{row}">
                <span class="text-blue text-hover" @click="rowViewPdf(row.conFileUrl)">
                    {{getFileName(row.conFileUrl)}}
                </span>
            </template>
            <template #carAimgUrl="{row}">
                <span class="text-blue text-hover" @click="rowViewImg(row.carAimgUrl)">
                    {{getFileName(row.carAimgUrl)}}
                </span>
            </template>
            <template #carBimgUrl="{row}">
                <span class="text-blue text-hover" @click="rowViewImg(row.carBimgUrl)">
                    {{getFileName(row.carBimgUrl)}}
                </span>
            </template>
            <template #userImgUrl="{row}">
                <span class="text-blue text-hover" @click="rowViewImg(row.userImgUrl)">
                    {{getFileName(row.userImgUrl)}}
                </span>
            </template>
            <template #action="{row, index}">
                <el-button type="primary" size="small" @click="editRowClick(row)">编辑</el-button>
                <el-button type="danger" size="small" @click="delRowClick(row)">删除</el-button>
            </template>
        </HcTable>
        <template #action>
            <HcPages :pages="searchForm" @change="pageChange"/>
        </template>
        <!--预览图片-->
        <el-image-viewer v-if="showViewer" :initial-index="initialIndex" :url-list="previewFileList" @close="showViewerClose"/>
    </HcCard>
</template>

<script setup>
import {onActivated, ref} from 'vue'
import mainApi from "~api/people/archive";
import {useRouter} from 'vue-router'
import {getuserList} from "~api/other";
import {getArrValue} from "js-fast-way";
import {delMessage, getFileName} from "~uti/tools";

const router = useRouter()

onActivated(() => {
    getuserListApi()
    getTableData()
})

//获取用户下拉数据
const userList = ref([])
const getuserListApi = async () => {
    const {data} = await getuserList()
    userList.value = getArrValue(data)
}

const searchForm = ref({
    type: 1, userId: null,
    current: 1, size: 20, total: 0
})

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

//重置搜索表单
const resetClick = () => {
    searchForm.value = {current: 1, size: 20, total: 0}
}

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

//表格参数
const tableLoading = ref(false)
const tableColumn = [
    {key: 'name', name: '姓名', width: 100},
    {key: 'email', name: '邮箱', width: 120},
    {key: 'deptName', name: '部门', width: 120},
    {key: 'leaderName', name: '直属主管', width: 100},
    {key: 'postName', name: '职位', width: 120},
    {key: 'phone', name: '手机号', width: 120},
    {key: 'entryTime', name: '入职时间', width: 120},
    {key: 'cyear', name: '司龄', width: 100},
    {key: 'userTypeName', name: '员工类型', width: 100},
    {key: 'userStatusName', name: '员工状态', width: 100},
    {key: 'periodMoth', name: '试用期', width: 100},
    {key: 'idNumber', name: '身份证号码', width: 120},
    {key: 'carName', name: '身份证姓名', width: 120},
    {key: 'birthday', name: '出生日期', width: 120},
    {key: 'userAge', name: '年龄', width: 90},
    {key: 'sexName', name: '性别', width: 90},
    {key: 'nation', name: '民族', width: 90},
    {key: 'carAddr', name: '身份证地址', width: 120},
    {key: 'marriageStatusName', name: '婚姻状况', width: 100},
    {key: 'domicileAddr', name: '户籍', width: 100},
    {key: 'politicalName', name: '政治面貌', width: 100},
    {key: 'educationName', name: '学历', width: 100},
    {key: 'speciality', name: '专业', width: 100},
    {key: 'bankNum', name: '银行卡号', width: 120},
    {key: 'bankName', name: '开户行', width: 120},
    {key: 'conTypeName', name: '合同类型', width: 120},
    {key: 'conStartDate', name: '合同开始日期', width: 120},
    {key: 'conEndDate', name: '合同结束日期', width: 120},
    {key: 'renewalCount', name: '续签次数', width: 100},
    {key: 'conYear', name: '合同期限', width: 100},
    {key: 'conFileUrl', name: '合同附件', width: 120},
    {key: 'emergencyName', name: '紧急联系人姓名', width: 130},
    {key: 'emergencyPhone', name: '联系人电话', width: 120},
    {key: 'emergencyRelation', name: '联系人关系', width: 100},
    {key: 'carAimgUrl', name: '身份证(人面像)', width: 120},
    {key: 'carBimgUrl', name: '身份证(国徽)', width: 120},
    {key: 'userImgUrl', name: '员工照片', width: 120},
    {key: 'action', name: '操作', width: 145, align: 'center', fixed: 'right'},
]

//获取表格数据
const tableData = ref([
    {id:1}
])
const getTableData = async () => {
    tableLoading.value = true
    const {data} = await mainApi.page(searchForm.value)
    tableLoading.value = false
    tableData.value = getArrValue(data['records'])
    searchForm.value.total = data['total'] || 0
}

//编辑
const editRowClick = (row) => {
    router.push({
        name: 'people-archive-info',
        query: {id: row.id}
    })
}

//删除
const delRowClick = (row) => {
    delMessage(async () => {
        const {error, code, msg} = await mainApi.remove({ids: row.id})
        if (!error && code === 200) {
            window.$message?.success(msg)
            getTableData().then()
        } else {
            window.$message?.error(msg)
        }
    })
}

//预览Pdf
const rowViewPdf = (url) => {
    if (url) {
        window.open(url, '_blank')
    } else {
        window.$message?.error('文件不存在')
    }
}

//预览图片
const showViewer = ref(false)
const initialIndex = ref(0)
const previewFileList = ref([])
const rowViewImg = (url) => {
    if (url) {
        previewFileList.value = [url]
        showViewer.value = true
    } else {
        window.$message?.error('文件不存在')
    }
}

//关闭预览
const showViewerClose = () => {
    showViewer.value = false
}
</script>
