<script setup>
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat"

dayjs.extend(customParseFormat)

import VueDatePicker from '@vuepic/vue-datepicker'
import '@vuepic/vue-datepicker/dist/main.css'


definePageMeta({
  middleware: "auth"
})

const dataStore = useDataStore()
const supabase = useSupabaseClient()
const user = useSupabaseUser()
const toast = useToast()


const timeInfo = ref({
  user: "",
  start: "",
  end: null,
  notes: null,
})

const filterUser = ref(user.value.id || "")


const filteredRows = computed(() => {

  let times = dataStore.workingtimes

  if(dataStore.hasRight('viewTimes')) {
    if(filterUser.value !== "") {
      times = times.filter(i => i.profile === filterUser.value)
    }
  } else if(dataStore.hasRight('viewOwnTimes')) {
    times = times.filter(i => i.profile === user.value.id)
  } else {
    times  = []
  }

  return times

})



const itemInfo = ref({
  user: "",
  start: new Date(),
  end: "",
  notes: null,
  state: "Entwurf"
})


const columns = [
  {
    key:"state",
    label: "Status",
    sortable:true
  },
  {
    key: "profile",
    label: "Mitarbeiter",
    sortable:true
  },
  {
    key: "date",
    label: "Datum",
    sortable:true
  },
  {
    key:"start",
    label:"Start",
    sortable:true
  },
  {
    key: "end",
    label: "Ende",
    sortable:true
  },
  {
    key: "duration",
    label: "Dauer",
    sortable:true
  },
  {
    key: "notes",
    label: "Notizen",
    sortable:true
  }
]

const runningTimeInfo = ref({})
const showConfigTimeModal = ref(false)
const configTimeMode = ref("create")



const startTime = async () => {
  console.log("started")
  timeInfo.value = {
    user: user.value.id,
    start: dayjs().format("HH:mm:ssZ"),
    date: dayjs().format("YYYY-MM-DD"),
    tenant: dataStore.currentTenant,
    state: "Im Web gestartet"
  }

  const {data,error} = await supabase
      .from("workingtimes")
      .insert([timeInfo.value])
      .select()

  if(error) {
    console.log(error)
  } else if(data) {
    //timeInfo.value = data[0]
    await dataStore.fetchWorkingTimes()
    runningTimeInfo.value = dataStore.times.find(time => time.profile === user.value.id && !time.end)
  }

}

const stopStartedTime = async () => {
  //console.log(runningTimeInfo.value)

  runningTimeInfo.value.end = dayjs().format("HH:mm:ssZ")
  runningTimeInfo.value.state = "Im Web gestoppt"

  /*const mapNumRange = (num, inMin, inMax, outMin, outMax) =>
      ((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;*/

  //runningTimeInfo.value.duration = Math.round(mapNumRange(Math.abs(new Date(runningTimeInfo.value.end) - new Date(runningTimeInfo.value.start))/1000/60,0,60,0,1)*100)/100

  const {data,error} = await supabase
      .from("workingtimes")
      .update(runningTimeInfo.value)
      .eq('id',runningTimeInfo.value.id)
      .select()
  console.log(data)

  if(error) {
    console.log(error)
  } else {
    toast.add({title: "Zeit erfolgreich gestoppt"})
    runningTimeInfo.value = {}
    dataStore.fetchWorkingTimes()
  }
}

if(dataStore.workingtimes.find(time => time.profile == user.value.id && !time.end)) {
  runningTimeInfo.value = dataStore.workingtimes.find(time => time.profile == user.value.id && !time.end)
}


const createTime = async () => {
  const {data,error} = await supabase
      .from("times")
      .insert({...itemInfo.value, tenant: dataStore.currentTenant})
      .select()

  if(error) {
    console.log(error)
  } else if(data) {
    itemInfo.value = {}
    toast.add({title: "Zeit erfolgreich erstellt"})
    showConfigTimeModal.value = false
    await dataStore.fetchTimes()

  }
}

