<!-- Rekapitulasi Gangguan Per Posko -->
<template>
    <div>
        <DxDataGrid class="max-h-[calc(100vh-140px)]" :data-source="data" :show-column-lines="true" :show-row-lines="false"
            :show-borders="true" :row-alternation-enabled="true" :hover-state-enabled="true"
            @selection-changed="onSelectionChanged" :column-width="100" @exporting="onExporting"
            :allow-column-resizing="true" column-resizing-mode="widget" :word-wrap-enabled="true">
            <DxSelection mode="single" />
            <DxPaging :enabled="false" />
            <DxScrolling column-rendering-mode="virtual" mode="virtual" />
            <DxLoadPanel :enabled="true" />
            <DxSearchPanel :visible="true" :highlight-case-sensitive="true" />
            <DxExport :enabled="true" :formats="['pdf', 'xlsx', 'document']" :allow-export-selected-data="false" />
            <DxColumnFixing :enabled="true" />

            <DxColumn :width="60" alignment="center" data-field="no" caption="NO" css-class="custom-table-column" />
            <DxColumn :width="150" name="poskoGroup" alignment="center" data-field="posko" caption="Posko"
                css-class="custom-table-column" :group-index="0" />
            <DxColumn :width="150" alignment="center" data-field="posko" caption="Posko" css-class="custom-table-column" />
            <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
                <DxColumn :width="120" alignment="center" data-field="laporan.total" data-type="number" caption="Total"
                    css-class="custom-table-column" />
                <DxColumn alignment="center" caption="Sudah Selesai" css-class="custom-table-column">
                    <DxColumn :width="120" alignment="center" data-field="laporan.sudahSelesai.jml" data-type="number"
                        caption="Jml" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="laporan.sudahSelesai.persen" data-type="number"
                        caption="%" css-class="custom-table-column" />
                </DxColumn>
                <DxColumn alignment="center" caption="Belum Selesai" css-class="custom-table-column">
                    <DxColumn :width="120" alignment="center" data-field="laporan.belumSelesai.jml" data-type="number"
                        caption="Jml" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="laporan.belumSelesai.persen" data-type="number"
                        caption="%" css-class="custom-table-column" />
                </DxColumn>
            </DxColumn>
            <DxColumn alignment="center" caption="Response Time" css-class="custom-table-column">
                <DxColumn alignment="center" caption="Menit" css-class="custom-table-column">
                    <DxColumn :width="120" alignment="center" data-field="responseTime.menit.total" data-type="number"
                        caption="Total" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="responseTime.menit.rataRata" data-type="number"
                        caption="Rata-Rata" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="responseTime.menit.max" data-type="number"
                        caption="Max" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="responseTime.menit.min" data-type="number"
                        caption="Min" css-class="custom-table-column" />
                </DxColumn>
                <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
                    <DxColumn :width="120" alignment="center" data-field="responseTime.laporan.lebihSla" data-type="number"
                        caption=">SLA" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="responseTime.laporan.kurangSla" data-type="number"
                        caption="≤SLA" css-class="custom-table-column" />
                </DxColumn>
            </DxColumn>
            <DxColumn alignment="center" caption="Recovery Time" css-class="custom-table-column">
                <DxColumn alignment="center" caption="Menit" css-class="custom-table-column">
                    <DxColumn :width="120" alignment="center" data-field="recoveryTime.menit.total" data-type="number"
                        caption="Total" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="recoveryTime.menit.rataRata" data-type="number"
                        caption="Rata-Rata" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="recoveryTime.menit.max" data-type="number"
                        caption="Max" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="recoveryTime.menit.min" data-type="number"
                        caption="Min" css-class="custom-table-column" />
                </DxColumn>
                <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
                    <DxColumn :width="120" alignment="center" data-field="recoveryTime.laporan.lebihSla" data-type="number"
                        caption=">SLA" css-class="custom-table-column" />
                    <DxColumn :width="120" alignment="center" data-field="recoveryTime.laporan.kurangSla" data-type="number"
                        caption="≤SLA" css-class="custom-table-column" />
                </DxColumn>
            </DxColumn>

            <DxSummary>
                <DxGroupItem :show-in-group-footer="true" column="no" display-format="Total" />
                <DxGroupItem :show-in-group-footer="true" column="laporan.total" summary-type="sum" display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="laporan.sudahSelesai.jml" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="laporan.sudahSelesai.persen" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="laporan.belumSelesai.jml" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="laporan.belumSelesai.persen" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="responseTime.menit.total" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="responseTime.menit.rataRata" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="responseTime.menit.max" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="responseTime.menit.min" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="responseTime.laporan.lebihSla" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="responseTime.laporan.kurangSla" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="recoveryTime.menit.total" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="recoveryTime.menit.rataRata" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="recoveryTime.menit.max" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="recoveryTime.menit.min" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="recoveryTime.laporan.lebihSla" summary-type="sum"
                    display-format="{0}" />
                <DxGroupItem :show-in-group-footer="true" column="recoveryTime.laporan.kurangSla" summary-type="sum"
                    display-format="{0}" />

                <DxTotalItem summary-type="sum" display-format="Total" show-in-column="no" css-class="text-white" />
                <DxTotalItem summary-type="sum" display-format="{0}" show-in-column="laporan.total" column="laporan.total"
                    css-class="text-white" />
                <DxTotalItem column="laporan.sudahSelesai.jml" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="laporan.sudahSelesai.persen" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="laporan.belumSelesai.jml" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="laporan.belumSelesai.persen" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="responseTime.menit.total" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="responseTime.menit.rataRata" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="responseTime.menit.max" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="responseTime.menit.min" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="responseTime.laporan.lebihSla" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="responseTime.laporan.kurangSla" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="recoveryTime.menit.total" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="recoveryTime.menit.rataRata" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="recoveryTime.menit.max" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="recoveryTime.menit.min" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="recoveryTime.laporan.lebihSla" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
                <DxTotalItem column="recoveryTime.laporan.kurangSla" summary-type="sum" display-format="{0}"
                    css-class="text-white" />
            </DxSummary>

        </DxDataGrid>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useFiltersStore } from '@/stores/filters'
