<script setup lang="ts">
import InputNumber from '@/components/Form/InputNumber.vue'
import Select from '@/components/Select.vue'
import DatePicker from '@/components/DatePicker.vue'
import {
  selectedUid,
  selectedUp3Posko,
  selectedPosko,
  fetchUid,
  itemsUid,
  itemsUp3,
  itemsPosko
} from './reference'
import { onMounted, ref } from 'vue'

const emit = defineEmits(['update:filters'])
const uidPlaceholder = 'Semua Unit Induk Distribusi/Wilayah'
const up3Placeholder = 'Semua Unit Pelaksanaan Pelayanan Pelanggan'
const poskoPlaceholder = 'Semua Posko'
const up3 = ref({ id: 0, name: up3Placeholder })
const uid = ref({ id: 0, name: uidPlaceholder })
const posko = ref({ id: 0, name: poskoPlaceholder })
const data = ref({
  uid: uid.value,
  up3: up3.value,
  posko: posko.value,
  periode: '',
  minJmlLapor: 1,
  maxJmlLapor: 1
})

const setUid = (value: any) => {
  uid.value = value
  selectedUid(value)
  up3.value = { id: 0, name: up3Placeholder }
  data.value.uid = value
}

const setUp3 = (value: any) => {
  up3.value = value
  selectedUp3Posko(value)
  posko.value = { id: 0, name: poskoPlaceholder }
  data.value.up3 = value
}

const setPosko = (value: any) => {
  posko.value = value
  selectedPosko(value)
  data.value.posko = value
}

onMounted(() => {
  fetchUid()
  emit('update:filters', data.value)
})
</script>

<template>
  <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
    <div class="flex flex-col flex-1 space-y-2">
      <label class="filter-input-label">Unit Induk Distribusi/Wilayah:</label>

      <Select @update:selected="setUid($event)" :data="itemsUid" :placeholder="uidPlaceholder" />
    </div>

    <div class="flex flex-col flex-1 space-y-2">
      <label class="filter-input-label">Unit Pelaksanaan Pelayanan Pelanggan:</label>

      <Select @update:selected="setUp3($event)" :data="itemsUp3" :selected="up3" :placeholder="up3Placeholder" />
    </div>

    <div class="flex flex-col flex-1 space-y-2">
      <label class="filter-input-label">Posko:</label>

      <Select @update:selected="setPosko($event)" :data="itemsPosko" :selected="posko"
        :placeholder="poskoPlaceholder" />
    </div>

    <div class="flex flex-col flex-1 space-y-2">
      <label class="filter-input-label">Periode Tanggal:</label>

      <DatePicker @update:date-value="(value) => (data.periode = value)" />
    </div>

    <div class="flex flex-col flex-1 space-y-2">
      <label class="filter-input-label">Lapor Ulang:</label>

      <div class="flex flex-1 justify-between gap-x-1.5">
        <InputNumber :value="data.minJmlLapor" @update:time-value="(value) => (data.minJmlLapor = value)"
          class="flex flex-1" />
        <small class="flex items-center">s/d</small>
        <InputNumber :value="data.maxJmlLapor" @update:time-value="(value) => (data.maxJmlLapor = value)"
          class="flex flex-1" />
      </div>
    </div>
  </div>

</template>
