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

  <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"
      @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
        :width="100"
        alignment="center"
        :calculateCellValue="(item: any) => data.findIndex((i) => i == item) + 1"
        caption="NO"
        css-class="custom-table-column"
        data-type="number"
        data-field="no"
        cell-template="formatNumber"
      />

      <DxColumn
        :width="120"
        alignment="center"
        data-field="nama_up3"
        caption="UP3"
        css-class="custom-table-column"
        :group-index="0"
      />

      <DxColumn
        :width="170"
        alignment="center"
        data-field="nama_ulp"
        caption="Unit"
        css-class="custom-table-column"
        cell-template="formatText"
      />
      <DxColumn alignment="center" caption="Laporan" css-class="custom-table-column">
        <DxColumn
          :width="150"
          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="150"
            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="150"
            alignment="center"
            data-field="total_inproses"
            data-type="number"
            caption="Jml"
            css-class="custom-table-column"
          />
          <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="Hari" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="total_durasi_response"
            data-type="number"
            caption="Total"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="avg_durasi_response"
            data-type="number"
            caption="Rata-Rata"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="max_durasi_response"
            data-type="number"
            caption="Max"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            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="150"
            alignment="center"
            data-field="total_diatas_sla_response"
            data-type="number"
            caption=">SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            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="Penyelesaian" css-class="custom-table-column">
        <DxColumn alignment="center" caption="Hari" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="total_durasi_recovery"
            data-type="number"
            caption="Total"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="avg_durasi_recovery"
            data-type="number"
            caption="Rata-Rata"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="max_durasi_recovery"
            data-type="number"
            caption="Max"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            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="150"
            alignment="center"
            data-field="total_diatas_sla_recovery"
            data-type="number"
            caption=">SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="total_dibawah_sla_recovery"
            data-type="number"
            caption="≤SLA"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
        </DxColumn>
      </DxColumn>

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

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

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

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

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

  <DetailDialog
    :open="dialogDataSelected"
    title="Rekapitulasi Keluhan Per Unit"
    @on-close="closeDialogDataSelected"
    :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=""
            caption="Dalam Proses Bidang"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field=""
            caption="Selesai Bidang Unit"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="durasi_response_time"
            caption="Durasi Response Time"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="durasi_recovery_time"
            caption="Durasi Recovery Time"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field=""
            caption="Status"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="idpel_nometer"
            caption="IDPEL/NO METER"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="nama_pelapor"
            caption="Nama Pelapor"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="200"
            alignment="center"
            data-field="alamat_pelapor"
            caption="Alamat Pelapor"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="no_telp_pelapor"
            caption="No Tlp Pelapor"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="200"
            alignment="center"
            data-field="keterangan_pelapor"
            caption="Keterangan Pelapor"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field=""
            caption="Rayon"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field=""
            caption="Uraian"
            :allow-resizing="false"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field=""
            caption="Respon Pelanggan"
            :allow-resizing="false"
            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">Dalam Proses Bidang:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.dalam_proses_bidang"
            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">Selesai Bidang Unit:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.selesai_bidang_unit"
            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">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"
            type="textarea"
            :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">Rayon:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.rayon" 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">Uraian:</h3>
          <InputText
            :readonly="true"
            type="textarea"
            :value="dataSubSelected?.uraian"
            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">Response Pelanggan:</h3>
          <InputText
            :readonly="true"
            type="textarea"
            :value="dataSubSelected?.response_pelanggan"
            class-name="flex-1"
          />
        </div>
      </div>
    </div>
  </DetailDialog>
</template>

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

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>()
const dataSubSelected = ref<any>()
const dialogDataSelected = ref(false)
const closeDialogDataSelected = () => (dialogDataSelected.value = false)
const loadingData = ref(false)
const loadingSubData = ref(false)

const { onResult, onError, loading, refetch } = useQuery(queries.keluhan.rekap.keluhanPerUnit, {
  dateFrom: new Date('2023-10-01').toISOString().slice(0, 10),
  dateTo: new Date('2023-10-01').toISOString().slice(0, 10),
  idUlp: 0,
  idUid: 0,
  idUp3: 0
})