const updateTime = async () => {
  const {error} = await supabase
      .from("workingTimes")
      .update(itemInfo.value)
      .eq('id',itemInfo.value.id)

  if(error) {
    console.log(error)
  }

  toast.add({title: "Zeit erfolgreich gespeichert"})
  showConfigTimeModal.value = false
  await dataStore.fetchTimes()
}

const format = (date) => {
  let dateFormat = dayjs(date).format("DD.MM.YY HH:mm")

  return `${dateFormat}`;
}

const getDuration = (time) => {
  const minutes = Math.floor(dayjs(time.end, "HH:mm:ssZ").diff(dayjs(time.start, "HH:mm:ssZ"),'minutes',true))
  const hours = Math.floor(minutes/60)
  return {
    //dezimal: dez,
    hours: hours,
    minutes: minutes,
    composed: `${hours}:${String(minutes % 60).padStart(2,"0")} h`
  }
}

const setState = async (newState) => {
  itemInfo.value.state = newState
  await updateTime()
}

</script>

<template>
  <UDashboardNavbar title="Zeiterfassung">

  </UDashboardNavbar>
  <UDashboardToolbar>
    <template #left>
      <UButton
          @click="startTime"
          :disabled="runningTimeInfo.id "
      >
        Start
      </UButton>
      <UButton
          @click="configTimeMode = 'create'; itemInfo = {start: new Date(), end: new Date(), user: user.id, state: 'Entwurf'}; showConfigTimeModal = true"
      >
        Erstellen
      </UButton>
      <USelectMenu
          v-if="dataStore.hasRight('viewTimes')"
          :options="dataStore.profiles"
          option-attribute="fullName"
          value-attribute="id"
          v-model="filterUser"
      >
        <template #label>
          {{dataStore.getProfileById(filterUser) ? dataStore.getProfileById(filterUser).fullName : "Kein Benutzer ausgewählt"}}
        </template>
      </USelectMenu>
    </template>
  </UDashboardToolbar>
  <UCard v-if="runningTimeInfo.id" class="m-3">
    <template #header>
      Gestartete Zeit:
    </template>
    <p>Start: {{dayjs(runningTimeInfo.start, "HH:mm:ssZ").format("HH:mm")}}</p>
    <p>Dauer: {{dayjs().diff(dayjs(runningTimeInfo.start, "HH:mm:ssZ"),'minutes') > 59 ? `${Math.floor(dayjs().diff(dayjs(runningTimeInfo.start, "HH:mm:ssZ"),'minutes') / 60)}:${dayjs().diff(dayjs(runningTimeInfo.start, "HH:mm:ssZ"),'minutes') % 60} h` : dayjs().diff(dayjs(runningTimeInfo.start, "HH:mm:ssZ"),'minutes') + ' min' }}</p>

    <UFormGroup
        class="mt-2"
        label="Notizen:"
    >
      <UTextarea
          v-model="runningTimeInfo.notes"
      />
    </UFormGroup>
    <template #footer>
      <UButton
          @click="stopStartedTime"
          :disabled="!runningTimeInfo.id"
      >
        Stop
      </UButton>
    </template>
  </UCard>

  <UModal
      v-model="showConfigTimeModal"
  >
    <UCard>
      <template #header>
        Zeiteintrag {{configTimeMode === 'create' ?  "erstellen" : "bearbeiten"}}
      </template>
      <UFormGroup
          label="Start:"
          :help="itemInfo.state !== 'Entwurf' ? 'Bearbeiten der Startzeit nicht möglich' : ''"
      >
        <VueDatePicker
            v-model="itemInfo.start"
            locale="de"
            cancel-text="Abbrechen"
            select-text="Auswählen"
            now-button-label="Jetzt"
            text-input="MM.dd.yyyy HH:mm"
            :dark="useColorMode().value !== 'light'"
            :format="format"
            :preview-format="format"
            :disabled="configTimeMode === 'create' ? false : itemInfo.state !== 'Entwurf'"
        />
      </UFormGroup>
      <UFormGroup
          label="Ende:"
          :help="itemInfo.state !== 'Entwurf' ? 'Bearbeiten der Endzeit nicht möglich' : ''"
      >
        <VueDatePicker
            v-model="itemInfo.end"
            locale="de"
            cancel-text="Abbrechen"
            select-text="Auswählen"
            now-button-label="Jetzt"
            text-input="MM.dd.yyyy HH:mm"
            :dark="useColorMode().value !== 'light'"
            :format="format"
            :preview-format="format"
            :disabled="configTimeMode === 'create' ? false : itemInfo.state !== 'Entwurf'"
        />
      </UFormGroup>
      <UFormGroup
          label="Benutzer:"
          :help="itemInfo.state !== 'Entwurf' ? 'Bearbeiten des Benutzers nicht möglich' : ''"
      >
        <USelectMenu
            :options="dataStore.profiles"
            v-model="itemInfo.profile"
            option-attribute="fullName"
            value-attribute="id"
            :disabled="(configTimeMode === 'create' ? false : itemInfo.state !== 'Entwurf') || (!dataStore.hasRight('createTime') || !dataStore.hasRight('createOwnTime'))"
        >
          <template #label>
            {{dataStore.profiles.find(profile => profile.id === itemInfo.profile) ? dataStore.profiles.find(profile => profile.id === itemInfo.profile).fullName : "Benutzer auswählen"}}
          </template>
        </USelectMenu>
      </UFormGroup>

      <UFormGroup
          label="Notizen:"
      >
        <UTextarea
            v-model="itemInfo.notes"
        />
      </UFormGroup>


      <template #footer v-if="configTimeMode === 'create' || itemInfo.state === 'Entwurf'">
        <InputGroup>
          <UButton
              @click="createTime"
              v-if="configTimeMode === 'create'"
              variant="outline"
          >
            Erstellen
          </UButton>
          <UButton
              @click="updateTime"
              v-else-if="configTimeMode === 'edit'"
              v-if="itemInfo.state === 'Entwurf'"
          >
            Speichern
          </UButton>
          <UTooltip
            text="Eingereichte Zeiten können nur noch durch Manager bearbeitet werden"
          >
            <UButton
                @click="setState('Eingereicht')"
                v-if="itemInfo.state === 'Entwurf'"
            >
              Einreichen
            </UButton>
          </UTooltip>

        </InputGroup>

      </template>
    </UCard>
  </UModal>
  
  <UTable
      class="mt-3"
      :columns="columns"
      :rows="filteredRows"
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Noch keine Einträge' }"
      @select="(row) => {configTimeMode = 'edit';
        itemInfo = row;
        showConfigTimeModal = true}"
  >
<!--    <template #state-data="{row}">
                <span
                    v-if="row.state === 'Entwurf'"
                    class="text-rose-500"
                >{{row.state}}</span>
      <span
          v-if="row.state === 'Eingereicht'"
          class="text-cyan-500"
      >{{row.state}}</span>
      <span
          v-if="row.state === 'Bestätigt'"
          class="text-primary-500"
      >{{row.state}}</span>
    </template>-->
    <template #profile-data="{row}">
      {{dataStore.profiles.find(profile => profile.id === row.profile) ? dataStore.profiles.find(profile => profile.id === row.profile).fullName : row.profile }}
    </template>
    <template #date-data="{row}">
      {{dayjs(row.date).format("DD.MM.YYYY")}}
    </template>

    <template #start-data="{row}">
      {{dayjs(row.start, "HH:mm:ssZ").format("HH:mm")}} Uhr
    </template>
    <template #end-data="{row}">
      {{dayjs(row.end, "HH:mm:ssZ").format("HH:mm")}} Uhr
    </template>
    <template #duration-data="{row}">
      {{getDuration(row).composed}}
    </template>
  </UTable>
</template>

<style scoped>

</style>