<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
    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"
    :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
      :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"
      name="namaUnit"
      css-class="custom-table-column"
      cell-template="formatText"
      :group-index="0"
    />
    <DxColumn alignment="center" caption="Total WO" css-class="custom-table-column">
      <DxColumn alignment="center" caption="CC 123" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_cc123"
          data-type="number"
          caption="a"
          css-class="custom-table-column"
          cell-template="formatNumber"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_pln_mobile"
          data-type="number"
          caption="b"
          css-class="custom-table-column"
          cell-template="formatNumber"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="Loket" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_loket"
          data-type="number"
          caption="c"
          css-class="custom-table-column"
          cell-template="formatNumber"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="Lainnya" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_lainnya"
          data-type="number"
          caption="d"
          css-class="custom-table-column"
          cell-template="formatNumber"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="Total" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_total"
          data-type="number"
          caption="e=a+b+c+d"
          css-class="custom-table-column"
          cell-template="formatNumber"
        />
      </DxColumn>
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="Total Pengaduan Yang Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn alignment="center" caption="f" 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="g" 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="h=f+g" 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="% Pengaduan Yang Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn alignment="center" caption="i=f/e" 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="j=g/e" 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="k=i+j" 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" @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>

    <DxSummary>
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="wo_cc123"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="wo_pln_mobile"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="wo_loket"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="wo_lainnya"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="wo_total"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
      <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(parseFloat(e.value.toString()))"
      />
    </DxSummary>
  </DxDataGrid>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxGroupItem,
  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 { formatWaktu } from '@/components/Form/FiltersType/reference'
import { formatNumber, formatPercentage, isNumber } from '@/utils/numbers'
const position = { of: '#data' }
const showIndicator = ref(true)
const loading = ref(computed(() => props.loading))
const shading = ref(true)
const showPane = ref(true)
const props = defineProps({
  data: Array as () => any[],
  loading: {
    type: Boolean,
    default: false
  }
})
const showDialog = ref(false)
const data = computed(() => props.data)
const dataSelected = ref()
const dataSub = ref([])
const dataSubSelected = ref()
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>
