<template>
  <BaseSetting title="Anwendungs Einstellungen" :submit-function="submit" v-slot="{ enableEdit }">
    <div class="w-full">
      <label for="custom_login_message">Nachricht unter Login (optional)</label>
      <input
        id="custom_login_message"
        type="text"
        :readonly="!enableEdit"
        :value="appSettings['app.custom_login_message']"
      />
    </div>
    <div class="w-full flex flex-row items-center gap-2">
      <div
        v-if="!enableEdit"
        class="border-2 border-gray-500 rounded-sm"
        :class="appSettings['app.show_link_to_calendar'] ? 'bg-gray-500' : 'h-3.5 w-3.5'"
      >
        <CheckIcon v-if="appSettings['app.show_link_to_calendar']" class="h-2.5 w-2.5 stroke-4 text-white" />
      </div>
      <input v-else id="show_link_to_calendar" type="checkbox" :checked="appSettings['app.show_link_to_calendar']" />
      <label for="show_link_to_calendar">Kalender-Link anzeigen</label>
    </div>
  </BaseSetting>
</template>

<script setup lang="ts">
import { useAbilityStore } from "@/stores/ability";
import { useSettingStore } from "@/stores/admin/management/setting";
import { CheckIcon } from "@heroicons/vue/24/outline";
import { mapActions, mapState } from "pinia";
import { defineComponent } from "vue";
import BaseSetting from "./BaseSetting.vue";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      enableEdit: false as boolean,
      status: undefined as undefined | "loading" | "success" | "failed",
    };
  },
  computed: {
    ...mapState(useSettingStore, ["readByTopic"]),
    ...mapState(useAbilityStore, ["can"]),
    appSettings() {
      return this.readByTopic("app");
    },
  },
  methods: {
    ...mapActions(useSettingStore, ["updateSettings"]),
    submit(e: any) {
      const formData = e.target.elements;
      return this.updateSettings([
        {
          key: "app.custom_login_message",
          value: formData.custom_login_message.value || null,
        },
        {
          key: "app.show_link_to_calendar",
          value: formData.show_link_to_calendar.checked || null,
        },
      ]);
    },
  },
});
</script>
