<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"
        @input="setStart"
        label="Beginn"
        type="datetime"
        :rules="[noValidDate, notEmpty]"
      />
      <IsoDateInput
        ref="bla"
        class="col-xs-12 col-sm-6 q-pa-sm"
        :value="job.end"
        @input="setEnd"
        label="Ende"
        type="datetime"
        :rules="[noValidDate, isAfterDate, notEmpty]"
      />
    </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"
        @input="setJobType"
        option-label="name"
        option-value="name"
        map-options
        clearable
        :rules="[notEmpty]"
      />
      <q-input
        filled
        class="col-xs-12 col-sm-6 q-pa-sm"
        label="Dienstanzahl"
        type="number"
        :value="job.required_services"
        @input="setRequired"
        :rules="[notEmpty]"
      />
    </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"
        @input="setComment"
        filled
        :rules="[notEmpty]"
      />
    </q-card-section>
    <q-btn label="Schicht löschen" color="negative" @click="removeJob" :disabled="jobCanDelete" />
  </q-card-section>
</template>

<script lang="ts">
import { defineComponent, computed, ref, PropType } from 'vue';
import IsoDateInput from 'src/components/utils/IsoDateInput.vue';
import { mapGetters } from 'vuex';
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 scheduleGetters = mapGetters('schedule', ['jobTypes']);
    const jobtypes = computed(() => <FG.JobType[]>scheduleGetters.jobTypes());

    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';
    }

    const refreshKey = ref(0);

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

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

<style></style>
