<script setup>
import LTitle from "@/components/LTitle.vue";

// const props = defineProps({
//   data: {
//     type: Object,
//     required: true,
//   },
// })
// const { data } = props
const data = ref({
    swift_number: "999333_20181029143459_23453A4E0",
    code: "00",
    flag_specialList_c: "1",
    sl_id_court_bad: "0",
    sl_id_court_executed: "0",
    sl_id_bank_bad: "0",
    sl_id_bank_overdue: "0",
    sl_id_bank_lost: "0",
    sl_id_nbank_bad: "0",
    sl_id_nbank_overdue: "0",
    sl_id_nbank_lost: "0",
    sl_id_nbank_nsloan_bad: "0",
    sl_id_nbank_nsloan_overdue: "0",
    sl_id_nbank_nsloan_lost: "0",
    sl_id_nbank_sloan_bad: "0",
    sl_id_nbank_sloan_overdue: "0",
    sl_id_nbank_sloan_lost: "0",
    sl_id_nbank_cons_bad: "0",
    sl_id_nbank_cons_overdue: "0",
    sl_id_nbank_cons_lost: "0",
    sl_id_nbank_finlea_bad: "0",
    sl_id_nbank_finlea_overdue: "0",
    sl_id_nbank_finlea_lost: "0",
    sl_id_nbank_autofin_bad: "0",
    sl_id_nbank_autofin_overdue: "0",
    sl_id_nbank_autofin_lost: "0",
    sl_id_nbank_other_bad: "0",
    sl_id_nbank_other_overdue: "0",
    sl_id_nbank_other_lost: "0",
    sl_id_court_bad_time: "0",
    sl_id_court_executed_time: "1",
    sl_id_bank_bad_time: "1",
    sl_id_bank_overdue_time: "2",
    sl_id_bank_lost_time: "0",
    sl_id_nbank_bad_time: "0",
    sl_id_nbank_overdue_time: "1",
    sl_id_nbank_lost_time: "1",
    sl_id_nbank_nsloan_bad_time: "0",
    sl_id_nbank_nsloan_overdue_time: "1",
    sl_id_nbank_nsloan_lost_time: "1",
    sl_id_nbank_sloan_bad_time: "0",
    sl_id_nbank_sloan_overdue_time: "1",
    sl_id_nbank_sloan_lost_time: "1",
    sl_id_nbank_cons_bad_time: "1",
    sl_id_nbank_cons_overdue_time: "0",
    sl_id_nbank_cons_lost_time: "1",
    sl_id_nbank_finlea_bad_time: "1",
    sl_id_nbank_finlea_overdue_time: "2",
    sl_id_nbank_finlea_lost_time: "0",
    sl_id_nbank_autofin_bad_time: "1",
    sl_id_nbank_autofin_overdue_time: "0",
    sl_id_nbank_autofin_lost_time: "2",
    sl_id_nbank_other_bad_time: "0",
    sl_id_nbank_other_overdue_time: "0",
    sl_id_nbank_other_lost_time: "0",
    sl_id_court_bad_allnum: "1",
    sl_id_court_executed_allnum: "1",
    sl_id_bank_bad_allnum: "1",
    sl_id_bank_overdue_allnum: "2",
    sl_id_bank_lost_allnum: "5",
    sl_id_nbank_bad_allnum: "6",
    sl_id_nbank_overdue_allnum: "1",
    sl_id_nbank_lost_allnum: "1",
    sl_id_nbank_nsloan_bad_allnum: "7",
    sl_id_nbank_nsloan_overdue_allnum: "1",
    sl_id_nbank_nsloan_lost_allnum: "1",
    sl_id_nbank_sloan_bad_allnum: "1",
    sl_id_nbank_sloan_overdue_allnum: "1",
    sl_id_nbank_sloan_lost_allnum: "1",
    sl_id_nbank_cons_bad_allnum: "1",
    sl_id_nbank_cons_overdue_allnum: "9",
    sl_id_nbank_cons_lost_allnum: "1",
    sl_id_nbank_finlea_bad_allnum: "1",
    sl_id_nbank_finlea_overdue_allnum: "2",
    sl_id_nbank_finlea_lost_allnum: "4",
    sl_id_nbank_autofin_bad_allnum: "1",
    sl_id_nbank_autofin_overdue_allnum: "8",
    sl_id_nbank_autofin_lost_allnum: "2",
    sl_id_nbank_other_bad_allnum: "5",
    sl_id_nbank_other_overdue_allnum: "7",
    sl_id_nbank_other_lost_allnum: "2",
    sl_cell_bank_bad: "0",
    sl_cell_bank_overdue: "0",
    sl_cell_bank_lost: "0",
    sl_cell_nbank_bad: "0",
    sl_cell_nbank_overdue: "0",
    sl_cell_nbank_lost: "0",
    sl_cell_nbank_nsloan_bad: "0",
    sl_cell_nbank_nsloan_overdue: "0",
    sl_cell_nbank_nsloan_lost: "0",
    sl_cell_nbank_sloan_bad: "0",
    sl_cell_nbank_sloan_overdue: "0",
    sl_cell_nbank_sloan_lost: "0",
    sl_cell_nbank_cons_bad: "0",
    sl_cell_nbank_cons_overdue: "0",
    sl_cell_nbank_cons_lost: "0",
    sl_cell_nbank_finlea_bad: "0",
    sl_cell_nbank_finlea_overdue: "0",
    sl_cell_nbank_finlea_lost: "0",
    sl_cell_nbank_autofin_bad: "0",
    sl_cell_nbank_autofin_overdue: "0",
    sl_cell_nbank_autofin_lost: "0",
    sl_cell_nbank_other_bad: "0",
    sl_cell_nbank_other_overdue: "0",
    sl_cell_nbank_other_lost: "0",
    sl_cell_bank_bad_time: "1",
    sl_cell_bank_overdue_time: "2",
    sl_cell_bank_lost_time: "0",
    sl_cell_nbank_bad_time: "0",
    sl_cell_nbank_overdue_time: "1",
    sl_cell_nbank_lost_time: "1",
    sl_cell_nbank_nsloan_bad_time: "0",
    sl_cell_nbank_nsloan_overdue_time: "1",
    sl_cell_nbank_nsloan_lost_time: "1",
    sl_cell_nbank_sloan_bad_time: "0",
    sl_cell_nbank_sloan_overdue_time: "1",
    sl_cell_nbank_sloan_lost_time: "1",
    sl_cell_nbank_cons_bad_time: "1",
    sl_cell_nbank_cons_overdue_time: "0",
    sl_cell_nbank_cons_lost_time: "1",
    sl_cell_nbank_finlea_bad_time: "1",
    sl_cell_nbank_finlea_overdue_time: "2",
    sl_cell_nbank_finlea_lost_time: "0",
    sl_cell_nbank_autofin_bad_time: "1",
    sl_cell_nbank_autofin_overdue_time: "0",
    sl_cell_nbank_autofin_lost_time: "2",
    sl_cell_nbank_other_bad_time: "0",
    sl_cell_nbank_other_overdue_time: "0",
    sl_cell_nbank_other_lost_time: "0",
    sl_cell_bank_bad_allnum: "1",
    sl_cell_bank_overdue_allnum: "2",
    sl_cell_bank_lost_allnum: "3",
    sl_cell_nbank_bad_allnum: "5",
    sl_cell_nbank_overdue_allnum: "1",
    sl_cell_nbank_lost_allnum: "1",
    sl_cell_nbank_nsloan_bad_allnum: "3",
    sl_cell_nbank_nsloan_overdue_allnum: "1",
    sl_cell_nbank_nsloan_lost_allnum: "1",
    sl_cell_nbank_sloan_bad_allnum: "7",
    sl_cell_nbank_sloan_overdue_allnum: "1",
    sl_cell_nbank_sloan_lost_allnum: "1",
    sl_cell_nbank_cons_bad_allnum: "1",
    sl_cell_nbank_cons_overdue_allnum: "8",
    sl_cell_nbank_cons_lost_allnum: "1",
    sl_cell_nbank_finlea_bad_allnum: "1",
    sl_cell_nbank_finlea_overdue_allnum: "2",
    sl_cell_nbank_finlea_lost_allnum: "4",
    sl_cell_nbank_autofin_bad_allnum: "1",
    sl_cell_nbank_autofin_overdue_allnum: "6",
    sl_cell_nbank_autofin_lost_allnum: "2",
    sl_cell_nbank_other_bad_allnum: "7",
    sl_cell_nbank_other_overdue_allnum: "6",
    sl_cell_nbank_other_lost_allnum: "9",
});
console.log("spp data", data);
const bankLost = ref([]);
const lost = ref([]);
function totalStats(data) {
    const result = {};

    const keys = {
        bank: "银行失联异常",
        nbank_nsloan: "网络小额贷款",
        nbank_autofin: "汽车金融",
        nbank_sloan: "地方小戴",
        nbank_finlea: "融资租赁",
        nbank_cons: "消费金融",
        nbank_other: "其他风险",
    };
    const levels = ["bad", "overdue"];
    // 遍历每个时间维度
    for (const key of Object.keys(keys)) {
        result[key] = {
            label: keys[key],
            num: 0,
            date: null,
        };
        let time = null;
        for (const i of levels) {
            const numfield = `sl_id_${key}_${i}`;
            if (data[numfield] !== undefined) {
                result[key].num += Number(data[numfield] || 0);
            }
            const timefield = `sl_id_${key}_${i}_time`;
            const timeData = data[timefield];
            if (timeData !== 0 || time === null || timeData < time) {
                time = timeData;
            }
        }
        result[key].date = time;
    }

    return result;
}
function lostSotalStats(data) {
    const result = {};

    const keys = {
        bank: "银行失联异常",
        nbank_nsloan: "网络小额贷款",
        nbank_autofin: "汽车金融",
        nbank_sloan: "地方小戴",
        nbank_finlea: "融资租赁",
        nbank_cons: "消费金融",
        nbank_other: "其他风险",
    };
    const levels = ["lost"];
    for (const key of Object.keys(keys)) {
        result[key] = {
            label: keys[key],
            num: 0,
            date: null,
        };
        let time = null;
        for (const i of levels) {
            const numfield = `sl_id_${key}_${i}`;
            if (data[numfield] !== undefined) {
                result[key].num += Number(data[numfield] || 0);
            }
            const timefield = `sl_id_${key}_${i}_time`;
            const timeData = data[timefield];
            if (timeData !== 0 || time === null || timeData < time) {
                time = timeData;
            }
        }
        result[key].date = time;
    }

    return result;
}
onMounted(() => {
    const result = totalStats(data.value);
    const lostResult = lostSotalStats(data.value);
    bankLost.value = result;
    lost.value = lostResult;
});
</script>

