<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
    @cell-click="onCellClicked"
    ref="dataGridRef"
    :allow-column-reordering="true"
    class="max-h-[calc(100vh-140px)] mb-10"
    :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"
    :data-source="data"
    v-if="loadingData == false"
  >
    <DxGrouping :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"
      name="namaULP"
    />

    <DxColumn
      v-if="grouping.length > 0"
      v-for="(group, index) in grouping"
      :key="index"
      :width="150"
      alignment="center"
      :data-field="group.data"
      :caption="group.caption"
      css-class="custom-table-column"
      cell-template="formatText"
      :group-index="index"
    />
    <DxColumn alignment="center" caption="Total Petugas" css-class="custom-table-column">
      <DxColumn
        :width="170"
        alignment="center"
        data-field="total_petugas"
        caption="a"
        css-class="custom-table-column"
        cell-template="formatNumber"
      />
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="Total Petugas Yang Pengaduan Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn alignment="center" caption="b" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="petugas_pln_mobile_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatNumberPLNMobileMarking"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="petugas_pln_mobile_nonmarking"
            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="c" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="petugas_cc123_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatNumberCCMarking"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="petugas_cc123_nonmarking"
            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="d=b+c" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="petugas_anomali_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatNumberTotalMarking"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="petugas_anomali_nonmarking"
            data-type="number"
            caption="Non Marking"
            css-class="custom-table-column"
            cell-template="formatNumberTotalNonMarking"
          />
        </DxColumn>
      </DxColumn>
    </DxColumn>
    <DxColumn
      alignment="center"
      caption="% Petugas Yang Pengaduan Diselesaikan Secara Anomali"
      css-class="custom-table-column"
    >
      <DxColumn alignment="center" caption="PLN Mobile" css-class="custom-table-column">
        <DxColumn alignment="center" caption="e=b/a" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_pln_mobile_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_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="f=c/a" css-class="custom-table-column">
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_cc123_marking"
            data-type="number"
            caption="Marking"
            css-class="custom-table-column"
            cell-template="formatPercentage"
          />
          <DxColumn
            :width="150"
            alignment="center"
            data-field="persen_cc123_nonmarking"
            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="g=e+f" 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({ media: 'PLN Mobile', marking: 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({ media: 'PLN Mobile', marking: 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({ media: 'Call PLN 123', marking: 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({ media: 'Call PLN 123', marking: 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({ media: '', marking: 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({ media: '', marking: 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({ media: null, marking: 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({ media: null, marking: null })"
      >
        {{ isNumber(data.text) ? formatPercentage(data.text) : data.text }}
      </p>
    </template>

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

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

    <DxSummary :calculate-custom-summary="calculateCustomSummary">
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="total_petugas"
        name="total_petugas"
        summary-type="custom"
        css-class="!text-right"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="petugas_pln_mobile_marking"
        name="petugas_pln_mobile_marking"
        summary-type="custom"
        css-class="!text-right cursor-pointer"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="petugas_pln_mobile_nonmarking"
        name="petugas_pln_mobile_nonmarking"
        summary-type="custom"
        css-class="!text-right cursor-pointer"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="petugas_cc123_marking"
        name="petugas_cc123_marking"
        summary-type="custom"
        css-class="!text-right cursor-pointer"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="petugas_cc123_nonmarking"
        name="petugas_cc123_nonmarking"
        summary-type="custom"
        css-class="!text-right cursor-pointer"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="petugas_anomali_marking"
        name="petugas_anomali_marking"
        summary-type="custom"
        css-class="!text-right cursor-pointer"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="petugas_anomali_nonmarking"
        name="petugas_anomali_nonmarking"
        summary-type="custom"
        css-class="!text-right cursor-pointer"
        :customize-text="(e: any) => formatNumber(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_pln_mobile_marking"
        name="persen_pln_mobile_marking"
        summary-type="custom"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_pln_mobile_non_marking"
        name="persen_pln_mobile_non_marking"
        summary-type="custom"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_cc123_marking"
        name="persen_cc123_marking"
        summary-type="custom"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
      <DxGroupItem
        :show-in-group-footer="false"
        :align-by-column="true"
        column="persen_cc123_nonmarking"
        name="persen_cc123_nonmarking"
        summary-type="custom"
        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"
        name="persen_anomali_marking"
        summary-type="custom"
        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"
        name="persen_anomali_non_marking"
        summary-type="custom"
        css-class="!text-right"
        :customize-text="(e: any) => formatPercentage(e.value)"
      />
    </DxSummary>
  </DxDataGrid>

  <div class="mt-4 lg:mt-6 max-w-7xl">
    <h1 class="text-xl font-medium md:text-2xl text-dark">
      Detail Petugas {{ parameterRequest.media != null ? parameterRequest.media : '' }}
      {{
        parameterRequest.marking != null
          ? parameterRequest.marking == 1
            ? 'Marking'
            : 'Non Marking'
          : ''
      }}
    </h1>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { DxDataGrid } from 'devextreme-vue'
import {
  DxColumn,
  DxColumnFixing,
  DxExport,
  DxGroupItem,
  DxGrouping,
  DxPaging,
  DxScrolling,
  DxSearchPanel,
  DxSelection,
  DxSummary
} from 'devextreme-vue/data-grid'
import { formatNumber, formatPercentage, isNumber } from '@/utils/numbers'
import { formatWaktu } from '@/components/Form/FiltersType/reference'
import { queries, requestGraphQl } from '@/utils/api/api.graphql'
import {
  exportToPDF,
  exportToXLSX,
  exportToDOCX
} from '@/report/Anomali/Gangguan/Anomali_LAPPGP_LPT'
import { getDataRowGroup } from '@/utils/helper'

const position = { of: '#data' }
const showIndicator = ref(true)
const shading = ref(true)
const showPane = ref(true)
const props = defineProps({
  data: Array as () => any[],
  filters: Object as () => any,
  grouping: {
    type: Array as () => any[],
    default: []
  },
  loadingData: Boolean
})
const emit = defineEmits([
  'update:dataSub',
  'update:loadingSubData',
  'update:marking',
  'update:data-group'
])
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 loadingSubData = ref(false)
const isDialogEnabled = ref(false)
const reportMeta = ref<any>(computed(() => props.filters))
const parameterRequest = ref<any>({
  media: null,
  marking: null
})
const groupDialog = ref(false)
const groupData = ref<any>(null)
const groupIndex = ref(0)

const setParameterRequest = (data: any) => {
  parameterRequest.value = data
  const parameter = parameterRequest.value

  if (parameter.media != null && parameter.marking != null) {
    emit('update:marking', parameter.marking)
    isDialogEnabled.value = true
  } else {
    isDialogEnabled.value = false
  }
}

const onCellClicked = (e: any) => {
  if (e.rowType == 'group') {
    groupDialog.value = true
    groupIndex.value = e.row.groupIndex
    console.log(e.data)
    console.log('columnIndex', e)
    console.log('columnValue', e.data.aggregates[e.column.columnIndex])

    if (e.column.dataField == 'petugas_pln_mobile_marking') {
      setParameterRequest({ media: 'PLN Mobile', marking: 1 })
    } else if (e.column.dataField == 'petugas_pln_mobile_nonmarking') {
      setParameterRequest({ media: 'PLN Mobile', marking: 2 })
    } else if (e.column.dataField == 'petugas_cc123_marking') {
      setParameterRequest({ media: 'Call PLN 123', marking: 1 })
    } else if (e.column.dataField == 'petugas_cc123_nonmarking') {
      setParameterRequest({ media: 'Call PLN 123', marking: 2 })
    } else if (e.column.dataField == 'petugas_anomali_marking') {
      setParameterRequest({ media: '', marking: 1 })
    } else if (e.column.dataField == 'petugas_anomali_nonmarking') {
      setParameterRequest({ media: '', marking: 2 })
    } else {
      setParameterRequest({ media: null, marking: null })
    }
    groupData.value = getDataRowGroup(e.data)
    showDetail()
  } else {
    groupDialog.value = false
  }
}

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

let total_petugas = 0
let petugas_pln_mobile_marking = 0
let petugas_pln_mobile_nonmarking = 0
let petugas_cc123_marking = 0
let petugas_cc123_nonmarking = 0
let petugas_anomali_marking = 0
let petugas_anomali_nonmarking = 0
let persen_pln_mobile_marking = 0
let persen_pln_mobile_non_marking = 0
let persen_cc123_marking = 0
let persen_cc123_nonmarking = 0
let persen_anomali_marking = 0
let persen_anomali_non_marking = 0

const calculateCustomSummary = (options: any) => {
  if (options.name === 'total_petugas') {
    if (options.summaryProcess === 'calculate') {
      total_petugas += options.value
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = total_petugas
    }
  }

  if (options.name === 'petugas_pln_mobile_marking') {
    if (options.summaryProcess === 'calculate') {
      petugas_pln_mobile_marking += options.value
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = petugas_pln_mobile_marking
    }
  }

  if (options.name === 'petugas_pln_mobile_nonmarking') {
    if (options.summaryProcess === 'calculate') {
      petugas_pln_mobile_nonmarking += options.value
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = petugas_pln_mobile_nonmarking
    }
  }

  if (options.name === 'petugas_cc123_marking') {
    if (options.summaryProcess === 'calculate') {
      petugas_cc123_marking += options.value
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = petugas_cc123_marking
    }
  }

  if (options.name === 'petugas_cc123_nonmarking') {
    if (options.summaryProcess === 'calculate') {
      petugas_cc123_nonmarking += options.value
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = petugas_cc123_nonmarking
    }
  }

  if (options.name === 'petugas_anomali_marking') {
    if (options.summaryProcess === 'start') {
      petugas_pln_mobile_marking = 0
      petugas_cc123_marking = 0
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = petugas_pln_mobile_marking + petugas_cc123_marking
    }
  }

  if (options.name === 'petugas_anomali_nonmarking') {
    if (options.summaryProcess === 'start') {
      petugas_pln_mobile_nonmarking = 0
      petugas_cc123_nonmarking = 0
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = petugas_pln_mobile_nonmarking + petugas_cc123_nonmarking
    }
  }

  if (options.name === 'persen_pln_mobile_marking') {
    if (options.summaryProcess === 'start') {
      petugas_pln_mobile_marking = 0
      total_petugas = 0
    } else if (options.summaryProcess === 'calculate') {
      persen_pln_mobile_marking =
        !isFinite(petugas_pln_mobile_marking / total_petugas) || petugas_pln_mobile_marking == 0
          ? 0
          : (petugas_pln_mobile_marking / total_petugas) * 100
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue =
        !isFinite(petugas_pln_mobile_marking / total_petugas) || petugas_pln_mobile_marking == 0
          ? 0
          : (petugas_pln_mobile_marking / total_petugas) * 100
    }
  }

  if (options.name === 'persen_pln_mobile_non_marking') {
    if (options.summaryProcess === 'start') {
      petugas_pln_mobile_nonmarking = 0
      total_petugas = 0
    } else if (options.summaryProcess === 'calculate') {
      persen_pln_mobile_non_marking =
        !isFinite(petugas_pln_mobile_nonmarking / total_petugas) ||
        petugas_pln_mobile_nonmarking == 0
          ? 0
          : (petugas_pln_mobile_nonmarking / total_petugas) * 100
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue =
        !isFinite(petugas_pln_mobile_nonmarking / total_petugas) ||
        petugas_pln_mobile_nonmarking == 0
          ? 0
          : (petugas_pln_mobile_nonmarking / total_petugas) * 100
    }
  }

  if (options.name === 'persen_cc123_marking') {
    if (options.summaryProcess === 'start') {
      petugas_cc123_marking = 0
      total_petugas = 0
    } else if (options.summaryProcess === 'calculate') {
      persen_cc123_marking =
        !isFinite(petugas_cc123_marking / total_petugas) || petugas_cc123_marking == 0
          ? 0
          : (petugas_cc123_marking / total_petugas) * 100
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue =
        !isFinite(petugas_cc123_marking / total_petugas) || petugas_cc123_marking == 0
          ? 0
          : (petugas_cc123_marking / total_petugas) * 100
    }
  }

  if (options.name === 'persen_cc123_nonmarking') {
    if (options.summaryProcess === 'start') {
      petugas_cc123_nonmarking = 0
      total_petugas = 0
    } else if (options.summaryProcess === 'calculate') {
      persen_cc123_nonmarking =
        !isFinite(petugas_cc123_nonmarking / total_petugas) || petugas_cc123_nonmarking == 0
          ? 0
          : (petugas_cc123_nonmarking / total_petugas) * 100
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue =
        !isFinite(petugas_cc123_nonmarking / total_petugas) || petugas_cc123_nonmarking == 0
          ? 0
          : (petugas_cc123_nonmarking / total_petugas) * 100
    }
  }

  if (options.name === 'persen_anomali_marking') {
    if (options.summaryProcess === 'start') {
      persen_pln_mobile_marking = 0
      persen_cc123_marking = 0
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = persen_pln_mobile_marking + persen_cc123_marking
    }
  }

  if (options.name === 'persen_anomali_non_marking') {
    if (options.summaryProcess === 'start') {
      persen_cc123_nonmarking = 0
      persen_pln_mobile_non_marking = 0
    } else if (options.summaryProcess === 'finalize') {
      options.totalValue = persen_pln_mobile_non_marking + persen_cc123_nonmarking
    }
  }
}

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: '',
    media: parameterRequest.value?.media,
    isMarking: parameterRequest.value?.marking
  }

  if (groupDialog.value) {
    if (props.grouping[groupIndex.value].data == 'nama_uid') {
      query = {
        ...query,
        idUid: groupData.value?.id_uid ? groupData.value?.id_uid : 0,
        namaRegional: groupData.value?.nama_regional ? groupData.value?.nama_regional : ''
      }
    } else if (props.grouping[groupIndex.value].data == 'nama_up3') {
      query = {
        ...query,
        idUid: groupData.value?.id_uid ? groupData.value?.id_uid : 0,
        idUp3: groupData.value?.id_up3 ? groupData.value?.id_up3 : 0,
        namaRegional: groupData.value?.nama_regional ? groupData.value?.nama_regional : ''
      }
    }
  } else {
    query = {
      ...query,
      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: selected?.nama_regional ? selected?.nama_regional : '',
      media: parameterRequest.value?.media,
      isMarking: parameterRequest.value?.marking
    }
  }

  loadingSubData.value = true
  emit('update:loadingSubData', loadingSubData.value)
  await requestGraphQl(
    jenisLaporan.name == 'Laporan Berulang Unit'
      ? queries.anomali.gangguan.petugasBerulangDetail
      : jenisLaporan.name == 'Laporan Rating Negatif'
        ? queries.anomali.gangguan.petugasRatingNegatifDetail
        : queries.anomali.gangguan.petugasSkipStepDetail,
    query
  )
    .then((result) => {
      if (result.data.data != undefined) {
        if (jenisLaporan.name == 'Laporan Berulang Unit') {
          dataSub.value = result.data.data.detailAnomaliPenangananPengaduanGangguanPetugasBerulang
        } else if (jenisLaporan.name == 'Laporan Rating Negatif') {
          dataSub.value =
            result.data.data.detailAnomaliPenangananPengaduanGangguanPetugasRatingNegatif
        } else {
          dataSub.value = result.data.data.detailAnomaliPenangananPengaduanGangguanPetugasSkipStep
        }
      } else {
        dataSub.value = []
      }
      console.log('group by', props.grouping[groupIndex.value].data)

      dataSub.value = dataSub.value.map((e: any) => ({
        ...e,
        nama_unit: groupDialog.value
          ? props.grouping[groupIndex.value].data == 'nama_uid'
            ? e.nama_uid || e.nama_unit
            : props.grouping[groupIndex.value].data == 'nama_up3'
              ? e.nama_up3 || e.nama_unit
              : 'Semua Unit'
          : e.nama_unit
      }))
      emit('update:dataSub', dataSub.value)
      emit('update:data-group', props.grouping[groupIndex.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 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()
  }
}
</script>
