<template>
  <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"
    @exporting="onExporting"
    :allow-column-resizing="true"
    column-resizing-mode="widget"
    :word-wrap-enabled="true"
  >
    <DxGrouping auto-expand-all="false" expand-mode="rowClick" />
    <DxSelection mode="single" />
    <DxPaging :enabled="false" />
    <DxScrolling column-rendering-mode="virtual" mode="virtual" />
    <DxSearchPanel :visible="true" :highlight-case-sensitive="true" />
    <DxExport
      :enabled="true"
      :formats="['pdf', 'xlsx', 'document']"
      :allow-export-selected-data="false"
    />
    <DxColumnFixing :enabled="true" />

    <DxColumn
      alignment="center"
      data-field="nama_unit"
      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
      :width="170"
      alignment="center"
      data-field="nama_unit"
      caption="Nama Unit"
      css-class="custom-table-column"
      cell-template="formatText"
    />
    <DxColumn
      :width="170"
      alignment="center"
      data-field="nama_petugas"
      caption="Nama Petugas"
      css-class="custom-table-column"
      cell-template="formatText"
    />
    <DxColumn alignment="center" caption="Total WO" css-class="custom-table-column">
      <DxColumn
        alignment="center"
        caption="(PLN Mobile, CC123, DLL)"
        css-class="custom-table-column"
      >
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_total"
          data-type="number"
          caption="a"
          css-class="custom-table-column"
          cell-template="formatNumberTotalWo"
        />
      </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
          :width="150"
          alignment="center"
          data-field="wo_pln_mobile"
          data-type="number"
          caption="b"
          css-class="custom-table-column"
          cell-template="formatNumberPLNMobile"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="CC 123" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="wo_cc123"
          data-type="number"
          caption="c"
          css-class="custom-table-column"
          cell-template="formatNumberCC"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="Total" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-type="number"
          caption="d=b+c"
          css-class="custom-table-column"
          cell-template="formatNumberTotal"
          :calculate-display-value="(rowData: any) => rowData.wo_pln_mobile + rowData.wo_cc123"
        />
      </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
          :width="150"
          alignment="center"
          data-field="persen_anomali_pln_mobile"
          data-type="number"
          caption="e=b/a"
          css-class="custom-table-column"
          cell-template="formatPercentage"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="CC 123" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-field="persen_anomali_cc123"
          data-type="number"
          caption="f=c/a"
          css-class="custom-table-column"
          cell-template="formatPercentage"
        />
      </DxColumn>
      <DxColumn alignment="center" caption="Total" css-class="custom-table-column">
        <DxColumn
          :width="150"
          alignment="center"
          data-type="number"
          caption="g=e+f"
          css-class="custom-table-column"
          cell-template="formatPercentage"
          :calculate-display-value="
            (rowData: any) => rowData.persen_anomali_pln_mobile + rowData.persen_anomali_cc123
          "
        />
      </DxColumn>
    </DxColumn>

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

    <template #formatNumberTotalWo="{ data }">
      <p
        class="text-right"
        @click="
          setParameterRequest({
            media: null,
            value: data.text
          })
        "
      >
        {{
          isNumber(data.text)
            ? data.column.caption == '%'
              ? formatPercentage(data.text)
              : formatNumber(data.text)
            : data.text
        }}
      </p>
    </template>

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

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

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

    <template #formatPercentage="{ data }">
      <p
        class="text-right"
        @click="
          setParameterRequest({
            media: null,
            value: data.text
          })
        "
      >
        {{ isNumber(data.text) ? formatPercentage(data.text) : data.text }}
      </p>
    </template>

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

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

  <DetailDialog
    :open="dialogDetail"
    :title="`Daftar Detail Anomali Penanganan Pengaduan Gangguan Petugas - ${filters.jenisLaporan.name}`"
    @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="nama_petugas"
            caption="Nama Petugas"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name != 'Laporan Berulang Unit'"
            :width="150"
            alignment="center"
            data-field="no_laporan"
            caption="No Laporan"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
            :width="150"
            alignment="center"
            data-field="no_laporan_laporulang"
            caption="No Laporan"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name == 'Laporan Rating Negatif'"
            :width="150"
            alignment="center"
            data-field="rating"
            caption="Rating"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name == 'Laporan Rating Negatif'"
            :width="150"
            alignment="center"
            data-field="feedback"
            caption="Feedback"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name == 'Laporan Rating Negatif'"
            :width="150"
            alignment="center"
            data-field="ulasan"
            caption="Ulasan"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
            :width="150"
            alignment="center"
            data-field="no_laporan"
            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 (m)"
            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
            v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
            :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="marking"
            caption="Referensi Marking"
            css-class="custom-table-column"
            cell-template="formatText"
          />
          <DxColumn
            v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
            :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="Tindakan"
            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">Rayon:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.nama_ulp" 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 Petugas:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.nama_petugas" 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
            v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
            :readonly="true"
            :value="dataSubSelected?.no_laporan_laporulang"
            class-name="flex-1"
          />
          <InputText
            v-else
            :readonly="true"
            :value="dataSubSelected?.no_laporan"
            class-name="flex-1"
          />
        </div>

        <div
          v-if="filters.jenisLaporan.name == 'Laporan Rating Negatif'"
          class="flex flex-row items-center justify-between w-full"
        >
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Rating:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.rating" class-name="flex-1" />
        </div>

        <div
          v-if="filters.jenisLaporan.name == 'Laporan Rating Negatif'"
          class="flex flex-row items-center justify-between w-full"
        >
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Feedback:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.feedback" class-name="flex-1" />
        </div>

        <div
          v-if="filters.jenisLaporan.name == 'Laporan Rating Negatif'"
          class="flex flex-row items-center justify-between w-full"
        >
          <h3 class="text-sm font-medium w-[135px] text-gray-800">Ulasan:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.ulasan" class-name="flex-1" />
        </div>

        <div
          v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
          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" 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_dispatch_time)
                ? formatWaktu(dataSubSelected?.durasi_dispatch_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_perjalanan)
                ? formatWaktu(dataSubSelected?.durasi_perjalanan)
                : '-'
            "
            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?.distance" 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_by" 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
          v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
          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:</h3>
          <InputText :readonly="true" :value="dataSubSelected?.marking" class-name="flex-1" />
        </div>

        <div
          v-if="filters.jenisLaporan.name == 'Laporan Berulang Unit'"
          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 { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxGrouping,
  DxPager,
  DxPaging,
  DxScrolling,
  DxSearchPanel,
  DxSelection
} from 'devextreme-vue/data-grid'
import { computed, ref } from 'vue'
import { formatNumber, formatPercentage, isNumber } from '@/utils/numbers'
import { formatWaktu } from '@/components/Form/FiltersType/reference'
import {
  exportToPDF,
  exportToXLSX,
  exportToDOCX,
  exportDetailToPDF,
  exportDetailToXLSX,
  exportDetailToDOCX
} from '@/report/Anomali/Gangguan/Anomali_LAPPGP_LPP'
import { queries, requestGraphQl } from '@/utils/api/api.graphql'
import DetailDialog from '@/components/Dialogs/DetailDialog.vue'
import InputText from '@/components/InputText.vue'
const props = defineProps({
  data: Array as () => any[],
  filters: Object as () => any,
  marking: {
    type: Number,
    default: 0
  },
  dataGroup: Object as () => any
})
const filters = computed(() => props.filters)
const emit = defineEmits(['update:loadingSubData'])
const data = computed(() => props.data)
const dataSub = ref<any[]>([])
const dataSelected = ref<any>(null)
const dataSubSelected = ref<any>(null)
const dialogDetail = ref(false)
const loadingSubData = ref(false)
const isDialogEnabled = ref(false)
const parameterRequest = ref<any>({
  media: null,
  value: 0
})
const closedialogDetail = () => (dialogDetail.value = false)

