<template>
  <div class="feedback__submission submission-form-container">
    <div class="submission-form-container__inputs">
      <submission-input
        :input-text="userInput.text"
        :saved="saved"
        :final="final"
        :placeholder="placeholder"
        :reopen="reopenSubmission"
        @input="saveInput"
      />
    </div>

    <div
      class="submission-form-container__actions"
      v-if="!final">
      <button
        class="submission-form-container__submit button button--primary button--white-bg"
        @click="$emit('turnIn')"
      >{{ action }}
      </button>
      <button
        class="submission-form-container__submit submission-form-container__spellcheck button button--primary button--white-bg"
        v-if="spellcheck"
        @click="$emit('spellcheck')"
      >{{ spellcheckText }}
      </button>
      <div v-if="userInput.document">
        <document-block
          :value="{url: userInput.document}"
          show-trash-icon
          @trash="changeDocumentUrl('')"
        />
      </div>

      <simple-file-upload
        :value="userInput.document"
        class="submission-form-container__document"
        v-if="allowsDocuments"
        @link-change-url="changeDocumentUrl"
      />
      <slot/>
    </div>

    <final-submission
      :user-input="userInput"
      :shared-msg="sharedMsg"
      v-if="final"
      @reopen="$emit('reopen')"/>
  </div>
</template>

<script>
  import SubmissionInput from '@/components/content-blocks/assignment/SubmissionInput';
  import FinalSubmission from '@/components/content-blocks/assignment/FinalSubmission';
  import SimpleFileUpload from '@/components/SimpleFileUpload';
  import DocumentBlock from '@/components/content-blocks/DocumentBlock';

  export default {
    props: {
      userInput: Object,
      saved: Boolean,
      placeholder: String,
      action: String,
      reopen: Function,
      document: String,
      spellcheck: {
        type: Boolean,
        default: false
      },
      spellcheckLoading: {
        type: Boolean,
        default: false
      },
      sharedMsg: String
    },

    components: {
      SubmissionInput,
      FinalSubmission,
      SimpleFileUpload,
      DocumentBlock
    },

    computed: {
      final() {
        return !!this.userInput && this.userInput.final
      },
      allowsDocuments() {
        return 'document' in this.userInput;
      },
      spellcheckText() {
        if (!this.spellcheckLoading) {
          return 'Rechtschreibung prüfen'
        } else {
          return 'Wird geprüft...'
        }
      }
    },

    methods: {
      reopenSubmission() {
        this.$emit('reopen');
      },
      saveInput(input) {
        this.$emit('saveInput', input);
      },
      changeDocumentUrl(documentUrl) {
        this.$emit('changeDocumentUrl', documentUrl);
      }
    },

  }
</script>

<style scoped lang="scss">
  @import '@/styles/_mixins.scss';

  .submission-form-container {

    @include input-box-shadow;
    background-color: $color-white;
    border-radius: $input-border-radius;
    border: 1px solid $color-silver;
    padding: $medium-spacing;
    margin-bottom: $medium-spacing;

    &__inputs {
      margin-bottom: 12px;
    }

    &__submit {
      margin-right: $medium-spacing;
    }

    &__actions {
      display: flex;
      align-items: center;
    }

    &__document {
      &:hover {
        cursor: pointer;
      }
    }

    &__spellcheck {
      /* so the button does not change size when changing the text */
      width: 235px;
      text-align: center;
      display: inline-block;
    }
  }

</style>
