<template>
    <div class="electronicContractBill">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
            <el-form-item label="账单年月" prop="billDate">
                <el-date-picker v-model="queryParams.billDate" type="month" placeholder="账单年月" value-format="YYYY-MM" />
            </el-form-item>
            <el-form-item label="派遣" prop="baseCode">
                <el-select
                    v-model="queryParams.baseCode"
                    placeholder="请选择"
                    style="width: 240px"
                    filterable
                >
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="账单年月" prop="billYM" />
            <el-table-column label="累计发放人次" prop="totalPerson" />
            <el-table-column label="服务费总额" prop="totalFee" />
            <el-table-column label="状态" align="center">
                <template #default="{ row }">
                    <el-tag type="warning" v-if="row.state == 'none'">未结算</el-tag>
                    <el-tag v-if="row.state == 'settle'">已结算</el-tag>
                    <el-tag type="success" v-if="row.state == 'payment'">已付款</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="240" align="center">
                <template #default="{ row }">
                    <el-button v-if="row.state == 'none'" link type="primary" icon="Memo"
                        @click="handleSettle(row)">结算</el-button>
                    <el-button v-if="row.state == 'payment' && row.platState == 'settle'" link type="primary" icon="Tickets" @click="handlePayment(row)">收款</el-button>
                    <el-button link type="primary" icon="Edit" @click="handleDetails(row)">明细</el-button>
                    <el-button link type="primary" icon="Download" @click="exportData(row)">导出</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.limit"
            @pagination="getList" />
        <el-dialog v-model="dialogFormVisible" title="明细" width="1000">
            <el-form :inline="true">
                <el-form-item label="批次号" prop="batch">
                    <el-input v-model="detailQueryParams.batch" placeholder="请输入批次号" style="width: 220px"
                        @keyup.enter="getDetailList" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="getDetailList">搜索</el-button>
                    <el-button icon="Refresh" @click="resetDetailQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="detailLoading" :data="detailList">
                <el-table-column label="账单年月" align="center" prop="billYM" />
                <el-table-column label="批次号" align="center" prop="batch" />
                <el-table-column label="发薪人数" align="center" prop="totalPerson" />
                <el-table-column label="服务费" align="center" prop="totalFee" />
            </el-table>
            <div style="position: relative;">
                <pagination v-show="detailTotal > 0" :total="detailTotal" v-model:page="detailQueryParams.page"
                    v-model:limit="detailQueryParams.limit" @pagination="getDetailList" />
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="dialogFormVisible = false">确定</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import { ref } from "vue";
import { payBillList, payBillBatchList, payBillPayment, payBillSettle,queryAllDispatch } from "@/api/bill"
const { proxy } = getCurrentInstance();
const options = ref([]); // 派遣下拉
const list = ref([{}]);
const loading = ref(false);
const total = ref(0); // 分页
const queryParams = ref({
    billDate: '',
    baseCode: '',
    page: 1,
    limit: 10
}); // 查询参数

const detailList = ref([]);
const detailLoading = ref(false);
const dialogFormVisible = ref(false); // 弹窗
const detailTotal = ref(0);
const detailQueryParams = ref({
    page: 1,
    limit: 10,
    baseCode: '',
    billDate: '',
    batch: '',
}) // 弹窗列表查询条件
/** 查询明细 */
function handleDetails(row) {
    dialogFormVisible.value = true;
    detailLoading.value = true;
    detailTotal.value = 0;
    detailList.value = [];
    detailQueryParams.value.page = 1;
    detailQueryParams.value.limit = 10;
    detailQueryParams.value.baseCode = row.baseCode;
    detailQueryParams.value.billDate = row.billYM;
    detailQueryParams.value.batch = '';
    getDetailList()
}
/** 查询明细 */
function getDetailList() {
    payBillBatchList(detailQueryParams.value).then(res => {
        detailLoading.value = false;
        detailTotal.value = res.count;
        detailList.value = res.data;
    })
}
// 重置弹窗明细查询条件
function resetDetailQuery() {
    detailQueryParams.value.name = '';
    detailQueryParams.value.idNo = '';
    getDetailList()
}
// 导出
function exportData(row) {
    proxy.download("/djl-salary/salary/pay/bill/export", { billDate: row.billYM,baseCode: row.baseCode}, `发薪账单导出结果.xlsx`);
}
// 结算
function handleSettle(row) {
    proxy.$modal.confirm('是否确认结算当前账单？').then(function () {
        return payBillSettle({
            id: row.id,
            baseCode: row.baseCode,
        });
    }).then((res) => {
        getList();
        proxy.$modal.msgSuccess(res.code);
    }).catch(() => { });
}
// 收款
function handlePayment(row) {
    proxy.$modal.confirm('是否确认收款？').then(function () {
        return payBillPayment({
            id: row.id,
            baseCode: row.baseCode,
        });
    }).then((res) => {
        getList();
        proxy.$modal.msgSuccess(res.code);
    }).catch(() => { });
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    queryParams.value.limit = 10;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    queryParams.value = {
        billDate: '',
        page: 1,
        limit: 10
    }
    handleQuery();
}
/** 查询预支工资账单列表 */
function getList() {
    loading.value = true;
    payBillList(queryParams.value).then(response => {
        list.value = response.data;
        total.value = response.count;
        loading.value = false;
    });
}
function getqueryAllDispatch() {
    queryAllDispatch().then(res => {
        res.data = res.data || []
        options.value = res.data.map(item => {
            return {
                value: item.baseCode,
                label: item.name
            }
        });
    })
}
getqueryAllDispatch();
getList();
</script>
<style lang="scss">
.electronicContractBill {
    padding: 20px;
}
</style>