<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>Property Type</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 label="Description" style="width: 100%;">
                    <input
                      class="form-control uniInput"
                      type="text"
                      name="description"
                      id="description"
                      v-model="propertyType.description"
                      v-bind:class="{
                        'is-valid': propertyType.description,
                        'is-invalid':
                          !requiredField(propertyType.description) &&
                          descriptionLoad,
                      }"
                      v-on:keyup="descriptionLoad = true"
                      v-on:blur="descriptionLoad = true"
                    />
                    <div class="invalid-feedback">Description is required!</div>
                  </float-label>
                </div>
                <div class="col-md-4">
                  <float-label label="Property Usage Type" style="width: 100%;">
                    <select
                      class="form-control uniSelect"
                      name="UsageType"
                      id="UsageType"
                      v-model="propertyType.usageType"
                      v-bind:class="{
                        'is-valid': propertyType.usageType,
                      }"
                      v-on:keyup="usageTypeLoad = true"
                      v-on:blur="usageTypeLoad = true"
                    >
                      <option value="0">Residential</option>
                      <option value="1">Commercial</option>
                    </select>
                    <div class="invalid-feedback">Usage Type is required!</div>
                  </float-label>
                </div>
              </div>
            </form>
          </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: 'PropertyType',
  components: {
    alert,
  },
  data() {
    return {
      boolSent: false,
      errorOccurred: '',
      errorMessage: '',
      boolValidationError: false,
      descriptionLoad: false,
      usageTypeLoad: false,
    }
  },
  mounted() {
    this.clearPropertyType()
    if (this.$route.params.id > 0) {
      this.getPropertyType(this.$route.params.id)
    }
  },
  computed: {
    ...mapState('propertyTypes', ['propertyType']),
    checkAccess() {
      if (Log.getUser().role === 'Super Admin') {
        return true
      } else {
        return false
      }
    },
  },
  methods: {
    ...mapActions('propertyTypes', [
      'getPropertyType',
      'savePropertyType',
      'updatePropertyType',
      'clearPropertyType',
    ]),
    SubmitData() {
      if (this.validatePage()) {
        console.log('SubmitData')
        if (this.propertyType.id > 0) {
          this.updatePropertyType(this.propertyType)
        } else {
          console.log('savePropertyType')
          console.log(this.propertyType)
          this.savePropertyType(this.propertyType)
        }
        this.$router.push('/propertyTypes/list')
      }
    },
    Close() {
      this.$router.push('/propertyTypes/list')
    },
    validatePage: function () {
      if (this.propertyType.description) {
        this.errorOccurred = ''
        this.errorMessage = ''
        this.boolValidationError = false
        return true
      } else {
        this.boolValidationError = true
        this.nameLoad = true
        this.descriptionLoad = true
        this.usageTypeLoad = true
        this.errorOccurred = 'ERROR'
        this.errorMessage = 'Please check form and correct all input fields!'
        return false
      }
    },
    requiredField: function (tfield) {
      if (tfield) {
        return true
      } else {
        return false
      }
    },
  },
}
</script>
