import { usePostsStore } from '@/stores/posts'
import { useUlpStore } from '@/stores/ulp'
import { getUid, getUp3, getPosko, getUlp, getRegional, getUidRegional } from '@/utils/api/api.rest'
import { ref } from 'vue'
interface Item {
  id: any
  name: any
}
const months = [
  { id: 1, name: 'Januari' },
  { id: 2, name: 'Februari' },
  { id: 3, name: 'Maret' },
  { id: 4, name: 'April' },
  { id: 5, name: 'Mei' },
  { id: 6, name: 'Juni' },
  { id: 7, name: 'Juli' },
  { id: 8, name: 'Agustus' },
  { id: 9, name: 'September' },
  { id: 10, name: 'Oktober' },
  { id: 11, name: 'November' },
  { id: 12, name: 'Desember' }
]
// create 4 year back array
const year = new Date().getFullYear()
const years = ref<Item[]>([])
for (let i = 0; i < 5; i++) {
  years.value.push({ id: year - i, name: year - i })
}
const itemsUid = ref<Item[]>([])
const itemsUp3 = ref<Item[]>([])
const itemsPosko = ref<Item[]>([])
const itemsUlp = ref<Item[]>([])
const itemsRegional = ref<Item[]>([])
const itemsMedia = ref<Item[]>([])
// Fetch data from the API using Axios
const fetchMedia = () => {
  itemsMedia.value = [
    { id: 'Ulasan Aplikasi PLN Mobile', name: 'Ulasan Aplikasi PLN Mobile' },
    { id: 'Twitter', name: 'Twitter' },
    { id: 'PLN Mobile', name: 'PLN Mobile' },
    { id: 'Media Massa', name: 'Media Massa' },
    { id: 'Live Chat Website', name: 'Live Chat Website' },
    { id: 'Live Chat PLN Mobile', name: 'Live Chat PLN Mobile' },
    { id: 'Instagram', name: 'Instagram' },
    { id: 'Facebook', name: 'Facebook' },
    { id: 'Email', name: 'Email' },
    { id: 'EMS', name: 'EMS' },
    { id: 'Datang ke Kantor', name: 'Datang ke Kantor' },
    { id: 'Call ke Kantor Unit', name: 'Call ke Kantor Unit' },
    { id: 'Call PLN 123', name: 'Call PLN 123' }
  ]
}
const fetchUidByRegional = async (regional: number) => {
  if (regional == 0) {
    itemsUid.value = []
  } else {
    try {
      const res = await getUidRegional(regional)
      itemsUid.value = res.data.map((item: any) => ({
        id: item.id,
        name: item.nama.toUpperCase()
      }))
    } catch (error) {
      console.error('Error fetching data:', error)
    }
  }
}
// Fetch data from the API using Axios
const fetchUid = async () => {
  try {
    const res = await getUid()
    itemsUid.value = res.data.map((item: any) => ({
      id: item.id,
      name: item.nama
    }))
  } catch (error) {
    console.error('Error fetching data:', error)
  }
}

const fetchDataUp3 = async (uid: number) => {
  if (uid == 0) {
    itemsUp3.value = []
    itemsUlp.value = []
    itemsPosko.value = []
  } else {
    try {
      const res = await getUp3(uid)
      itemsUp3.value = res.data.map((item: any) => ({
        id: item.id,
        name: item.nama
      }))
    } catch (error) {
      console.error('Error fetching data:', error)
    }
  }
}
const fetchDataUlp = async (up3: number) => {
  if (up3 == 0) {
    itemsUlp.value = []
  } else {
    try {
      const res = await getUlp(up3)
      itemsUlp.value = res.data.map((item: any) => ({
        id: item.id,
        name: item.nama
      }))
    } catch (error) {
      console.error('Error fetching data:', error)
    }
  }
}
const fetchDataPosko = async (up3: number) => {
  if (up3 == 0) {
    itemsPosko.value = []
  } else {
    try {
      const res = await getPosko(up3)
      itemsPosko.value = res.data.map((item: any) => ({
        id: item.id,
        name: item.nama
      }))
    } catch (error) {
      console.error('Error fetching data:', error)
    }
  }
}
const fetchRegional = async () => {
  try {
    const res = await getRegional()
    itemsRegional.value = res.data.map((item: any) => ({
      id: item.id,
      name: String(item.nama).toUpperCase()
    }))
  } catch (error) {
    console.error('Error fetching data:', error)
  }
}

const formatWaktu = (durasiDetik: number): string => {
  const hari = Math.floor(durasiDetik / (3600 * 24))
  const sisaDetik = durasiDetik % (3600 * 24)
  const jam = Math.floor(sisaDetik / 3600)
  const sisaDetik2 = sisaDetik % 3600
  const menit = Math.floor(sisaDetik2 / 60)
  const detik = sisaDetik2 % 60

  return `${hari} - ${jam} : ${menit} : ${detik}`
}

const selectedUid = (value: any) => {
  fetchDataUp3(value.id)
}
const selectedUp3Posko = (value: any) => {
  if (value.id != 0) {
    fetchDataPosko(value.id)
  }
}
const selectedUp3Ulp = (value: any) => {
  if (value.id != 0) {
    fetchDataUlp(value.id)
  }
}
const selectedPosko = (value: any) => {
  usePostsStore().setData(value.id)
}
const selectedUlp = (value: any) => {
  useUlpStore().setData(value.id)
}

export {
  selectedUid,
  selectedUp3Posko,
  selectedUp3Ulp,
  selectedPosko,
  selectedUlp,
  fetchRegional,
  fetchUidByRegional,
  fetchUid,
  fetchMedia,
  itemsUid,
  itemsUp3,
  itemsPosko,
  itemsRegional,
  itemsMedia,
  itemsUlp,
  months,
  years,
  formatWaktu
}
