<template>

  <h2>{{ $t('pick_social_issue') }}</h2>


  <div id="awc_create_form">

    <div id="picking">
      <p>{{ $t('pick_social_issue_linked_with_action') }}</p>

      <div v-for="si in socialIssues">
        <input type="radio" v-bind:value="si.id" name="socialIssue" v-model="socialIssuePicked"> {{ si.title.fr }}
      </div>

      <div v-if="hasSocialIssuePicked">
        <h2>{{ $t('pick_an_action') }}</h2>
        <vue-multiselect
          v-model="socialActionPicked"
          label="text"
          :options="socialActionsReachables"
          :searchable="true"
          :close-on-select="true"
          :show-labels="true"
          track-by="id"
        ></vue-multiselect>
      </div>

      <div v-if="isLoadingSocialActions">
        <p>spinner</p>
      </div>

      <div v-if="hasSocialActionPicked" id="persons">
        <h2>{{ $t('persons_involved') }}</h2>

        <ul>
          <li v-for="p in personsReachables" :key="p.id">
            <input type="checkbox" :value="p.id" v-model="personsPicked">
            <person-render-box render="badge" :options="{}" :person="p"></person-render-box>
          </li>
        </ul>
      </div>

    </div>
    <div v-if="hasSocialActionPicked" id="start_date">
      <p><label>{{ $t('startDate') }}</label> <input type="date" v-model="startDate" /></p>
    </div>

    <div v-if="hasSocialActionPicked" id="end_date">
      <p><label>{{ $t('endDate') }}</label> <input type="date" v-model="endDate" /></p>
    </div>

    <div id="confirm">
      <div v-if="hasErrors">
        <p>{{ $t('form_has_errors') }}</p>

        <ul>
          <li v-for="e in errors">
            {{ e }}
          </li>
        </ul>
      </div>

      <div>
        <ul class="record_actions">
          <li class="cancel">
            <a href="#" class="btn btn-cancel">
              {{ $t('action.cancel') }}
            </a>
          </li>
          <li v-if="hasSocialActionPicked">
            <button class="btn btn-save" v-show="!isPostingWork" @click="submit">
              {{ $t('action.save') }}
            </button>
            <button class="btn btn-save" v-show="isPostingWork" disabled>
              {{ $t('Save') }}
            </button>
          </li>
        </ul>
      </div>
    </div>
  </div>

</template>

<style lang="scss">

#awc_create_form {
  display: grid;
  grid-template-areas:
    "picking picking"
    "start_date end_date"
    "confirm confirm"
    ;
  grid-template-columns: 50% 50%;
  column-gap: 1.5rem;

  #picking {
    grid-area: picking;

    #persons {
      ul {
        padding: 0;

        list-style-type: none;
      }
    }
  }

  #start_date {
    grid-area: start_date;
  }

  #end_date {
    grid-area: end_date;
  }

  #confirm {
    grid-area: confirm;
  }
}
</style>

<script>
import { mapState, mapActions, mapGetters } from 'vuex';
import VueMultiselect from 'vue-multiselect';
import { dateToISO, ISOToDate } from 'ChillMainAssets/chill/js/date.js';
import PersonRenderBox from 'ChillPersonAssets/vuejs/_components/Entity/PersonRenderBox.vue';

const i18n = {
  messages: {
    fr: {
      startDate: "Date de début",
      endDate: "Date de fin",
      form_has_errors: "Le formulaire comporte des erreurs",
      pick_social_issue: "Choisir une problématique sociale",
      pick_an_action: "Choisir une action d'accompagnement",
      pick_social_issue_linked_with_action: "Indiquez la problématique sociale liée à l'action d'accompagnement",
      persons_involved: "Usagers concernés",

    }
  }
}

export default {
  name: 'App',
  components: {
    VueMultiselect,
     PersonRenderBox,
  },
  methods: {
    submit() {
      this.$store.dispatch('submit');
    }
  },
  i18n,
  computed: {
    ...mapState([
      'socialIssues',
      'socialActionsReachables',
      'errors',
      'personsReachables',
    ]),
    ...mapGetters([
      'hasSocialIssuePicked',
      'hasSocialActionPicked',
      'isLoadingSocialActions',
      'isPostingWork',
      'hasErrors',
    ]),
    personsPicked: {
      get() {
        let s = this.$store.state.personsPicked.map(p => p.id);
        console.log('persons picked', s);

        return s;
      },
      set(v) {
        console.log('persons picked', v);
        this.$store.commit('setPersonsPickedIds', v);
      }
    },
    socialIssuePicked: {
      get() {
        let s = this.$store.state.socialIssuePicked;

        if (s === null) {
          return null;
        }

        return s.id;
      },
      set(value) {
        this.$store.dispatch('pickSocialIssue', value);
      }
    },
    socialActionPicked: {
      get() {
        return this.$store.state.socialActionPicked;
      },
      set(value) {
        this.$store.commit('setSocialAction', value);
      }
    },
    startDate: {
      get() {
        let d = this.$store.state.startDate;
        return dateToISO(d);
      },
      set(value) {
        this.$store.commit('setStartDate', ISOToDate(value));
      }
    },
    endDate: {
      get() {
        return dateToISO(this.$store.state.endDate);
      },
      set(value) {
        this.$store.commit('setEndDate', ISOToDate(value));
      }
    },
  }
}

</script>
