<template>
  <div class="mt-4 lg:mt-6 max-w-7xl">
    <h1 class="text-xl font-medium md:text-2xl text-dark">Laporan Pengaduan Total</h1>
  </div>
  <DxDataGrid
    ref="dataGridRef"
    :allow-column-reordering="true"
    class="max-h-[calc(100vh-140px)] mb-10"
    :show-column-lines="true"
    :show-row-lines="false"
    :show-borders="true"
    :row-alternation-enabled="true"
    :hover-state-enabled="true"
    @selection-changed="onDataSelectionChanged"
    :column-width="100"
    @exporting="onExporting"
    :allow-column-resizing="true"
    column-resizing-mode="widget"
    :word-wrap-enabled="true"
    :data-source="data"
  >
    <DxGrouping expand-mode="rowClick" />
    <DxSelection mode="single" />
    <DxPaging :enabled="false" />
    <DxScrolling column-rendering-mode="virtual" mode="virtual" />
    <DxLoadPanel
      shading-color="rgba(0,0,0,0.4)"
      :position="position"
      :show-indicator="showIndicator"
      :show-pane="showPane"
      :shading="shading"
      v-if="loading"
      v-model:visible="loading"
      :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="170"
      alignment="center"
      data-field="nama_ulp"
      caption="Nama Unit"
      css-class="custom-table-column"
      cell-template="formatText"
    />
    <DxColumn
      :width="170"
      alignment="center"
      data-field="nama_ulp"
      caption="Nama Unit"
      css-class="custom-table-column"
      cell-template="formatText"
      name="namaUnit"
      :group-index="0"
    />
    <DxColumn alignment="center" caption="Total Petugas" css-class="custom-table-column">
      <DxColumn
        :width="170"
        alignment="center"
        data-field="total_petugas"
        caption="a"
        css-class="custom-table-column"
        cell-template="formatNumber"
      />
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="Total Petugas Yang Pengaduan Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn alignment="center" caption="b" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="anomali_pln_mobile_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="anomali_pln_mobile_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </DxColumn>
      </DxColumn>
      <DxColumn alignment="center" caption="CC 123" css-class="custom-table-column">
        <DxColumn alignment="center" caption="c" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="anomali_cc123_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="anomali_cc123_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </DxColumn>
      </DxColumn>
      <DxColumn alignment="center" caption="Total" css-class="custom-table-column">
        <DxColumn alignment="center" caption="d=b+c" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="total_anomali_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="total_anomali_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </DxColumn>
      </DxColumn>
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="% Petugas Yang Pengaduan Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn alignment="center" caption="e=b/a" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_anomali_pln_mobile_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_anomali_pln_mobile_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
        </DxColumn>
      </DxColumn>
      <DxColumn alignment="center" caption="CC 123" css-class="custom-table-column">
        <DxColumn alignment="center" caption="f=c/a" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_anomali_cc123_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_anomali_cc123_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
        </DxColumn>
      </DxColumn>
      <DxColumn alignment="center" caption="Total" css-class="custom-table-column">
        <DxColumn alignment="center" caption="g=e+f" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_anomali_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_anomali_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
        </DxColumn>
      </DxColumn>
    </DxColumn>

    <template #formatNumber="{ data }">
      <p class="text-right cursor-pointer">
        {{
          isNumber(data.text)
            ? data.column.caption == '%'
              ? formatPercentage(data.text)
              : formatNumber(data.text)
            : data.text
        }}
      </p>
    </template>

    <template #formatPercentage="{ data }">
      <p class="text-right cursor-pointer">
        {{ isNumber(data.text) ? formatPercentage(data.text) : data.text }}
      </p>
    </template>

    <template #formatText="{ data }">
      <p class="text-left cursor-pointer">
        {{ data.text }}
      </p>
    </template>

    <template #formatTime="{ data }">
      <p class="!text-right">
        {{ parseInt(data.text) ? formatWaktu(data.text) : '-' }}
      </p>
    </template>

    <DxSummary>
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="total_petugas"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_pln_mobile_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_pln_mobile_non_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_cc123_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_cc123_non_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="total_anomali_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="total_anomali_non_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_pln_mobile_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_pln_mobile_non_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_cc123_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_cc123_non_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_non_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
    </DxSummary>
  </DxDataGrid>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxGroupItem,
  DxGrouping,
  DxLoadPanel,
  DxPaging,
  DxScrolling,
  DxSearchPanel,
  DxSelection,
  DxSummary
} 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'
import { formatNumber, formatPercentage, isNumber } from '@/utils/numbers'
import { formatWaktu } from '@/components/Form/FiltersType/reference'

const position = { of: '#data' }
const showIndicator = ref(true)
const shading = ref(true)
const showPane = ref(true)
const props = defineProps({
  data: Array as () => any[],
  loading: {
    type: Boolean,
    default: false
  }
})
const data = computed(() => props.data)
const dataSub = ref<any[]>([])
const dataSelected = ref<any>({})
const dataSubSelected = ref<any>({})
const dialogDetail = ref(false)
const dataGridRef = ref<DxDataGrid | null>(null)
const clearSelection = () => {
  const dataGrid = dataGridRef.value!.instance!
  dataGrid.clearSelection()
}
const showDetail = () => {
  clearSelection()
  dialogDetail.value = true
}
const closeDialog = () => {
  dialogDetail.value = false
}
const loading = ref(computed(() => props.loading))

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 onDataSelectionChanged = ({ selectedRowsData }: any) => {
  if (selectedRowsData[0] != undefined) {
    dataSelected.value = selectedRowsData[0]
  }
  showDetail()
}
</script>