const filterData = (params: any) => {
  const dateValue = params.periode.split(' s/d ')
  const { ulp, uid, up3 } = params

  refetch({
    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),
    idUlp: ulp ? ulp.id : 0,
    idUid: uid ? uid.id : 0,
    idUp3: up3 ? up3.id : 0
  })

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

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

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

const getDetail = () => {
  // dataSub.value = dummyData.keluhan.rekap.rekapitulasiKeluhanPerUnit
}

const showDialogDataSelected = () => {
  dialogDataSelected.value = true
  console.log(dataSelected.value)
  getDetail()
}

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

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

const filters = ref()

onMounted(() => {
  if (import.meta.env.DEV) {
    data.value = [
      {
        id: 0,
        nama_regional: 'REGIONAL SUMKAL',
        id_uid: 120,
        nama_uid: 'WILAYAH SUMATERA UTARA',
        id_up3: 12100,
        nama_up3: 'UP3 PEMATANG SIANTAR',
        id_ulp: 12102,
        nama_ulp: 'ULP LIMA PULUH',
        total: 11,
        total_selesai: 1,
        persen_selesai: 9.090909090909092,
        total_inproses: 10,
        persen_inproses: 90.9090909090909,
        total_durasi_response: 10193,
        avg_durasi_response: 1019.3,
        min_durasi_response: 379,
        max_durasi_response: 1782,
        total_dibawah_sla_response: 7,
        total_diatas_sla_response: 3,
        total_durasi_recovery: 13701,
        avg_durasi_recovery: 1245.5454545454545,
        min_durasi_recovery: 388,
        max_durasi_recovery: 2501,
        total_dibawah_sla_recovery: 11,
        total_diatas_sla_recovery: 0
      },
      {
        id: 1,
        nama_regional: 'REGIONAL SULMAPANA',
        id_uid: 41,
        nama_uid: 'WILAYAH MALUKU DAN MALUKU UTARA',
        id_up3: 412,
        nama_up3: 'UP3 TERNATE',
        id_ulp: 41230,
        nama_ulp: 'ULP BACAN',
        total: 6,
        total_selesai: 4,
        persen_selesai: 66.66666666666666,
        total_inproses: 2,
        persen_inproses: 33.33333333333333,
        total_durasi_response: 7291,
        avg_durasi_response: 1458.2,
        min_durasi_response: 308,
        max_durasi_response: 2736,
        total_dibawah_sla_response: 2,
        total_diatas_sla_response: 3,
        total_durasi_recovery: 19860,
        avg_durasi_recovery: 3310,
        min_durasi_recovery: 2152,
        max_durasi_recovery: 6176,
        total_dibawah_sla_recovery: 5,
        total_diatas_sla_recovery: 1
      },
      {
        id: 2,
        nama_regional: 'REGIONAL JMB',
        id_uid: 101,
        nama_uid: 'DISTRIBUSI JAWA TENGAH & DIY',
        id_up3: 52150,
        nama_up3: 'UP3 MAGELANG',
        id_ulp: 52156,
        nama_ulp: 'ULP PARAKAN',
        total: 56,
        total_selesai: 15,
        persen_selesai: 26.785714285714285,
        total_inproses: 41,
        persen_inproses: 73.21428571428571,
        total_durasi_response: 145058,
        avg_durasi_response: 3086.340425531915,
        min_durasi_response: 459,
        max_durasi_response: 8933,
        total_dibawah_sla_response: 11,
        total_diatas_sla_response: 36,
        total_durasi_recovery: 204948,
        avg_durasi_recovery: 3659.785714285714,
        min_durasi_recovery: 1,
        max_durasi_recovery: 15497,
        total_dibawah_sla_recovery: 43,
        total_diatas_sla_recovery: 13
      },
      {
        id: 3,
        nama_regional: 'REGIONAL SULMAPANA',
        id_uid: 31,
        nama_uid: 'WILAYAH SULAWESI UTARA,TENGAH DAN GORONTALO',
        id_up3: 31500,
        nama_up3: 'UP3 TOLITOLI',
        id_ulp: 31530,
        nama_ulp: 'ULP BANGKIR',
        total: 56,
        total_selesai: 23,
        persen_selesai: 41.07142857142857,
        total_inproses: 33,
        persen_inproses: 58.92857142857143,
        total_durasi_response: 14809,
        avg_durasi_response: 643.8695652173913,
        min_durasi_response: 63,
        max_durasi_response: 6694,
        total_dibawah_sla_response: 22,
        total_diatas_sla_response: 1,
        total_durasi_recovery: 63412,
        avg_durasi_recovery: 1132.357142857143,
        min_durasi_recovery: 76,
        max_durasi_recovery: 6815,
        total_dibawah_sla_recovery: 52,
        total_diatas_sla_recovery: 4
      },
      {
        id: 4,
        nama_regional: 'REGIONAL JMB',
        id_uid: 103,
        nama_uid: 'DISTRIBUSI JAWA TIMUR',
        id_up3: 154,
        nama_up3: 'UP3 SITUBONDO',
        id_ulp: 51654,
        nama_ulp: 'ULP WONOSARI',
        total: 92,
        total_selesai: 0,
        persen_selesai: 0,
        total_inproses: 92,
        persen_inproses: 100,
        total_durasi_response: 201431,
        avg_durasi_response: 2426.879518072289,
        min_durasi_response: 14,
        max_durasi_response: 22160,
        total_dibawah_sla_response: 54,
        total_diatas_sla_response: 29,
        total_durasi_recovery: 856596,
        avg_durasi_recovery: 9310.826086956522,
        min_durasi_recovery: 0,
        max_durasi_recovery: 74239,
        total_dibawah_sla_recovery: 49,
        total_diatas_sla_recovery: 43
      },
      {
        id: 5,
        nama_regional: 'REGIONAL SUMKAL',
        id_uid: 110,
        nama_uid: 'WILAYAH ACEH',
        id_up3: 116,
        nama_up3: 'UP3 SIGLI',
        id_ulp: 11662,
        nama_ulp: 'ULP MEUREUDU',
        total: 8,
        total_selesai: 0,
        persen_selesai: 0,
        total_inproses: 8,
        persen_inproses: 100,
        total_durasi_response: 3766,
        avg_durasi_response: 470.75,
        min_durasi_response: 99,
        max_durasi_response: 1322,
        total_dibawah_sla_response: 8,
        total_diatas_sla_response: 0,
        total_durasi_recovery: 4463,
        avg_durasi_recovery: 557.875,
        min_durasi_recovery: 132,
        max_durasi_recovery: 1390,
        total_dibawah_sla_recovery: 8,
        total_diatas_sla_recovery: 0
      },
      {
        id: 6,
        nama_regional: 'REGIONAL JMB',
        id_uid: 287,
        nama_uid: 'DISTRIBUSI BALI',
        id_up3: 329,
        nama_up3: 'UP3 BALI UTARA',
        id_ulp: 55340,
        nama_ulp: 'ULP GILIMANUK',
        total: 47,
        total_selesai: 19,
        persen_selesai: 40.42553191489361,
        total_inproses: 28,
        persen_inproses: 59.57446808510638,
        total_durasi_response: 47015,
        avg_durasi_response: 1000.3191489361702,
        min_durasi_response: 22,
        max_durasi_response: 14684,
        total_dibawah_sla_response: 42,
        total_diatas_sla_response: 5,
        total_durasi_recovery: 74421,
        avg_durasi_recovery: 1583.4255319148936,
        min_durasi_recovery: 257,
        max_durasi_recovery: 14769,
        total_dibawah_sla_recovery: 44,
        total_diatas_sla_recovery: 3
      },
      {
        id: 7,
        nama_regional: null,
        id_uid: 0,
        nama_uid: '',
        id_up3: 0,
        nama_up3: '',
        id_ulp: 22450,
        nama_ulp: 'ULP PANGKALAN BUN',
        total: 167,
        total_selesai: 0,
        persen_selesai: 0,
        total_inproses: 167,
        persen_inproses: 100,
        total_durasi_response: 118469,
        avg_durasi_response: 789.7933333333333,
        min_durasi_response: 52,
        max_durasi_response: 3752,
        total_dibawah_sla_response: 135,
        total_diatas_sla_response: 15,
        total_durasi_recovery: 173448,
        avg_durasi_recovery: 1038.6107784431138,
        min_durasi_recovery: 0,
        max_durasi_recovery: 3793,
        total_dibawah_sla_recovery: 167,
        total_diatas_sla_recovery: 0
      },
      {
        id: 8,
        nama_regional: 'REGIONAL SUMKAL',
        id_uid: 131,
        nama_uid: 'WILAYAH SUMATERA BARAT',
        id_up3: 13300,
        nama_up3: 'UP3 SOLOK',
        id_ulp: 13334,
        nama_ulp: 'ULP SILUNGKANG',
        total: 1,
        total_selesai: 0,
        persen_selesai: 0,
        total_inproses: 1,
        persen_inproses: 100,
        total_durasi_response: 5118,
        avg_durasi_response: 5118,
        min_durasi_response: 5118,
        max_durasi_response: 5118,
        total_dibawah_sla_response: 0,
        total_diatas_sla_response: 1,
        total_durasi_recovery: 5696,
        avg_durasi_recovery: 5696,
        min_durasi_recovery: 5696,
        max_durasi_recovery: 5696,
        total_dibawah_sla_recovery: 0,
        total_diatas_sla_recovery: 1
      },
      {
        id: 9,
        nama_regional: 'REGIONAL SUMKAL',
        id_uid: 221,
        nama_uid: 'WILAYAH KALIMANTAN SELATAN DAN TENGAH',
        id_up3: 2220,
        nama_up3: 'UP3 BARABAI',
        id_ulp: 22230,
        nama_ulp: 'ULP AMUNTAI',
        total: 48,
        total_selesai: 4,
        persen_selesai: 8.333333333333332,
        total_inproses: 44,
        persen_inproses: 91.66666666666666,
        total_durasi_response: 25050,
        avg_durasi_response: 532.9787234042553,
        min_durasi_response: 58,
        max_durasi_response: 2352,
        total_dibawah_sla_response: 44,
        total_diatas_sla_response: 3,
        total_durasi_recovery: 49151,
        avg_durasi_recovery: 1023.9791666666666,
        min_durasi_recovery: 129,
        max_durasi_recovery: 2853,
        total_dibawah_sla_recovery: 48,
        total_diatas_sla_recovery: 0
      },
      {
        id: 10,
        nama_regional: 'REGIONAL SULMAPANA',
        id_uid: 31,
        nama_uid: 'WILAYAH SULAWESI UTARA,TENGAH DAN GORONTALO',
        id_up3: 31700,
        nama_up3: 'UP3 KOTAMOBAGU',
        id_ulp: 31730,
        nama_ulp: 'ULP MODAYAG',
        total: 21,
        total_selesai: 11,
        persen_selesai: 52.38095238095239,
        total_inproses: 10,
        persen_inproses: 47.61904761904761,
        total_durasi_response: 11456,
        avg_durasi_response: 716,
        min_durasi_response: 126,
        max_durasi_response: 2376,
        total_dibawah_sla_response: 13,
        total_diatas_sla_response: 3,
        total_durasi_recovery: 30489,
        avg_durasi_recovery: 1451.857142857143,
        min_durasi_recovery: 0,
        max_durasi_recovery: 3482,
        total_dibawah_sla_recovery: 21,
        total_diatas_sla_recovery: 0
      },
      {
        id: 11,
        nama_regional: 'REGIONAL JMB',
        id_uid: 427,
        nama_uid: 'DISTRIBUSI JAWA BARAT',
        id_up3: 546,
        nama_up3: 'UP3 TASIKMALAYA',
        id_ulp: 53231,
        nama_ulp: 'ULP CIAMIS',
        total: 123,
        total_selesai: 16,
        persen_selesai: 13.008130081300814,
        total_inproses: 107,
        persen_inproses: 86.99186991869918,
        total_durasi_response: 476405,
        avg_durasi_response: 5881.543209876543,
        min_durasi_response: 46,
        max_durasi_response: 41257,
        total_dibawah_sla_response: 21,
        total_diatas_sla_response: 60,
        total_durasi_recovery: 1177396,
        avg_durasi_recovery: 9572.325203252032,
        min_durasi_recovery: 0,
        max_durasi_recovery: 45912,
        total_dibawah_sla_recovery: 57,
        total_diatas_sla_recovery: 66
      },
      {
        id: 12,
        nama_regional: 'REGIONAL JMB',
        id_uid: 103,
        nama_uid: 'DISTRIBUSI JAWA TIMUR',
        id_up3: 157,
        nama_up3: 'UP3 SURABAYA UTARA',
        id_ulp: 51101,
        nama_ulp: 'ULP INDRAPURA',
        total: 139,
        total_selesai: 131,
        persen_selesai: 94.24460431654677,
        total_inproses: 8,
        persen_inproses: 5.755395683453238,
        total_durasi_response: 491177,
        avg_durasi_response: 3749.442748091603,
        min_durasi_response: 18,
        max_durasi_response: 20546,
        total_dibawah_sla_response: 51,
        total_diatas_sla_response: 80,
        total_durasi_recovery: 775066,
        avg_durasi_recovery: 5576.014388489209,
        min_durasi_recovery: 1,
        max_durasi_recovery: 49206,
        total_dibawah_sla_recovery: 78,
        total_diatas_sla_recovery: 61
      },
      {
        id: 13,
        nama_regional: 'REGIONAL SULMAPANA',
        id_uid: 41,
        nama_uid: 'WILAYAH MALUKU DAN MALUKU UTARA',
        id_up3: 414,
        nama_up3: 'UP3 MASOHI',
        id_ulp: 41430,
        nama_ulp: 'ULP MASOHI',
        total: 19,
        total_selesai: 17,
        persen_selesai: 89.47368421052632,
        total_inproses: 2,
        persen_inproses: 10.526315789473683,
        total_durasi_response: 18570,
        avg_durasi_response: 1092.3529411764705,
        min_durasi_response: 480,
        max_durasi_response: 2337,
        total_dibawah_sla_response: 13,
        total_diatas_sla_response: 4,
        total_durasi_recovery: 41850,
        avg_durasi_recovery: 2202.6315789473683,
        min_durasi_recovery: 22,
        max_durasi_recovery: 4386,
        total_dibawah_sla_recovery: 18,
        total_diatas_sla_recovery: 1
      },
      {
        id: 14,
        nama_regional: 'REGIONAL SUMKAL',
        id_uid: 131,
        nama_uid: 'WILAYAH SUMATERA BARAT',
        id_up3: 13100,
        nama_up3: 'UP3 PADANG',
        id_ulp: 13105,
        nama_ulp: 'ULP LUBUK ALUNG',
        total: 67,
        total_selesai: 56,
        persen_selesai: 83.5820895522388,
        total_inproses: 11,
        persen_inproses: 16.417910447761194,
        total_durasi_response: 67996,
        avg_durasi_response: 1214.2142857142858,
        min_durasi_response: 45,
        max_durasi_response: 17051,
        total_dibawah_sla_response: 48,
        total_diatas_sla_response: 8,
        total_durasi_recovery: 546970,
        avg_durasi_recovery: 8163.731343283582,
        min_durasi_recovery: 0,
        max_durasi_recovery: 221267,
        total_dibawah_sla_recovery: 40,
        total_diatas_sla_recovery: 27
      }
    ]
  }
})
</script>
