<template>
  <main id="main" style="padding-bottom: 50px;">
    <div class="container">
      <div class="form">
        <div class="row">
          <div class="col">
            <div class="section-header">
              <h2>Terms And Conditions</h2>
            </div>
          </div>
        </div>
        <div v-if="checkAccess">
          <div class="row">
            <div class="col-md-6">
              <label for="version" class="uniSelectLabel">Version:</label>
              <input
                name="version"
                type="text"
                v-model="version"
                class="mt-5 mb-3 ml-2"
              />
            </div>
            <div class="col-md-6 mt-5">
              <p style="float: right;">Last Modified: {{ getModiefiedDate }}</p>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <vue-editor
                v-model="termsInput"
                :editor-toolbar="customToolbar"
              />
            </div>
          </div>
          <div class="row">
            <div class="col-md-2 mt-3">
              <button @click="saveTerms()" class="btn-white-border">
                Save
              </button>
            </div>
            <div v-if="updated" class="col mt-3">
              <alert
                :text="'Terms and Contidtions Updated!!'"
                :type="'SUCCESS'"
              />
            </div>
          </div>
        </div>
        <div v-else>
          <alert
            :text="'You don\'t have permission to view this page'"
            :type="'ERROR'"
          />
        </div>
      </div>
    </div>
    <div v-if="wait" id="preloader"></div>
  </main>
</template>

<script>
/* eslint-disable */
import { VueEditor } from 'vue2-editor'
import { mapActions, mapGetters, mapState } from 'vuex'

import Log from '../../../assets/Log'
import alert from '../../shared/alert'

export default {
  components: {
    VueEditor,
    alert,
  },
  data() {
    return {
      termsInput: '',
      version: '',
      updated: false,
      wait: true,
      customToolbar: [
        [{ header: [false, 1, 2, 3, 4, 5, 6] }],
        ['bold', 'italic', 'underline', 'strike'],

        [{ list: 'ordered' }, { list: 'bullet' }, { list: 'check' }],
        [{ script: 'sub' }, { script: 'super' }],
        [{ indent: '-1' }, { indent: '+1' }],
      ],
    }
  },
  mounted() {
    this.populateTerms()
  },
  methods: {
    ...mapActions('termsConditions', ['setTerms', 'retrieveTerms']),
    saveTerms() {
      var termsObj = {
        termsConditions: this.termsInput,
        version: this.version,
      }
      this.setTerms(termsObj)
        .then(() => {
          this.updated = true
          this.$router.go()
        })
        .catch((ex) => {
          console.log(ex)
        })
    },
    async populateTerms() {
      await this.retrieveTerms()
      this.version = this.terms.version
      this.termsInput = this.terms.termsConditions
      setTimeout(() => {
        this.wait = false
      }, 500)
    },
  },
  computed: {
    ...mapGetters('termsConditions', ['getTermsAndConditions']),
    ...mapState('termsConditions', ['terms']),
    getModiefiedDate() {
      var date = new Date(this.terms.modified)
      return (
        date.getDate() +
        ' / ' +
        (date.getMonth() + 1) +
        ' / ' +
        date.getFullYear()
      )
    },
    checkAccess() {
      if (Log.getUser().role === 'Super Admin') {
        return true
      } else {
        return false
      }
    },
  },
}
</script>

<style lang="scss" scoped></style>