<template>
    <div class="card">
        <div class="flex flex-col gap-y-4">
            <LTitle title="一般异常行为" type="blue-green" />
            <LTable :data="bankLost" type="blue-green">
                <template #header>
                    <th class="border px-4 py-2">类型</th>
                    <th class="border px-4 py-2">异常次数</th>
                    <th class="border px-4 py-2">最次距今时间</th>
                </template>

                <template #default="{ row }">
                    <td class="border px-4 py-2">
                        {{ row.label }}
                    </td>
                    <td class="border px-4 py-2">
                        {{ row.num }}
                    </td>
                    <td class="border px-4 py-2">
                        {{ row.date !== "0" ? `近${row.date}年内` : "-" }}
                    </td>
                </template>
            </LTable>
            <LTitle title="高风险异常行为" type="blue-green" />
            <LTable :data="lost" type="blue-green">
                <template #header>
                    <th class="border px-4 py-2">类型</th>
                    <th class="border px-4 py-2">异常次数</th>
                    <th class="border px-4 py-2">最次距今时间</th>
                </template>

                <template #default="{ row }">
                    <td class="border px-4 py-2">
                        {{ row.label }}
                    </td>
                    <td class="border px-4 py-2">
                        {{ row.num }}
                    </td>
                    <td class="border px-4 py-2">
                        {{ row.date !== "0" ? `近${row.date}年内` : "-" }}
                    </td>
                </template>
            </LTable>
        </div>
    </div>
</template>

<style lang="scss" scoped></style>
