<template>
   <div class="vue-component">
      <h3>{{ $t('comment.title') }}</h3>
      
      <div class="error flash_message" v-if="errors.length > 0">
         {{ errors[0] }}
      </div>
      
      <div v-if="firstComment">
         créé par {{ firstComment.creator.text }}
         le {{ $d(firstComment.createdAt.datetime, 'long') }}
         <div v-if="firstComment.updatedAt.datetime !== firstComment.createdAt.datetime">
            modifié par {{ firstComment.updatedBy.text }}
            le {{ $d(firstComment.updatedAt.datetime, 'long') }}
         </div>
      </div>
      
      <form @submit.prevent="submitform">
         <textarea 
            name="content" 
            v-bind:placeholder="$t('comment.content')"
            rows="8" 
            cols="80"
            ckeditor="ckeditor"
            v-model="content">
         </textarea>
         <ul class="record_actions">
            <li>
               <button type="submit" class="sc-button bt-save">{{ $t('action.save') }}</button>
            </li>
         </ul>
      </form>
      
   </div>
</template>

<script>
export default {
   name: "Comment",
   data() {
      return {
         formdata: { 
            content: ''
         }
      }
   },
   computed: {
      comments() {
         return this.$store.state.accompanyingCourse.comments;
      },
      firstComment() {
         return this.comments[0];
      },
      content: {
         set(value) {
            this.formdata.content = value;
         },
         get() {
            return (this.comments.length > 0) ? this.comments[0].content : null ;
         }
      },
      errors() {
         return this.$store.state.errorMsg;
      }
   },
   methods: {
      submitform() {
         this.formdata['method'] = (!this.firstComment)? 'POST' : 'PATCH';
         this.$store.dispatch('postFirstComment', this.formdata);
      }
   }
}
/*
* TODO
* - patch endpoint to update Content
* - delete/reset button ?
* - manage flash messages => specific component ?
* - ckeditor
*/
</script>