const setParameterRequest = (data: any) => {
  parameterRequest.value = data
  const parameter = parameterRequest.value
  if (parameter.media != null) {
    if (parameter.value != 0) {
      isDialogEnabled.value = true
    } else {
      isDialogEnabled.value = false
    }
  } else {
    isDialogEnabled.value = false
  }
}

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

const getDetail = async () => {
  resetData()
  const { jenisLaporan } = filters.value
  const dateValue = filters.value.periode.split(' s/d ')

  const selected = dataSelected.value
  var 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: 0,
    // idUid: 0,
    // idUp3: 0,
    // namaRegional: selected?.nama_regional ? selected?.nama_regional : '',
    media: parameterRequest.value?.media,
    isMarking: props.marking,
    namaPetugas: selected?.nama_petugas ? selected?.nama_petugas : ''
  }

  // if (props.dataGroup.data == 'nama_uid') {
  //   query = {
  //     ...query,
  //     idUid: selected?.id_uid ? selected?.id_uid : 0
  //   }
  // } else if (props.dataGroup.data == 'nama_up3') {
  //   query = {
  //     ...query,
  //     idUid: selected?.id_uid ? selected?.id_uid : 0,
  //     idUp3: selected?.id_up3 ? selected?.id_up3 : 0
  //   }
  // } else if (props.dataGroup.data == 'nama_ulp') {
  //   query = {
  //     ...query,
  //     idUid: selected?.id_uid ? selected?.id_uid : 0,
  //     idUp3: selected?.id_up3 ? selected?.id_up3 : 0,
  //     idUlp: selected?.id_ulp ? selected?.id_ulp : 0
  //   }
  // }

  loadingSubData.value = true
  emit('update:loadingSubData', loadingSubData.value)
  await requestGraphQl(
    jenisLaporan.name == 'Laporan Berulang Unit'
      ? queries.anomali.gangguan.petugasBerulangDetailSub
      : jenisLaporan.name == 'Laporan Rating Negatif'
        ? queries.anomali.gangguan.petugasRatingNegatifDetailSub
        : queries.anomali.gangguan.petugasSkipStepDetailSub,
    query
  )
    .then((result) => {
      if (result.data.data != undefined) {
        if (jenisLaporan.name == 'Laporan Berulang Unit') {
          dataSub.value =
            result.data.data.subDetailAnomaliPenangananPengaduanGangguanPetugasBerulang
        } else if (jenisLaporan.name == 'Laporan Rating Negatif') {
          dataSub.value =
            result.data.data.subDetailAnomaliPenangananPengaduanGangguanPetugasRatingNegatif
        } else {
          dataSub.value =
            result.data.data.subDetailAnomaliPenangananPengaduanGangguanPetugasSkipStep
        }
        // dataSub.value = dataSub.value.map((item: any) => {
        //   return {
        //     ...item,
        //     nama_petugas: selected?.nama_petugas
        //   }
        // })
      } else {
        dataSub.value = []
      }
    })
    .catch((err) => {
      console.error(err)
    })
    .finally(() => {
      loadingSubData.value = false
      dialogDetail.value = true
      emit('update:loadingSubData', loadingSubData.value)
    })
}

const dataGridRef = ref<DxDataGrid | null>(null)
const reportMeta = ref<any>(computed(() => props.filters))
const clearSelection = () => {
  const dataGrid = dataGridRef.value!.instance!
  dataGrid.clearSelection()
}

const showDetail = () => {
  clearSelection()
  dataSub.value = []
  dataSubSelected.value = null
  if (isDialogEnabled.value) {
    getDetail()
  }
}

const onExporting = (e: any) => {
  if (e.format === 'pdf') {
    exportToPDF(reportMeta.value, data.value)
  } else if (e.format === 'xlsx') {
    exportToXLSX(reportMeta.value, e)
  } else {
    exportToDOCX(reportMeta.value, data.value)
  }
}

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

const onExportingDetail = (e: any) => {
  if (e.format === 'pdf') {
    exportDetailToPDF(reportMeta.value, dataSub.value)
  } else if (e.format === 'xlsx') {
    exportDetailToXLSX(reportMeta.value, e)
  } else {
    exportDetailToDOCX(reportMeta.value, dataSub.value)
  }
}

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