<script setup lang="ts">
import Select from '@/components/Select.vue';
import DatePicker from '@/components/DatePicker.vue';
import InputWithSuffix from '../InputWithSuffix.vue';
import { selectedUid, selectedUp3Ulp, selectedUlp, fetchUid, itemsUid, itemsUp3, itemsUlp } from './reference';
import { onMounted, ref } from 'vue';
const uidPlaceholder = 'Semua Unit Induk Distribusi/Wilayah';
const up3Placeholder = 'Semua Unit Pelaksanaan Pelayanan Pelanggan';
const ulpPlaceholder = 'Semua Unit Layanan Pelanggan';
const uppp = ref({ id: 0, name: up3Placeholder });
const uid = ref({ id: 0, name: uidPlaceholder });
const ulp = ref({ id: 0, name: ulpPlaceholder });
const emit = defineEmits(['update:filters'])
const data = ref({
  uid: uid.value,
  up3: uppp.value,
  ulp: ulp.value,
  periode: '',
  minDurasiResponseTime: 1,
  maxDurasiResponseTime: 1
})
const setUid = (value: any) => {
  uid.value = value;
  selectedUid(value);
  uppp.value = { id: 0, name: up3Placeholder };
  data.value.uid = value;
};

const setUp3 = (value: any) => {
  uppp.value = value;
  selectedUp3Ulp(value);
  ulp.value = { id: 0, name: ulpPlaceholder };
  data.value.up3 = value;
};

const setUlp = (value: any) => {
  ulp.value = value;
  selectedUlp(value);
  data.value.ulp = value;
};
const triggerInput = ref(false)
const sla = [
  {
    id: 0,
    name: 'Durasi Menit'
  },
  {
    id: 1,
    name: 'Dibawah  / Sesuai SLA (<= 45 menit)'
  },
  {
    id: 2,
    name: 'Melebihi SLA (> 45 menit)'
  }
];
const changeDuration = (value: any) => {
  if (value.id === 0) {
    console.log('Durasi Menit')
    data.value.minDurasiResponseTime = 0
    data.value.maxDurasiResponseTime = 5
    triggerInput.value = false
  } else if (value.id === 1) {
    data.value.minDurasiResponseTime = 0
    data.value.maxDurasiResponseTime = 45
    console.log('Dibawah  / Sesuai SLA (<= 45 menit)')
    triggerInput.value = true
  } else {
    data.value.minDurasiResponseTime = 46
    data.value.maxDurasiResponseTime = 99999 * 60 * 24
    triggerInput.value = true
    console.log('Melebihi SLA (> 45 menit)')
  }

}
onMounted(() => {
  emit('update:filters', data.value)
  fetchUid()
})
</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" :placeholder="up3Placeholder" />
    </div>

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

      <Select @update:selected="setUlp($event)" :data="itemsUlp" :placeholder="ulpPlaceholder" />
    </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">Durasi:</label>

      <div class="flex flex-col gap-y-1">
        <Select @update:selected="changeDuration($event)" :data="sla" placeholder="Durasi Menit" />

        <div class="flex flex-1 justify-between gap-x-1.5">
          <InputWithSuffix @update:minute-value="(value: any) => data.minDurasiResponseTime = value" @value="data.minDurasiResponseTime" :disabled=triggerInput
            class="flex flex-1" />
          <small class="flex items-center">s/d</small>
          <InputWithSuffix @update:minute-value="(value: any) => data.maxDurasiResponseTime = value" @value="data.maxDurasiResponseTime"
            :disabled="triggerInput" class="flex flex-1" />
        </div>
      </div>
    </div>
  </div>

</template>
