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

    <Anomali_LAPPKU_LPT :data="data" />
    <Anomali_LAPPKU_LPP :data="data" />
</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'
const data = ref<any[]>([])

const GET_laporanCheckInCheckOut = gql`
  query laporanCheckInCheckOut($dateFrom: Date!, $dateTo: Date!, $posko: String, $idUid: Int, $idUp3: Int) {
    laporanCheckInCheckOut(
      dateFrom: $dateFrom
      dateTo: $dateTo
      posko: $posko
      idUid: $idUid
      idUp3: $idUp3
    ) {
    avg_durasi_wo_gangguan_individual
    avg_durasi_wo_penugasan_khusus
    avg_rct_wo_gangguan_individual
    avg_rpt_wo_gangguan_individual
    jumlah_wo_gangguan_individual
    jumlah_wo_penugasan_khusus
    personil_yantek
    user_regu
    }
  }
`
const filterData = (params: any) => {
    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,
                    }
                ]
            })
        }
        console.log(queryResult.data)
        console.log(queryResult.loading)
        console.log(queryResult.networkStatus)
    })
    onError((error) => {
        console.log(error)
    })
}
const { onResult, onError, loading, refetch } = useQuery(
    GET_laporanCheckInCheckOut,
    {
        dateFrom: new Date().toISOString().slice(0, 10),
        dateTo: new Date().toISOString().slice(0, 10),
        posko: '',
        idUid: 0,
        idUp3: 0
    }
)
const filters = ref()
</script>