<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="w-36 ml-2">
                <el-date-picker v-model="searchForm.month" class="block" size="large" type="month" format="YYYY-MM" value-format="YYYY-MM"/>
            </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="downloadXlsx" size="large">
                <HcIcon name="download-2"/>
                <span>下载模板</span>
            </el-button>
            <el-button type="primary" @click="toImportTempClick" size="large" class="ml-2">
                <HcIcon name="folder-upload"/>
                <span>导入数据</span>
            </el-button>
        </template>
        <HcTable border :isIndex="false" :loading="tableLoading" :column="tableColumn" :datas="tableData"/>
        <template #action>
            <HcPages :pages="searchForm" @change="pageChange"/>
        </template>

        <!--导入数据弹窗-->
        <HcDialog bgColor="#ffffff" widths="26rem" isToBody :show="importModal" title="导入数据"
                  saveText="上传excel" @close="importModalClose" @save="importModalSave"
        >
            <div class="w-full">
                <el-date-picker v-model="uploadParams.dateInfo" class="block" type="month" size="large" format="YYYY-MM" value-format="YYYY-MM"/>
            </div>
        </HcDialog>
        <!--上传控件-->
        <HcUploadFile ref="uploadFileRef" :options="uploadOptions" :params="uploadParams" @success="uploadFileSuccess"/>
    </HcCard>
</template>

<script setup>
import {onActivated, ref} from 'vue'
import {getuserList} from "~api/other";
import mainApi from "~api/people/salary";
import {getTokenHeader} from "~src/api/request/header";
import {downloadBlob, getArrValue} from "js-fast-way";

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

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


const searchForm = ref({
    userId: null, month: 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, align: 'center'},
    {key: 'yaAllDays', name: '应出勤天数', width: 100, align: 'center'},
    {key: 'saAllDays', name: '实际出勤天数', width: 110, align: 'center'},
    {key: 'yearDays', name: '年假', width: 80, align: 'center'},
    {key: 'caLeaveDays', name: '调休假', width: 80, align: 'center'},
    {key: 'sickLeaveDays', name: '病假', width: 80, align: 'center'},
    {key: 'absenceDays', name: '事假', width: 80, align: 'center'},
    {key: 'basicSalary', name: '基本工资', width: 100, align: 'center'},
    {key: 'postSalary', name: '岗位工资', width: 100, align: 'center'},
    {key: 'meritSalary', name: '绩效工资', width: 100, align: 'center'},
    {key: 'bonusSalary', name: '奖励', width: 100, align: 'center'},
    {key: 'mealWance', name: '伙食补贴', width: 100, align: 'center'},
    {key: 'otherWance', name: '其他补贴', width: 100, align: 'center'},
    {key: 'attendDuction', name: '考勤扣款', width: 100, align: 'center'},
    {key: 'absDuction', name: '事假扣款', width: 100, align: 'center'},
    {key: 'otherDuction', name: '其他扣款', width: 100, align: 'center'},
    {key: 'grossPay', name: '应发工资', width: 100, align: 'center'},
    {key: 'personalTax', name: '代扣个人应交社保', width: 140, align: 'center'},
    {key: 'socialSec', name: '代扣代缴个税', width: 120, align: 'center'},
    {key: 'netSalary', name: '实发工资', width: 100, align: 'center'},
    {key: 'desc', name: '备注', minWidth: 140, align: 'center'},
]

//获取表格数据
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 downloadXlsx = async () => {
    const {error, disposition, res} = await mainApi.exportTemplate()
    if (!error && disposition) {
        downloadBlob(res, disposition)
    } else {
        window.$message?.error('数据异常')
    }
}


//导入数据弹窗
const importModal = ref(false)
const uploadTime = ref(null)

//上传配置
const uploadFileRef = ref(null)
const uploadParams = ref({
    dateInfo: null
})
const uploadOptions = {
    url: '/api/blade-control/userpayinfo/import-userpay',
    headers: getTokenHeader(),
    multiple: false,
    size: 50
}

//打开导入数据弹窗
const toImportTempClick = () => {
    uploadTime.value = null
    importModal.value = true
}

//导入数据弹窗保存
const importModalSave = () => {
    if(uploadParams.value.dateInfo) {
        uploadFileRef.value?.selectFile()
    } else {
        window.$message?.error('请先选择月份')
    }
}

// 文件上传成功的回调
const uploadFileSuccess = () => {
    window.$message?.success('导入成功')
    uploadFileRef.value?.setModalShow(false)
    importModalClose()
    getTableData()
}

//导入数据弹窗关闭
const importModalClose = () => {
    uploadTime.value = null
    importModal.value = false
}
</script>

<style lang='scss' scoped>

</style>
