<template>
  <main id="main" style="margin-top: 200px; padding-bottom: 50px;">
    <div class="container">
      <div class="form">
        <div class="row">
          <div class="col">
            <div class="section-header">
              <h2>Contact Us Source</h2>
            </div>
          </div>
        </div>
        <div v-if="checkAccess" class="row mb-3">
          <div class="col-md-12">
            <form id="mainForm">
              <div class="form-group row">
                <div class="col-md-4">
                  <float-label fixed label="Description">
                    <input
                      class="form-control uniInput"
                      type="text"
                      name="description"
                      id="description"
                      v-model="contactUsSource.description"
                      v-bind:class="{
                        'is-valid': contactUsSource.description,
                        'is-invalid':
                          !requiredField(contactUsSource.description) &&
                          descriptionLoad,
                      }"
                      v-on:keyup="descriptionLoad = true"
                      v-on:blur="descriptionLoad = true"
                    />
                    <div class="invalid-feedback">Description is Required!</div>
                  </float-label>
                </div>
              </div>
            </form>
          </div>
          <div v-if="boolValidationError" class="row">
            <div class="col">
              <alert :text="errorMessage" :type="errorOccurred" />
            </div>
          </div>
          <div class="col-md-12">
            <button
              type="button"
              @click="SubmitData()"
              class="btn-white-border"
            >
              Save
            </button>
            <button type="button" @click="Close()" class="btn-white-border">
              Close
            </button>
          </div>
        </div>
        <div v-else class="row">
          <div class="col">
            <alert
              :text="'You don\'t have permission to view this page'"
              :type="'ERROR'"
            />
          </div>
        </div>
      </div>
    </div>
  </main>
</template>

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

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

export default {
  name: 'ContactUsSource',
  data() {
    return {
      errorOccurred: '',
      errorMessage: '',
      boolMessage: false,
      boolValidationError: false,
      descriptionLoad: false,
    }
  },
  components: {
    alert,
  },
  mounted() {
    this.getContactUsSources()
    this.clearContactUsSource()
    if (this.$route.params.id > 0) {
      console.log(this.$route.params.id)
      this.getContactUsSource(this.$route.params.id)
      console.log(this.contactUsSource.description)
    }
  },
  computed: {
    ...mapState('contactUsSources', ['contactUsSource', 'contactUsSources']),
    checkAccess() {
      if (Log.getUser().role === 'Super Admin') {
        return true
      } else {
        return false
      }
    },
  },
  methods: {
    ...mapActions('contactUsSources', [
      'getContactUsSource',
      'saveContactUsSource',
      'updateContactUsSource',
      'clearContactUsSource',
      'getContactUsSources',
    ]),
    SubmitData() {
      if (this.validatePage()) {
        console.log('SubmitData')
        if (this.contactUsSource.id > 0) {
          this.updateContactUsSource(this.contactUsSource)
        } else {
          this.saveContactUsSource(this.contactUsSource)
        }
        this.$router.push('/contactUsSource/list')
      }
    },
    Close() {
      this.$router.push('/contactUsSource/list')
    },
    uniqueSource: function (tfield) {
      if (tfield) {
        if (
          this.contactUsSources.find(
            (i) => i.description === this.contactUsSource.description,
          )
        ) {
          return false
        } else {
          return true
        }
      } else {
        return false
      }
    },
    requiredField: function (tfield) {
      if (tfield) {
        return true
      } else {
        return false
      }
    },
    validatePage: function () {
      console.log('validatePage')
      if (this.contactUsSource.description) {
        console.log('passed')
        if (!this.uniqueSource(this.contactUsSource.description)) {
          this.statusLoad = true
          this.descriptionLoad = true
          this.boolValidationError = true
          this.errorOccurred = 'ERROR'
          this.errorMessage = 'Status Code already recorded!'
        } else {
          this.errorOccurred = ''
          this.errorMessage = ''
          this.boolValidationError = false
          return true
        }
      } else {
        console.log('failed')
        this.descriptionLoad = true
        this.surnameLoad = true
        this.boolValidationError = true
        this.errorOccurred = 'ERROR'
        this.errorMessage = 'Please check form and correct all input fields!'
        return false
      }
    },
  },
}
</script>
