<template> 
    <div class="electronicContractBill">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
            <el-form-item label="账单年月" prop="billYM">
                <el-date-picker v-model="queryParams.billYM" 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="totalTimes" />
            <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="平台状态" align="center">
                <template #default="{row}">
                <el-tag type="warning" v-if="row.platState == 'settle'">挂账</el-tag>
                <el-tag type="success" v-if="row.platState == 'payment'">已收款</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" 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="proId">
                    <el-select
                        v-model="detailQueryParams.proId"
                        placeholder="请选择"
                        filterable
                        style="width: 300px"
                    >
                    <el-option
                        v-for="item in projectData"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                    </el-option>
                    </el-select>
                </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="project" />
                <el-table-column label="签订合同人数" align="center" prop="totalTimes" />
                <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 { contractBillList, proContractBillList, contractBillPayment, contractBillSettle, queryAllDispatch } from "@/api/bill"
import { projectSelect } from "@/api/home/index";
const { proxy } = getCurrentInstance();
const projectData = ref([])
const list = ref([{}]);
const loading = ref(false);
const options = ref([]); // 派遣下拉
const total = ref(0); // 分页
const queryParams = ref({
    billYM: '',
    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: '',
    billYM: '',
}) // 弹窗列表查询条件
//查询所有项目
projectSelect().then(res => {
  projectData.value  = [{
    id:'',
    name: '请选择'
  },...res.data.map(item => {
    return {
      id:item.id,
      name: item.name,
      state: item.state
    }
  })]
})
/** 查询明细 */
function handleDetails(row) {
    dialogFormVisible.value = true;
    detailLoading.value = true;
    detailTotal.value = 0;
    detailList.value = [];
    detailQueryParams.value.page = 1;
    detailQueryParams.value.limit = 10;
    detailQueryParams.value.billYM = row.billYM;
    detailQueryParams.value.baseCode = row.baseCode;
    detailQueryParams.value.proId = ''
    getDetailList()
}
/** 查询明细 */
function getDetailList() {
    proContractBillList(detailQueryParams.value).then(res => {
        detailLoading.value = false;
        detailTotal.value = res.count;
        detailList.value = res.data;
    })
}
// 重置弹窗明细查询条件
function resetDetailQuery() {
    detailQueryParams.value.page = 1;
    detailQueryParams.value.limit = 10;
    detailQueryParams.value.proId = ''
    getDetailList()
}
// 导出
function exportData(row) {
    proxy.download("/djl-contract/contract/bill/export", { billYM: row.billYM }, `合同账单导出结果.xlsx`);
}
// 结算
function handleSettle(row) {
    proxy.$modal.confirm('是否确认结算当前账单？').then(function () {
        return contractBillSettle({
            id: row.id,
            baseCode: row.baseCode,
        });
    }).then((res) => {
        getList();
        proxy.$modal.msgSuccess(res.code);
    }).catch(() => { });
}
// 收款
function handlePayment(row) {
    proxy.$modal.confirm('是否确认收款？').then(function () {
        return contractBillPayment({
            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 = {
        billYM: '',
        page: 1,
        limit: 10
    }
    handleQuery();
}
/** 查询预支工资账单列表 */
function getList() {
    loading.value = true;
    contractBillList(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>