<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="34rem" isToBody :show="importModal" title="导入数据"
                  @close="importModalClose" footer isRowFooter>
            <el-form label-position="top" label-width="auto" :model="formModel" size="large">
                <el-form-item label="请选择导入薪酬所时间属范围：">
                    <el-date-picker v-model="formModel.time" type="daterange" start-placeholder="起始时间" end-placeholder="结束时间" :default-time="defaultTime"/>
                </el-form-item>
            </el-form>
            <template #rightRowFooter>
                <HcUploadFile ref="HcUploadFileRef" :options="UploadFileOptions" @progress="HcUploadFileProgress" @success="HcUploadFileSuccess" @change="HcUploadFileChange">
                    <el-button type="primary" size="large" class="ml-2">
                        <HcIcon name="folder-upload"/>
                        <span>上传excel</span>
                    </el-button>
                </HcUploadFile>
                <el-button type="primary" @click="importModalClose" size="large" class="ml-2">
                    <HcIcon name="close-circle"/>
                    <span>取消</span>
                </el-button>
            </template>
        </HcDialog>
    </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 defaultTime = ref([
    new Date(2000, 1, 1, 0, 0, 0),
    new Date(2000, 2, 1, 23, 59, 59),
])

//导入数据弹窗
const importModal = ref(false)
const importModalClose = () => {
    importModal.value = false
}
const formModel = ref({})

const toImportTempClick = () => {
    importModal.value = true

}
//上传配置
const UploadFileOptions = {
    //此处设置header
    headers: getTokenHeader(),
}

// 文件上传进度
const HcUploadFileProgress = (res) => {
    console.log('文件上传进度', res)
}

const HcUploadFileRef = ref(null)
// 文件上传成功的回调
const HcUploadFileSuccess = (res) => {
    HcUploadFileRef.value?.setModalShow(false);
    importModal.value = false
    getTableData()

}

// 文件全部上传成功
const HcUploadFileChange = () => {
    console.log('文件全部上传成功')
}
</script>
<style lang='scss' scoped>
</style>