import { DxDataGrid } from 'devextreme-vue'
import { DxColumn, DxColumnFixing, DxExport, DxGroupItem, DxLoadPanel, DxPaging, DxScrolling, DxSearchPanel, DxSelection, DxSummary, DxTotalItem } from 'devextreme-vue/data-grid'
import { jsPDF } from 'jspdf'
import { exportDataGrid as exportToPdf } from 'devextreme/pdf_exporter'
import { exportDataGrid as exportToExcel } from 'devextreme/excel_exporter'
import { saveAs } from 'file-saver'
import { Workbook } from 'exceljs'

const onExporting = (e: any) => {
    if (e.format === 'pdf') {
        const doc = new jsPDF()

        exportToPdf({
            jsPDFDocument: doc,
            component: e.component,
            indent: 5,
        }).then(() => {
            doc.save(`.pdf`)
        })
    } else {
        const workbook = new Workbook()
        const worksheet = workbook.addWorksheet('Employees')

        exportToExcel({
            component: e.component,
            worksheet,
            autoFilterEnabled: true,
        }).then(() => {
            workbook.xlsx.writeBuffer().then((buffer: any) => {
                saveAs(new Blob([buffer], { type: 'application/octet-stream' }), 'DataGrid.xlsx')
            })
        })

        e.cancel = true
    }
}

const onSelectionChanged = ({ selectedRowsData }: any) => {
    const data = selectedRowsData[0]
    console.log(data)
}

const poskoOptions = ['POSKO A', 'POSKO B', 'POSKO C', 'POSKO D', 'POSKO E'];

const createDummy = () => {
    var dummy = data.value;
    for (let i = 1; i <= 50; i++) {
        const randomPosko = poskoOptions[Math.floor(Math.random() * poskoOptions.length)];

        const totalLaporan = Math.floor(Math.random() * 21);
        const laporanSelesai = Math.floor(Math.random() * (totalLaporan + 1));
        const laporanBelumSelesai = totalLaporan - laporanSelesai;

        const responseTimeTotal = Math.floor(Math.random() * 31);
        const responseTimeRataRata = (Math.random() * 21) + 10;
        const responseTimeMax = responseTimeRataRata + Math.floor(Math.random() * 11);
        const responseTimeMin = responseTimeRataRata - Math.floor(Math.random() * 11);

        const responseTimeLebihSla = Math.floor(Math.random() * (laporanSelesai + 1));
        const responseTimeKurangSla = laporanSelesai - responseTimeLebihSla;

        const recoveryTimeTotal = Math.floor(Math.random() * 31);
        const recoveryTimeRataRata = (Math.random() * 21) + 10;
        const recoveryTimeMax = recoveryTimeRataRata + Math.floor(Math.random() * 11);
        const recoveryTimeMin = recoveryTimeRataRata - Math.floor(Math.random() * 11);

        const recoveryTimeLebihSla = Math.floor(Math.random() * (laporanSelesai + 1));
        const recoveryTimeKurangSla = laporanSelesai - recoveryTimeLebihSla;

        const entry = {
            no: i,
            posko: randomPosko,
            laporan: {
                total: totalLaporan,
                sudahSelesai: {
                    jml: laporanSelesai,
                    persen: (laporanSelesai / totalLaporan) * 100,
                },
                belumSelesai: {
                    jml: laporanBelumSelesai,
                    persen: (laporanBelumSelesai / totalLaporan) * 100,
                },
            },
            responseTime: {
                menit: {
                    total: responseTimeTotal,
                    rataRata: responseTimeRataRata,
                    max: responseTimeMax,
                    min: responseTimeMin,
                },
                laporan: {
                    lebihSla: responseTimeLebihSla,
                    kurangSla: responseTimeKurangSla,
                },
            },
            recoveryTime: {
                menit: {
                    total: recoveryTimeTotal,
                    rataRata: recoveryTimeRataRata,
                    max: recoveryTimeMax,
                    min: recoveryTimeMin,
                },
                laporan: {
                    lebihSla: recoveryTimeLebihSla,
                    kurangSla: recoveryTimeKurangSla,
                },
            }
        };

        dummy.push(entry);
    }
    data.value = dummy;
}

const data = ref<any[]>([])

onMounted(() => {
    const filters = useFiltersStore()

    filters.setConfig({
        type: 'type-8',
        reportButton: true
    })
    createDummy()
})
</script>