<template>
  <div class="mt-4 lg:mt-6 max-w-7xl">
    <h1 class="text-xl font-medium md:text-2xl text-dark">Laporan Pengaduan PLN Mobile</h1>
  </div>
  <DxDataGrid
    :allow-column-reordering="true"
    class="max-h-[calc(100vh-140px)] mb-10"
    :data-source="data"
    :show-column-lines="true"
    :show-row-lines="false"
    :show-borders="true"
    :row-alternation-enabled="true"
    :hover-state-enabled="true"
    @selection-changed="onDataSelectionChanged"
    @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" />
    <DxSearchPanel :visible="true" :highlight-case-sensitive="true" />
    <DxExport
      :enabled="true"
      :formats="['pdf', 'xlsx', 'document']"
      :allow-export-selected-data="false"
    />
    <DxColumnFixing :enabled="true" />

    <DxColumn
      alignment="center"
      data-field="nama_ulp"
      caption="Nama Unit"
      css-class="custom-table-column"
      cell-template="formatText"
    />
    <DxColumn alignment="center" caption="Total WO PLN Mobile" css-class="custom-table-column">
      <DxColumn
        :width="170"
        alignment="center"
        data-field="wo_total"
        data-type="number"
        caption="a"
        css-class="custom-table-column"
        cell-template="formatNumber"
      />
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="Total Pengaduan Yang Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn
        :width="170"
        alignment="center"
        data-field="total_anomali_marking"
        data-type="number"
        caption="b"
        css-class="custom-table-column"
        cell-template="formatNumber"
      />
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="% Pengaduan Yang Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn
        :width="170"
        alignment="center"
        data-field="persen_anomali_marking"
        data-type="number"
        caption="c=b/a"
        css-class="custom-table-column"
        cell-template="formatPercentage"
      />
    </DxColumn>

    <template #formatNumber="{ data }">
      <p class="text-right cursor-pointer" @click="showData()">
        {{
          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" @click="showData()">
        {{ isNumber(data.text) ? formatPercentage(data.text) : data.text }}
      </p>
    </template>

    <template #formatText="{ data }">
      <p class="text-left cursor-pointer" @click="showData()">
        {{ data.text }}
      </p>
    </template>

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

<script setup lang="ts">
import { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxPaging,
  DxScrolling,
  DxSearchPanel,
  DxSelection
} 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 { computed, ref } from 'vue'
import { formatNumber, formatPercentage, isNumber } from '@/utils/numbers'
import { formatWaktu } from '@/components/Form/FiltersType/reference'
const props = defineProps({
  data: Array as () => any[]
})
const data = computed(() => props.data)
const dataSub = ref<any[]>([])
const dataSelected = ref<any>({})
const dataSubSelected = ref<any>({})
const showDetail = ref(false)
const showData = () => {
  showDetail.value = true
}
const closeDetail = () => {
  showDetail.value = false
}

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) => {
  const dataSelected = selectedRowsData[0]
  console.log(data)
}
</script>
