<!-- Rekapitulasi Gangguan/Jenis Gangguan SE 004 -->

<template>
  <Filters
    @reset-form="data = []"
    :report-button="true"
    @run-search="() => filterData(filters)"
    class="mb-4"
  >
    <Type1 @update:filters="(value) => (filters = value)" />
  </Filters>

  <div id="data">
    <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-model:visible="loadingData"
        :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
        css-class="custom-table-column"
        :width="60"
        alignment="center"
        :calculateCellValue="(item: any) => data.findIndex((i) => i == item) + 1"
        data-type="number"
        caption="No"
        cell-template="formatNumber"
      />
      <DxColumn
        :width="120"
        alignment="center"
        data-field="kode"
        caption="Kode"
        css-class="custom-table-column"
        cell-template="formatText"
      />
      <DxColumn
        :width="120"
        alignment="center"
        data-field="id_fasilitas"
        caption=""
        css-class="custom-table-column"
        cell-template="formatText"
        name="idFasilitasGroup"
        :group-index="0"
      />
      <DxColumn
        :width="150"
        alignment="center"
        data-field="sub_kelompok"
        caption="Sub Kelompok (Equipment)"
        css-class="custom-table-column"
        cell-template="formatText"
      />
      <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
        <DxColumn
          :width="120"
          alignment="center"
          data-field="total"
          data-type="number"
          caption="Total"
          css-class="custom-table-column"
          cell-template="formatNumber"
        />
        <DxColumn alignment="center" caption="Sudah Selesai" css-class="custom-table-column">
          <DxColumn
            :width="120"
            alignment="center"
            data-field="total_selesai"
            data-type="number"
            caption="Jml"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="70"
            alignment="center"
            data-field="persen_selesai"
            data-type="number"
            caption="%"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </DxColumn>
        <DxColumn alignment="center" caption="Belum Selesai" css-class="custom-table-column">
          <DxColumn
            :width="120"
            alignment="center"
            data-field="total_inproses"
            data-type="number"
            caption="Jml"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="70"
            alignment="center"
            data-field="persen_inproses"
            data-type="number"
            caption="%"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </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="total_durasi_response"
            data-type="number"
            caption="Total"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="avg_durasi_response"
            data-type="number"
            caption="Rata-Rata"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="max_durasi_response"
            data-type="number"
            caption="Max"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="min_durasi_response"
            data-type="number"
            caption="Min"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
        </DxColumn>
        <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
          <DxColumn
            :width="120"
            alignment="center"
            data-field="total_diatas_sla_response"
            data-type="number"
            caption=">SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="total_dibawah_sla_response"
            data-type="number"
            caption="≤SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </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="total_durasi_recovery"
            data-type="number"
            caption="Total"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="avg_durasi_recovery"
            data-type="number"
            caption="Rata-Rata"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="max_durasi_recovery"
            data-type="number"
            caption="Max"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="min_durasi_recovery"
            data-type="number"
            caption="Min"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
        </DxColumn>
        <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
          <DxColumn
            :width="120"
            alignment="center"
            data-field="total_diatas_sla_recovery"
            data-type="number"
            caption=">SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="120"
            alignment="center"
            data-field="total_dibawah_sla_recovery"
            data-type="number"
            caption="≤SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </DxColumn>
      </DxColumn>

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

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

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

      <DxSummary>
        <DxGroupItem
          :show-in-group-footer="true"
          column="No"
          display-format="Total"
          css-class="!text-left"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_selesai"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="persen_selesai"
          summary-type="avg"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatPercentage(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_inproses"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="persen_inproses"
          summary-type="avg"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatPercentage(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_durasi_response"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="avg_durasi_response"
          summary-type="avg"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="max_durasi_response"
          summary-type="max"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => (parseInt(e.value) ? formatWaktu(e.value) : '-')"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="min_durasi_response"
          summary-type="min"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => (parseInt(e.value) ? formatWaktu(e.value) : '-')"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_diatas_sla_response"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_dibawah_sla_response"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_durasi_recovery"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="avg_durasi_recovery"
          summary-type="avg"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="max_durasi_recovery"
          summary-type="max"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => (parseInt(e.value) ? formatWaktu(e.value) : '-')"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="min_durasi_recovery"
          summary-type="min"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => (parseInt(e.value) ? formatWaktu(e.value) : '-')"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_diatas_sla_recovery"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
        <DxGroupItem
          :show-in-group-footer="true"
          column="total_dibawah_sla_recovery"
          summary-type="sum"
          display-format="{0}"
          css-class="!text-right"
          :customize-text="(e: any) => formatNumber(parseFloat(e.value.toString()))"
        />
      </DxSummary>
    </DxDataGrid>
  </div>

  <DetailDialog
    :open="dialogDetail"
    title="Detail Rekapitulasi Gangguan/Jenis Gangguan SE 004"
    @on-close="closeDialog"
    :full-width="true"
  >
    <div class="w-full mb-4 lg:w-[70%] lg:float-left">
      <div class="p-4 bg-white rounded-xl lg:mr-4">
        <DxDataGrid
          class="max-h-[calc(100vh-140px)]"
          :data-source="dataSub"
          :show-column-lines="true"
          :show-row-lines="false"
          :show-borders="true"
          :row-alternation-enabled="true"
          :hover-state-enabled="true"
          @selection-changed="onDataSubSelectionChanged"
          :column-width="100"
          @exporting="onExporting"
          :allow-column-resizing="true"
          column-resizing-mode="widget"
        >
          <DxSelection mode="single" />
          <DxPaging :page-size="5" :enabled="true" />
          <DxPager
            :visible="true"
            :allowed-page-sizes="[5, 10, 20]"
            display-mode="full"
            :show-page-size-selector="true"
            :show-info="true"
            :show-navigation-buttons="true"
          />
          <DxLoadPanel
            :position="position"
            :show-indicator="showIndicator"
            :show-pane="showPane"
            :shading="shading"
            v-model:visible="loadingSubData"
            :enabled="true"
          />
          <DxSearchPanel :visible="true" :highlight-case-sensitive="true" />
          <DxExport
            :enabled="true"
            :formats="['pdf', 'xlsx', 'document']"
            :allow-export-selected-data="false"
          />

          <DxColumn
            css-class="custom-table-column"
            :width="50"
            alignment="center"
            :calculateCellValue="(item: any) => dataSub.findIndex((i) => i == item) + 1"
            data-type="number"
            caption="No"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="no_laporan"
            caption="No Laporan"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="waktu_lapor"
            caption="Tgl Lapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="waktu_response"
            caption="Tgl Datang"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="waktu_recovery"
            caption="Tgl Nyala"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="durasi_response_time"
            caption="Durasi Response Time"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="durasi_recovery_time"
            caption="Durasi Recovery Time"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="status_akhir"
            caption="Status"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="referensi_marking"
            caption="Referensi Marking"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="idpel_nometer"
            caption="IDPEL/NO METER"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="nama_pelapor"
            caption="Nama Pelapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="alamat_pelapor"
            caption="Alamat Pelapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="no_telp_pelapor"
            caption="No Telp Pelapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="200"
            alignment="center"
            data-field="keterangan_pelapor"
            caption="Keterangan Pelapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="nama_posko"
            caption="Posko"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="tindakan"
            caption="Tidakan"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="penyebab"
            caption="Penyebab"
            css-class="custom-table-column"
            cell-template="formatText"
          />

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

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

          <template #formatTime="{ data }">
            <p>
              {{ parseInt(data.text) ? formatWaktu(data.text) : '-' }}
            </p>
          </template>
        </DxDataGrid>
      </div>
    </div>
    <div class="w-full mb-4 lg:w-[30%] lg:float-right">
      <div class="p-4 space-y-2 bg-white rounded-xl">
        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">No Laporan:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.no_laporan" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Tgl Lapor:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.waktu_lapor" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Tgl Datang:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.waktu_response"
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Tgl Nyala:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.waktu_recovery"
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Durasi Response Time:</h3>
          <InputText
            :readonly="true"
            :value="
              parseInt(dataSubSelected?.durasi_response_time)
                ? formatWaktu(dataSubSelected?.durasi_response_time)
                : '-'
            "
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Durasi Recovery Time:</h3>
          <InputText
            :readonly="true"
            :value="
              parseInt(dataSubSelected?.durasi_recovery_time)
                ? formatWaktu(dataSubSelected?.durasi_recovery_time)
                : '-'
            "
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Status:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.status_akhir" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Referensi Marking:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.referensi_marking"
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">IDPEL/NO METER:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.idpel_nometer" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Nama Pelapor:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.nama_pelapor" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Alamat Pelapor:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.alamat_pelapor"
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">No Telp Pelapor:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.no_telp_pelapor"
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Keterangan Pelapor:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.keterangan_pelapor"
            class-name="flex-1"
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Posko:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.nama_posko" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Tindakan:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.tindakan" class-name="flex-1" />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Penyeban:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.penyebab" class-name="flex-1" />
        </div>
      </div>
    </div>
  </DetailDialog>
</template>

<script setup lang="ts">
import Filters from '@/components/Form/Filters.vue'
import Type1 from '@/components/Form/FiltersType/Type1.vue'
import { onMounted, ref, watch } from 'vue'
import { formatWaktu } from '@/components/Form/FiltersType/reference'
import { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxLoadPanel,
  DxPaging,
  DxScrolling,
  DxSearchPanel,
  DxSelection,
  DxSummary,
  DxGroupItem,
  DxPager
} 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 { useQuery } from '@vue/apollo-composable'
import { queries } from '@/utils/api/api.graphql'
import { formatNumber, formatPercentage, isNumber } from '@/utils/numbers'
import InputText from '@/components/InputText.vue'
import DetailDialog from '@/components/Dialogs/DetailDialog.vue'
import { apolloClient } from '@/utils/api/api.graphql'
import { provideApolloClient } from '@vue/apollo-composable'

const client = apolloClient()
provideApolloClient(client)
const position = { of: '#data' }
const showIndicator = ref(true)
const shading = ref(true)
const showPane = ref(true)
const data = ref<any[]>([])
const dataSub = ref<any[]>([])
const dataSelected = ref<any>(null)
const dataSubSelected = ref<any>(null)
const dialogDetail = ref(false)
const loadingData = ref(false)
const loadingSubData = ref(false)

const closeDialog = () => {
  dialogDetail.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 filterData = (params: any) => {
  const dateValue = params.periode.split(' s/d ')
  const { posko, uid, up3 } = params

  const { onResult, onError, loading, refetch } = useQuery(
    queries.gangguan.rekap.jenisGangguanSE004,
    {
      dateFrom: dateValue[0]
        ? dateValue[0].split('-').reverse().join('-')
        : new Date().toISOString().slice(0, 10),
      dateTo: dateValue[1]
        ? dateValue[1].split('-').reverse().join('-')
        : new Date().toISOString().slice(0, 10),
      posko: posko ? posko.id : 0,
      idUid: uid ? uid.id : 0,
      idUp3: up3 ? up3.id : 0
    }
  )

  onResult((queryResult) => {
    if (queryResult.data != undefined) {
      data.value = queryResult.data.rekapitulasiJenisGangguanSE004
      console.log(queryResult.loading)
      console.log(queryResult.networkStatus)
    }
  })

  onError((queryError) => {
    console.log(queryError)
  })

  watch(loading, (value) => {
    loadingData.value = value
  })
}

const getDetail = () => {
  loadingSubData.value = true
  const dateValue = filters.value.periode.split(' s/d ')
  const ref = dataSelected.value

  const query = {
    dateFrom: dateValue[0]
      ? dateValue[0].split('-').reverse().join('-')
      : new Date().toISOString().slice(0, 10),
    dateTo: dateValue[1]
      ? dateValue[1].split('-').reverse().join('-')
      : new Date().toISOString().slice(0, 10),
    posko: ref.id_posko,
    idUid: ref.id_uid,
    idUp3: ref.id_up3
  }

  const { onResult, onError, loading, refetch } = useQuery(
    queries.gangguan.rekap.gangguanAllDetail,
    query
  )

  refetch(query)

  onResult((queryResult) => {
    if (queryResult.data != undefined) {
      dataSub.value = queryResult.data.detailGangguan
    }
  })

  onError((error) => {
    console.log(error)
  })

  watch(loading, (value) => {
    loadingSubData.value = value
  })
}

const showDetail = () => {
  if (dataSelected.value != null) {
    dataSub.value = []
    dataSubSelected.value = null
    dialogDetail.value = true
    getDetail()
  }
}

const onDataSelectionChanged = ({ selectedRowsData }: any) => {
  dataSelected.value = selectedRowsData[0]
  showDetail()
}

const onDataSubSelectionChanged = ({ selectedRowsData }: any) => {
  dataSubSelected.value = selectedRowsData[0]
}

const filters = ref()
onMounted(() => {
  if (import.meta.env.DEV) {
    data.value = [
      {
        id: '0',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Kabel',
        kode: '11232',
        total: 1754,
        total_selesai: 1754,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 248343,
        avg_durasi_response: 2.360302677532014,
        min_durasi_response: 0,
        max_durasi_response: 7020,
        total_dibawah_sla_response: 1714,
        total_diatas_sla_response: 4,
        total_durasi_recovery: 7444445,
        avg_durasi_recovery: 70.67217787913341,
        min_durasi_recovery: 0,
        max_durasi_recovery: 199591,
        total_dibawah_sla_recovery: 1660,
        total_diatas_sla_recovery: 94,
        total_response: 1718,
        total_recovery: 1754
      },
      {
        id: '1',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'MV Cell',
        kode: '11511',
        total: 56062,
        total_selesai: 56061,
        persen_selesai: 99.99821626056865,
        total_inproses: 1,
        persen_inproses: 0.0017837394313438694,
        total_durasi_response: 80699092,
        avg_durasi_response: 23.504602211915806,
        min_durasi_response: 24,
        max_durasi_response: 2592137,
        total_dibawah_sla_response: 52700,
        total_diatas_sla_response: 3360,
        total_durasi_recovery: 1398860783,
        avg_durasi_recovery: 415.39603995718875,
        min_durasi_recovery: 101,
        max_durasi_recovery: 2713062,
        total_dibawah_sla_recovery: 49187,
        total_diatas_sla_recovery: 6873,
        total_response: 56060,
        total_recovery: 56060
      },
      {
        id: '2',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'JTR',
        kode: '13400',
        total: 6022,
        total_selesai: 6022,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 8262056,
        avg_durasi_response: 22.382392026578074,
        min_durasi_response: -16519,
        max_durasi_response: 76299,
        total_dibawah_sla_response: 5655,
        total_diatas_sla_response: 365,
        total_durasi_recovery: 87345586,
        avg_durasi_recovery: 241.25440053138493,
        min_durasi_recovery: -16459,
        max_durasi_recovery: 2499716,
        total_dibawah_sla_recovery: 4802,
        total_diatas_sla_recovery: 1220,
        total_response: 6020,
        total_recovery: 6022
      },
      {
        id: '3',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Jointing',
        kode: '11243',
        total: 1,
        total_selesai: 1,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 60,
        avg_durasi_response: 1,
        min_durasi_response: 60,
        max_durasi_response: 60,
        total_dibawah_sla_response: 1,
        total_diatas_sla_response: 0,
        total_durasi_recovery: 240,
        avg_durasi_recovery: 4,
        min_durasi_recovery: 240,
        max_durasi_recovery: 240,
        total_dibawah_sla_recovery: 1,
        total_diatas_sla_recovery: 0,
        total_response: 1,
        total_recovery: 1
      },
      {
        id: '4',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: null,
        kode: '',
        total: 7153680,
        total_selesai: 1144750,
        persen_selesai: 16.00225338567003,
        total_inproses: 6008930,
        persen_inproses: 83.99774661432997,
        total_durasi_response: 746069414,
        avg_durasi_response: 29.719034788714257,
        min_durasi_response: -16906,
        max_durasi_response: 2563885,
        total_dibawah_sla_response: 352502,
        total_diatas_sla_response: 59097,
        total_durasi_recovery: 1633920126,
        avg_durasi_recovery: 65.89483875340065,
        min_durasi_recovery: -86061,
        max_durasi_recovery: 3373943,
        total_dibawah_sla_recovery: 412256,
        total_diatas_sla_recovery: 900,
        total_response: 411599,
        total_recovery: 413156
      },
      {
        id: '5',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'AVR / Kapasitor',
        kode: '11215',
        total: 20,
        total_selesai: 20,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 2682,
        avg_durasi_response: 2.15,
        min_durasi_response: 0,
        max_durasi_response: 1477,
        total_dibawah_sla_response: 20,
        total_diatas_sla_response: 0,
        total_durasi_recovery: 115132,
        avg_durasi_recovery: 95.85,
        min_durasi_recovery: 0,
        max_durasi_recovery: 18660,
        total_dibawah_sla_recovery: 15,
        total_diatas_sla_recovery: 5,
        total_response: 20,
        total_recovery: 20
      },
      {
        id: '6',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Kelompok Terencana (Planned) ',
        kode: '32000',
        total: 1130,
        total_selesai: 1127,
        persen_selesai: 99.73451327433628,
        total_inproses: 3,
        persen_inproses: 0.2654867256637168,
        total_durasi_response: 6837343,
        avg_durasi_response: 100.54432624113475,
        min_durasi_response: -2607074,
        max_durasi_response: 2553583,
        total_dibawah_sla_response: 1070,
        total_diatas_sla_response: 58,
        total_durasi_recovery: 9901876,
        avg_durasi_recovery: 145.95297249334516,
        min_durasi_recovery: -2605454,
        max_durasi_recovery: 2553643,
        total_dibawah_sla_recovery: 1022,
        total_diatas_sla_recovery: 105,
        total_response: 1128,
        total_recovery: 1127
      },
      {
        id: '7',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Arrester',
        kode: '11219',
        total: 764,
        total_selesai: 764,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 103137,
        avg_durasi_response: 2.1612057667103537,
        min_durasi_response: 0,
        max_durasi_response: 14436,
        total_dibawah_sla_response: 755,
        total_diatas_sla_response: 8,
        total_durasi_recovery: 2758840,
        avg_durasi_recovery: 59.97643979057592,
        min_durasi_recovery: 0,
        max_durasi_recovery: 62400,
        total_dibawah_sla_recovery: 711,
        total_diatas_sla_recovery: 53,
        total_response: 763,
        total_recovery: 764
      },
      {
        id: '8',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Trafo',
        kode: '11322',
        total: 322,
        total_selesai: 322,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 238159,
        avg_durasi_response: 14.425454545454546,
        min_durasi_response: 0,
        max_durasi_response: 40320,
        total_dibawah_sla_response: 255,
        total_diatas_sla_response: 20,
        total_durasi_recovery: 2275182,
        avg_durasi_recovery: 117.67391304347827,
        min_durasi_recovery: 59,
        max_durasi_recovery: 105660,
        total_dibawah_sla_recovery: 264,
        total_diatas_sla_recovery: 58,
        total_response: 275,
        total_recovery: 322
      },
      {
        id: '9',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Kabel',
        kode: '11242',
        total: 2,
        total_selesai: 2,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 377,
        avg_durasi_response: 3,
        min_durasi_response: 60,
        max_durasi_response: 317,
        total_dibawah_sla_response: 2,
        total_diatas_sla_response: 0,
        total_durasi_recovery: 9797,
        avg_durasi_recovery: 81.5,
        min_durasi_recovery: 1800,
        max_durasi_recovery: 7997,
        total_dibawah_sla_recovery: 2,
        total_diatas_sla_recovery: 0,
        total_response: 2,
        total_recovery: 2
      },
      {
        id: '10',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Kabel Opstyg / SKTR',
        kode: '11411',
        total: 91371,
        total_selesai: 91371,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 117146730,
        avg_durasi_response: 20.890635777395797,
        min_durasi_response: 0,
        max_durasi_response: 86580,
        total_dibawah_sla_response: 85886,
        total_diatas_sla_response: 5451,
        total_durasi_recovery: 488901032,
        avg_durasi_recovery: 88.69208711831017,
        min_durasi_recovery: 0,
        max_durasi_recovery: 4028013,
        total_dibawah_sla_recovery: 88945,
        total_diatas_sla_recovery: 2425,
        total_response: 91337,
        total_recovery: 91370
      },
      {
        id: '11',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Gardu Distribusi',
        kode: '13300',
        total: 275,
        total_selesai: 275,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 382161,
        avg_durasi_response: 22.952727272727273,
        min_durasi_response: -610712,
        max_durasi_response: 638280,
        total_dibawah_sla_response: 238,
        total_diatas_sla_response: 37,
        total_durasi_recovery: 14380135,
        avg_durasi_recovery: 871.3163636363637,
        min_durasi_recovery: -484681,
        max_durasi_recovery: 7565219,
        total_dibawah_sla_recovery: 195,
        total_diatas_sla_recovery: 80,
        total_response: 275,
        total_recovery: 275
      },
      {
        id: '12',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Konstruksi',
        kode: '12101',
        total: 13,
        total_selesai: 13,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 444894,
        avg_durasi_response: 570,
        min_durasi_response: -17483,
        max_durasi_response: 435093,
        total_dibawah_sla_response: 11,
        total_diatas_sla_response: 2,
        total_durasi_recovery: 520794,
        avg_durasi_recovery: 667.3076923076923,
        min_durasi_recovery: -13763,
        max_durasi_recovery: 445113,
        total_dibawah_sla_recovery: 11,
        total_diatas_sla_recovery: 2,
        total_response: 13,
        total_recovery: 13
      },
      {
        id: '13',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'Operasi',
        kode: '12503',
        total: 747,
        total_selesai: 747,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 1354769,
        avg_durasi_response: 29.809395973154363,
        min_durasi_response: -5325,
        max_durasi_response: 349500,
        total_dibawah_sla_response: 713,
        total_diatas_sla_response: 32,
        total_durasi_recovery: 4880492,
        avg_durasi_recovery: 108.41097724230255,
        min_durasi_recovery: -5325,
        max_durasi_recovery: 494389,
        total_dibawah_sla_recovery: 692,
        total_diatas_sla_recovery: 55,
        total_response: 745,
        total_recovery: 747
      },
      {
        id: '14',
        id_fasilitas: '111 -> Distribusi -> Penyulang',
        sub_kelompok: 'CO Branch',
        kode: '11217',
        total: 17364,
        total_selesai: 17364,
        persen_selesai: 100,
        total_inproses: 0,
        persen_inproses: 0,
        total_durasi_response: 9434964,
        avg_durasi_response: 9.046101538638853,
        min_durasi_response: -175685,
        max_durasi_response: 84720,
        total_dibawah_sla_response: 16595,
        total_diatas_sla_response: 758,
        total_durasi_recovery: 72920963,
        avg_durasi_recovery: 69.94573732718894,
        min_durasi_recovery: -175565,
        max_durasi_recovery: 136440,
        total_dibawah_sla_recovery: 16387,
        total_diatas_sla_recovery: 973,
        total_response: 17353,
        total_recovery: 17360
      }
    ]
  }
})
</script>
