<template>
  <div class="mt-4 lg:mt-6 max-w-7xl">
    <h1 class="text-xl font-medium md:text-2xl text-dark">Laporan Pengaduan Total</h1>
  </div>
  <DxDataGrid
    ref="dataGridRef"
    :allow-column-reordering="true"
    class="max-h-[calc(100vh-140px)] mb-10"
    :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"
  >
    <DxGrouping expand-mode="rowClick" :auto-expand-all="false" />
    <DxSelection mode="single" />
    <DxPaging :enabled="false" />
    <DxScrolling column-rendering-mode="virtual" mode="virtual" />

    <DxSearchPanel :visible="true" :highlight-case-sensitive="true" />
    <DxExport
      :enabled="true"
      :formats="['pdf', 'xlsx', 'document']"
      :allow-export-selected-data="false"
    />
    <DxColumnFixing :enabled="true" />

    <DxColumn
      :width="170"
      alignment="center"
      data-field="nama_ulp"
      caption="Nama Unit"
      css-class="custom-table-column"
      cell-template="formatText"
    />

    <DxColumn
      alignment="center"
      :min-width="170"
      data-type="text"
      data-field=""
      caption="Semua Unit"
      css-class="custom-table-column"
      :group-index="0"
    />
    <DxColumn
      alignment="center"
      :min-width="170"
      data-type="text"
      data-field="id_uid"
      caption="UID"
      css-class="custom-table-column"
      :group-index="1"
      name="namaUID"
      :calculate-group-value="(rowData: any) => rowData.nama_uid"
    />
    <DxColumn
      alignment="center"
      :min-width="170"
      data-type="text"
      data-field="id_up3"
      caption="UP3"
      css-class="custom-table-column"
      :group-index="1"
      name="namaUID"
      :calculate-group-value="(rowData: any) => rowData.nama_up3"
    />
    <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="formatNumberPLNMobileMarking"
          />
          <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="formatNumberPLNMobileNonMarking"
          />
        </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="formatNumberCCMarking"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="anomali_cc123_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatNumberCCNonMarking"
          />
        </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="formatNumberTotalMarking"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="total_anomali_non_marking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatNumberTotalNonMarking"
          />
        </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 #formatNumberPLNMobileMarking="{ data }">
      <p class="text-right cursor-pointer" @click="setParameterRequest('PLN Mobile', 1)">
        {{
          isNumber(data.text)
            ? data.column.caption == '%'
              ? formatPercentage(data.text)
              : formatNumber(data.text)
            : data.text
        }}
      </p>
    </template>

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

    <template #formatNumberCCMarking="{ data }">
      <p class="text-right cursor-pointer" @click="setParameterRequest('Call PLN 123', 1)">
        {{
          isNumber(data.text)
            ? data.column.caption == '%'
              ? formatPercentage(data.text)
              : formatNumber(data.text)
            : data.text
        }}
      </p>
    </template>

    <template #formatNumberCCNonMarking="{ data }">
      <p class="text-right cursor-pointer" @click="setParameterRequest('Call PLN 123', 2)">
        {{
          isNumber(data.text)
            ? data.column.caption == '%'
              ? formatPercentage(data.text)
              : formatNumber(data.text)
            : data.text
        }}
      </p>
    </template>

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

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

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

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

    <template #formatTime="{ data }">
      <p class="!text-right" @click="setParameterRequest(null, null)">
        {{ 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(e.value)"
      />
      <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(e.value)"
      />
      <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(e.value)"
      />
      <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(e.value)"
      />
      <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(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_pln_mobile_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_pln_mobile_non_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_cc123_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="anomali_cc123_non_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="total_anomali_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="total_anomali_non_marking"
        summary-type="sum"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_pln_mobile_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_pln_mobile_non_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_cc123_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_cc123_non_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_anomali_non_marking"
        summary-type="avg"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
    </DxSummary>
  </DxDataGrid>

  <DetailDialog
    :open="dialogDetail"
    title="Daftar Detail Anomali Penanganan Pengaduan"
    @on-close="closedialogDetail"
    :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
          :allow-column-reordering="true"
          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="onExportingDetail"
          :allow-column-resizing="true"
          column-resizing-mode="widget"
        >
          <DxSelection mode="single" />
          <DxPaging :page-size="20" :enabled="true" />
          <DxPager
            :visible="true"
            :allowed-page-sizes="[20, 50, 100]"
            display-mode="full"
            :show-page-size-selector="true"
            :show-info="true"
            :show-navigation-buttons="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"
            :calculate-display-value="(item: any) => dataSub.findIndex((i: any) => i == item) + 1"
            data-type="number"
            caption="No"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="nama_uid"
            caption="UIW/D"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="nama_up3"
            caption="UP3"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="nama_ulp"
            caption="Rayon"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <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="no_laporan_referensi"
            caption="No Laporan Referensi"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="waktu_lapor"
            caption="Tgl/Jam Lapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="waktu_response"
            caption="Tgl/Jam Datang"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="waktu_recovery"
            caption="Tgl/Jam 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="durasi_dispatch_time"
            caption="Durasi Penugasan Regu"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="durasi_perjalanan"
            caption="Durasi Perjalanan Regu"
            css-class="custom-table-column"
            cell-template="formatTime"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="distance"
            caption="Jarak Closing"
            css-class="custom-table-column"
            cell-template="formatNumber"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="dispatch_by"
            caption="Dispacth Oleh"
            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="150"
            alignment="center"
            data-field="keterangan_pelapor"
            caption="Keterangan Pelapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="media"
            caption="Sumber Lapor"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="diselesaikan_oleh"
            caption="Diselesaikan Oleh"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <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="status_induk"
            caption="Status Induk"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="referensi_marking_induk"
            caption="Referensi Marking Induk"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="kode_gangguan"
            caption="Kode Gangguan"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="jenis_gangguan"
            caption="Jenis Gangguan"
            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"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="tindakan"
            caption="Tindaan"
            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">UIW/D:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.nama_uid" 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">UP3:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.nama_up3" 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 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">No Laporan Referensi:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.no_laporan_referensi"
            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/Jam 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/Jam 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/Jam 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">Durasi Penugasan Regu:</h3>
          <InputText
            :readonly="true"
            class-name="flex-1"
            :value="
              parseInt(dataSubSelected?.durasi_recovery_time)
                ? formatWaktu(dataSubSelected?.durasi_recovery_time)
                : '-'
            "
          />
        </div>

        <div class="flex flex-row items-center justify-between w-full">
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Durasi Perjalanan Regu:</h3>
          <InputText
            :readonly="true"
            :value="
              parseInt(dataSubSelected?.durasi_dispatch_time)
                ? formatWaktu(dataSubSelected?.durasi_dispatch_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">Jarak Closing (m):</h3>
          <InputText :readonly="true" :value="dataSubSelected?.jarak_closing" 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">Dispatch Oleh:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.dispatch_oleh" 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"
            type="textarea"
            :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">Sumber Lapor:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.media" 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">Diselesaikan Oleh:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.diselesaikan_oleh"
            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">Status Induk:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.status_induk" 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 Induk:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.referensi_marking_induk"
            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">Kode Gangguan:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.kode_gangguan" 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">Jenis Gangguan:</h3>
          <InputText
            :readonly="true"
            :value="dataSubSelected?.jenis_gangguan"
            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">Penyebab:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.penyebab" 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>
    </div>
  </DetailDialog>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxGroupItem,
  DxGrouping,
  DxLoadPanel,
  DxPager,
  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'
import { queries, requestGraphQl } from '@/utils/api/api.graphql'
import InputText from '@/components/InputText.vue'
import DetailDialog from '@/components/Dialogs/DetailDialog.vue'
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[],
  filters: Object as () => any,
  loading: {
    type: Boolean,
    default: false
  }
})
const filters = ref<any>(computed(() => props.filters))
const data = computed(() => props.data)
const dataSub = ref<any[]>([])
const dataSelected = ref<any>(null)
const dataSubSelected = ref<any>(null)
const dialogDetail = ref(false)
const closedialogDetail = () => (dialogDetail.value = false)
const loadingData = ref(false)
const loadingSubData = ref(false)
const agreeToShowDialog = ref(false)
const mediaSelected = ref<any>(null)
const markingSelected = ref<any>(null)

const setParameterRequest = (media: any, marking: any) => {
  mediaSelected.value = media
  markingSelected.value = marking

  if (media != null && marking != null) {
    agreeToShowDialog.value = true
  } else {
    agreeToShowDialog.value = false
  }
}

const resetData = () => {
  dataSub.value = []
  dataSubSelected.value = null
}

const getDetail = async () => {
  resetData()
  const { ulp, uid, up3, jenisLaporan, periode } = filters.value
  console.table('LAPPGU', jenisLaporan)
  console.log('Media', mediaSelected.value)
  console.log('isMarking', markingSelected.value)
  console.log('periode', periode)

  const dateValue = periode.split(' s/d ')
  const selected = 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),
    idUlp: selected?.id_ulp ? selected?.id_ulp : 0,
    idUid: selected?.id_uid ? selected?.id_uid : 0,
    idUp3: selected?.id_up3 ? selected?.id_up3 : 0,
    namaRegional: '',
    media: mediaSelected.value,
    isMarking: markingSelected.value
  }

  loadingSubData.value = true
  await requestGraphQl(
    jenisLaporan.name == 'Laporan Berulang Unit'
      ? queries.anomali.gangguan.unitBerulangDetail
      : jenisLaporan.name == 'Laporan Rating Negatif'
        ? queries.anomali.gangguan.unitRatingNegatifDetail
        : queries.anomali.gangguan.unitSkipStepDetail,
    query
  )
    .then((result) => {
      if (result.data.data != undefined) {
        if (jenisLaporan.name == 'Laporan Berulang Unit') {
          dataSub.value = result.data.data.detailAnomaliPenangananPengaduanGangguanUnitBerulang
        } else if (jenisLaporan.name == 'Laporan Rating Negatif') {
          dataSub.value = result.data.data.detailAnomaliPenangananPengaduanGangguanUnitRatingNegatif
        } else {
          dataSub.value = result.data.data.detailAnomaliPenangananPengaduanGangguanUnitSkipStep
        }
      } else {
        dataSub.value = []
      }
    })
    .catch((err) => {
      console.error(err)
    })
    .finally(() => {
      loadingSubData.value = false
      dialogDetail.value = true
    })
}

const dataGridRef = ref<DxDataGrid | null>(null)
const clearSelection = () => {
  try {
    dataGridRef.value?.instance?.clearSelection()
  } catch (error) {
    console.log(error)
  }
}

const showDetail = () => {
  clearSelection()
  dataSub.value = []
  dataSubSelected.value = null
  if (agreeToShowDialog.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 onExportingDetail = (e: any) => {}

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

const onDataSelectionChanged = ({ selectedRowsData }: any) => {
  if (selectedRowsData[0] != undefined) {
    dataSelected.value = selectedRowsData[0]
    showDetail()
  }
}
</script>
