<template>
  <Filters
    @run-search="() => filterData(filters)"
    @reset-form="
      () => {
        data = []
      }
    "
    class="mb-4"
  >
    <Type4
      @update:filters="(value) => (filters = value)"
      :jenis-laporan="[
        { id: 1, name: 'Laporan Berulang Unit' },
        { id: 2, name: 'Laporan Rating Negatif' },
        { id: 3, name: 'Laporan Skip Step' }
      ]"
    />
  </Filters>

  <Anomali_LAPPKU_LPT :data="data" :loading="loadingData" :filters="filters" />
  <Anomali_LAPPKU_LPP :data="dataSecond" :filters="filters" />
</template>

<script setup lang="ts">
import Filters from '@/components/Form/Filters.vue'
import { ref } from 'vue'
import { Anomali_LAPPKU_LPT, Anomali_LAPPKU_LPP } from '../.'
import { Type4 } from '@/components/Form/FiltersType'
import { useQuery } from '@vue/apollo-composable'
import gql from 'graphql-tag'
import { queries, requestGraphQl } from '@/utils/api/api.graphql'
const data = ref<any[]>([])
const dataSecond = ref<any[]>([])
const loadingData = ref(false)
const filters = ref({
  jenisLaporan: {
    id: 1,
    name: 'Laporan Berulang Unit'
  },
  ulp: 0,
  uid: 0,
  up3: 0
})

const resetData = () => {
  data.value = []
  dataSecond.value = []
}

const filterData = async (params: any) => {
  resetData()
  const { ulp, uid, up3, jenisLaporan } = params
  console.table('LAPPGU', jenisLaporan)

  const dateValue = params.periode.split(' s/d ')
  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: ulp ? ulp.id : 0,
    idUid: uid ? uid.id : 0,
    idUp3: up3 ? up3.id : 0,
    namaRegional: ''
  }

  loadingData.value = true
  await requestGraphQl(
    jenisLaporan.name == 'Laporan Berulang Unit'
      ? queries.anomali.keluhan.unitBerulang
      : jenisLaporan.name == 'Laporan Rating Negatif'
        ? queries.anomali.keluhan.unitRatingNegatif
        : queries.anomali.keluhan.unitSkipStep,
    query
  )
    .then((result) => {
      if (result.data.data != undefined) {
        if (jenisLaporan.name == 'Laporan Berulang Unit') {
          data.value = result.data.data.anomaliLaporanPenangananPengaduanKeluhanUnitBerulang
        } else if (jenisLaporan.name == 'Laporan Rating Negatif') {
          data.value = result.data.data.anomaliLaporanPenangananPengaduanKeluhanUnitRatingNegatif
        } else {
          data.value = result.data.data.anomaliLaporanPenangananPengaduanKeluhanUnitSkipStep
        }
      } else {
        data.value = []
      }
      var unitName = ''
      if (uid.id == 0) {
        unitName = 'Semua Unit'
      } else {
        unitName = uid.name
        if (up3.id != 0) {
          unitName = up3.name
          if (ulp.id != 0) {
            unitName = ulp.name
          }
        }
      }
      dataSecond.value = [
        {
          nama_ulp: unitName,
          wo_pln_mobile: data.value.reduce((acc, cur) => acc + cur.wo_pln_mobile, 0),
          total_anomali: data.value.reduce((acc, cur) => acc + cur.anomali_pln_mobile, 0),
          persen_anomali:
            (data.value.reduce((acc, cur) => acc + cur.anomali_pln_mobile, 0) /
              data.value.reduce((acc, cur) => acc + cur.wo_pln_mobile, 0)) *
            100
        }
      ]
    })
    .catch((err) => {
      console.error(err)
    })
    .finally(() => {
      loadingData.value = false
    })
}
</script>
