<template>
  <Filters @run-search="() => filterData(filters)" class="mb-4">
    <Type4 @update:filters="(value) => (filters = value)" />
  </Filters>

  <Anomali_LAPPGP_LPT :data="data" :loading="loading" />
  <Anomali_LAPPGP_LPP :data="data" />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Anomali_LAPPGP_LPT, Anomali_LAPPGP_LPP } from '../.'
import { useQuery } from '@vue/apollo-composable'
import gql from 'graphql-tag'
import { Type4 } from '@/components/Form/FiltersType'
import Filters from '@/components/Form/Filters.vue'
const data = ref<any[]>([])

const GET_laporanAnomaliPenangananPengaduanGangguanPetugas = gql`
  query laporanAnomaliPenangananPengaduanGangguanPetugas(
    $dateFrom: Date!
    $dateTo: Date!
    $jenisLaporan: Int
    $idUid: Int
    $idUp3: Int
    $posko: String
  ) {
    laporanAnomaliPenangananPengaduanGangguanPetugas(
      dateFrom: $dateFrom
      dateTo: $dateTo
      jenisLaporan: $jenisLaporan
      idUid: $idUid
      idUp3: $idUp3
      posko: $posko
    ) {
      nama_posko
      persen_penyelesaian_petugas_anomali_cc_123_marking
      persen_penyelesaian_petugas_anomali_cc_123_non_marking
      persen_penyelesaian_petugas_anomali_marking
      persen_penyelesaian_petugas_anomali_non_marking
      persen_penyelesaian_petugas_anomali_pln_mobile_marking
      persen_penyelesaian_petugas_anomali_pln_mobile_non_marking
      total_penyelesaian_petugas_anomali_cc_123_marking
      total_penyelesaian_petugas_anomali_cc_123_non_marking
      total_penyelesaian_petugas_anomali_marking
      total_penyelesaian_petugas_anomali_non_marking
      total_penyelesaian_petugas_anomali_pln_mobile_marking
      total_penyelesaian_petugas_anomali_pln_mobile_non_marking
      total_petugas
    }
  }
`
const filterData = (params: any) => {
  console.table(params)

  const { posko, uid, up3 } = params
  const dateValue = params.periode.split(' s/d ')
  refetch({
    dateFrom: dateValue[0]
      ? dateValue[0].split('-').reverse().join('-')
      : new Date().toISOString().slice(0, 10),
    dateTo: dateValue[1]
      ? dateValue[1].split('-').reverse().join('-')
      : new Date().toISOString().slice(0, 10),
    posko: posko ? posko.id : '',
    idUid: uid ? uid.id : 0,
    idUp3: up3 ? up3.id : 0
  })
  onResult((queryResult) => {
    if (queryResult.data != undefined) {
      queryResult.data.daftarGangguanDialihkanKePoskoLain.forEach((item: any) => {
        data.value = [
          ...data.value,
          {
            ...item,
            total_penyelesaian_petugas_anomali_cc_123:
              item.total_penyelesaian_petugas_anomali_cc_123_marking +
              item.total_penyelesaian_petugas_anomali_cc_123_non_marking,
            total_penyelesaian_petugas_anomali_pln_mobile:
              item.total_penyelesaian_petugas_anomali_pln_mobile_marking +
              item.total_penyelesaian_petugas_anomali_pln_mobile_non_marking,
            total_penyelesaian_petugas_anomali:
              item.total_penyelesaian_petugas_anomali_marking +
              item.total_penyelesaian_petugas_anomali_non_marking
          }
        ]
      })
    }
    console.log(queryResult.data)
    console.log(queryResult.loading)
    console.log(queryResult.networkStatus)
  })
  onError((error) => {
    console.log(error)
  })
}
const { onResult, onError, loading, refetch } = useQuery(
  GET_laporanAnomaliPenangananPengaduanGangguanPetugas,
  {
    dateFrom: new Date().toISOString().slice(0, 10),
    dateTo: new Date().toISOString().slice(0, 10),
    posko: '',
    idUid: 0,
    idUp3: 0
  }
)
const filters = ref()
</script>
