<template>
    <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>
        <template #extra>
            <el-button type="primary" @click="addRowClick" size="large">
                <HcIcon name="add"/>
                <span>新增合同信息</span>
            </el-button>
        </template>
        <HcTable :loading="tableLoading" :column="tableColumn" :datas="tableData">
            <template #conFileName="{row}">
                <div class="text-link text-blue" @click="viewPdf(row)">{{ row?.conFileName }}</div>
            </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>
    </HcCard>
</template>

<script setup>
import {onActivated, ref} from 'vue'
import mainApi from "~api/people/contract";
import {useRouter} from 'vue-router'
import {getuserList} from "~api/other";
import {getArrValue} from "js-fast-way";
import {delMessage} 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: 'userName', name: '姓名'},
    {key: 'deptName', name: '部门'},
    {key: 'startDate', name: '合同开始时间'},
    {key: 'endDate', name: '合同结束时间'},
    {key: 'conTypeName', name: '合同类型'},
    {key: 'conFileName', name: '合同附件'},
    {key: 'action', name: '操作', align: 'center', fixed: 'right'},
]

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

//新增
const addRowClick = () => {
    router.push({name: 'people-contract-info'})
}

//编辑
const editRowClick = (row) => {
    router.push({
        name: 'people-contract-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 viewPdf = ({fileUrl}) => {
    if (fileUrl) {
        window.open(fileUrl, '_blank')
    } else {
        window.$message.error('暂无文件')
    }
}
</script>
