<template>
  <q-card-section class="fit row justify-start content-center items-center">
    <q-card-section class="fit row justify-start content-center items-center">
      <IsoDateInput
        class="col-xs-12 col-sm-6 q-pa-sm"
        :value="job.start"
        label="Beginn"
        type="datetime"
        :rules="[noValidDate, notEmpty]"
        @input="setStart"
      />
      <IsoDateInput
        ref="bla"
        class="col-xs-12 col-sm-6 q-pa-sm"
        :value="job.end"
        label="Ende"
        type="datetime"
        :rules="[noValidDate, isAfterDate, notEmpty]"
        @input="setEnd"
      />
    </q-card-section>
    <q-card-section class="row fit justify-start content-center items-center">
      <q-select
        :key="refreshKey"
        filled
        use-input
        label="Dienstart"
        input-debounce="0"
        class="col-xs-12 col-sm-6 q-pa-sm"
        :value="job.type"
        :options="jobtypes"
        option-label="name"
        option-value="name"
        map-options
        clearable
        :rules="[notEmpty]"
        @input="setJobType"
      />
      <q-input
        filled
        class="col-xs-12 col-sm-6 q-pa-sm"
        label="Dienstanzahl"
        type="number"
        :value="job.required_services"
        :rules="[notEmpty]"
        @input="setRequired"
      />
    </q-card-section>
    <q-card-section class="fit row justify-start content-center items-center">
      <q-input
        class="col-xs-12 col-sm-6 q-pa-sm"
        label="Beschreibung"
        type="textarea"
        :value="job.comment"
        filled
        :rules="[notEmpty]"
        @input="setComment"
      />
    </q-card-section>
    <q-btn label="Schicht löschen" color="negative" :disabled="jobCanDelete" @click="removeJob" />
  </q-card-section>
</template>

<script lang="ts">
import IsoDateInput from 'src/components/utils/IsoDateInput.vue';
import { defineComponent, computed, ref, onBeforeMount, PropType } from 'vue';
import { useScheduleStore } from '../../store';
import { date } from 'quasar';

export default defineComponent({
  name: 'Job',
  components: { IsoDateInput },
  props: {
    job: {
      required: true,
      type: Object as PropType<FG.Job>,
    },
    jobCanDelete: Boolean,
  },
  emits: {
    'set-start': (d: Date) => !!d,
    'remove-job': () => true,
    'set-end': (d: Date) => !!d,
    'set-comment': (c: string) => !!c,
    'set-job-type': (jt: FG.JobType) => !!jt,
    'set-required': (r: number) => isFinite(r),
  },
  setup(props, { emit }) {
    const store = useScheduleStore();

    onBeforeMount(() => store.getJobTypes());

    const jobtypes = computed(() => store.jobTypes);
    const refreshKey = ref<number>(0);

    function setStart(value: Date) {
      emit('set-start', value);
    }

    function setEnd(value: Date) {
      emit('set-end', value);
    }

    function setComment(value: string) {
      emit('set-comment', value);
    }

    function setJobType(value: FG.JobType) {
      emit('set-job-type', value);
    }

    function setRequired(value: number) {
      emit('set-required', value);
    }

    function removeJob() {
      emit('remove-job');
    }

    function notEmpty(val: string) {
      return !!val || 'Feld darf nicht leer sein!';
    }
    function noValidDate(val: string) {
      console.log(val);
      return !!date.isValid(val) || 'Datum/Zeit muss gesetzt sein!';
    }

    function isAfterDate(val: Date) {
      console.log('isAfterDate', props.job.start, val);
      return props.job.start < new Date(val) || 'Ende muss hinter dem Start liegen';
    }

    function refresh() {
      refreshKey.value += 1;
    }

    return {
      jobtypes,
      setStart,
      setEnd,
      setComment,
      setJobType,
      setRequired,
      removeJob,
      notEmpty,
      noValidDate,
      isAfterDate,
      refreshKey,
    };
  },
});
</script>

<style></style>
