<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="合同名称" prop="contract_name">
                    <el-input class="w-[280px]" v-model="queryParams.contract_name" clearable placeholder="请输入合同名称" />
                </el-form-item>
                <el-form-item label="合同编号" prop="contract_num">
                    <el-input class="w-[280px]" v-model="queryParams.contract_num" clearable placeholder="请输入合同编号" />
                </el-form-item>
                <el-form-item label="甲方单位" prop="part_b">
                    <el-input class="w-[280px]" v-model="queryParams.part_b" clearable placeholder="请输入甲方单位" />
                </el-form-item>
                <el-form-item label="合同类型" prop="contract_type">
                    <el-input class="w-[280px]" v-model="queryParams.contract_type" clearable placeholder="请输入合同类型" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table border :data="pager.lists" show-summary :summary-method="getSummaries">
                    <el-table-column label="合同名称" prop="contract_name" :render-header="pager.calcWidth" />
                    <el-table-column :render-header="pager.calcWidth" label="合同编号" prop="contract_code" />
                    <el-table-column :render-header="pager.calcWidth" label="合同类型" prop="contract_type_text"
                        show-overflow-tooltip />
                    <el-table-column label="业务性质" :render-header="pager.calcWidth" prop="business_nature_text"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="签约单位" prop="part_a_name"
                        show-overflow-tooltip />
                    <el-table-column label="签订部门" :render-header="pager.calcWidth" prop="signed_dept_name"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="合同签订金额" prop="signed_amount"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="已开票金额" prop="total_invoice_amount"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="未开票金额" prop="not_total_invoice_amount"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="开票已回款金额" prop="invoice_has_refund_amount"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="开票未回款金额" prop="invoice_not_refund_amount"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="合同未回款金额" prop="notHk" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ (+row.signed_amount) - (+row.total_refund_amount) }}
                        </template>
                    </el-table-column>
                    <el-table-column :render-header="pager.calcWidth" label="回款日期" prop="refund_date"
                        show-overflow-tooltip />
                    <el-table-column label="操作" width="150" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['contract.contract_negotiation/delete']" type="primary" link
                                @click="handleDetail(row)">
                                详情
                            </el-button>
                            <el-button v-perms="['contract.contract_negotiation/detail']" link @click="handleDown(row.id)">
                                下载
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <detailPage v-if="showDetail" ref="detailRef" @close="showDetail = false" :detailConfig="targetDetail" />
    </div>
</template>

<script lang="ts" setup name="financialRefundLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiContractLedgerLists, apiMarketingContract, apiMarketingContractdownload } from '@/api/contract_ledger'
import feedback from '@/utils/feedback'
import supervision_project from "@/views/supervision_project/detail.js"
import manage_project from "@/views/manage_project/detail.js"
import consult_project from "@/views/consult_project/detail.js"
import cost_project from "@/views/cost_projects/detail.js"
import {
    apiSupervisionProjectDetail
} from '@/api/supervision_project'
import { apiManageProjectDetail } from '@/api/manage_project'
import { apiConsultProjectDetail } from '@/api/consult_project'
import { apiCostProjectDetail } from '@/api/cost_project'

const targetDetail = ref({})
const detailRef = shallowRef<InstanceType<typeof EditPopup>>()
// // 是否显示编辑框
const showDetail = ref(false)

// 查询条件
const queryParams = reactive({
    contract_name: "",
    part_b: '',
    contract_type: '',
})


// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiContractLedgerLists,
    params: queryParams
})


const open = async (obj, api, id) => {
    targetDetail.value = obj
    let res = await api({ id })
    showDetail.value = true
    await nextTick()
    detailRef.value?.open()
    detailRef.value?.setFormData(res)
}

// 详情
const handleDetail = async (row: any) => {

    if (!row.project_id) {
        return feedback.msgError("暂无项目信息")
    }
    if (row.business_nature == 1) {
        open(supervision_project, apiSupervisionProjectDetail, row.project_id)

    }
    else if (row.business_nature == 2) {
        open(manage_project, apiManageProjectDetail, row.project_id)

    }
    else if (row.business_nature == 3) {
        open(consult_project, apiConsultProjectDetail, row.project_id)

    }
    else if (row.business_nature == 4) {
        open(cost_project, apiCostProjectDetail, row.project_id)
    }
    else {
        feedback.msgError("暂无项目信息")
    }
}


const getSummaries = (param) => {
    const { columns, data } = param;
    const sumPropList = ['total_invoice_amount', 'not_total_invoice_amount', 'invoice_has_refund_amount', 'invoice_not_refund_amount', 'notHk']
    const sums: string[] = [];
    columns.forEach((item, index) => {
        if (index === 0) {
            sums[index] = '总价';
        }
        else if (sumPropList.includes(item.property)) {
            const values = data.map((items) => {
                if (item.property == 'notHk') {
                    return Number(items.signed_amount) - Number(items.total_refund_amount)
                } else {
                    return Number(items[item.property])
                }
            });
            sums[index] = values.reduce((prev, curr) =>
                Number(prev) + Number(curr), 0
            );
            sums[index] += ' 元';
        }
        else {
            sums[index] = '';
        }
    })
    return sums;
}

// 下载
const handleDown = async (id: number | any[]) => {
    let res = await apiMarketingContractdownload({ id })
    const a = document.createElement('a')
    a.href = res.url
    a.click()
}
getLists()
</script>
