<template>
  <div>
    <h4 align="left" class="contactHeader">Send us a message</h4>
    <div class="form">
      <div v-if="boolValidationError" class="row">
        <div class="col">
          <alert :text="errorMessage" :type="errorOccurred" />
        </div>
      </div>
      <div class="row">
        <div class="form-group col-md-6">
          <float-label label="Name" style="width: 100%;">
            <input
              type="text"
              id="name"
              class="form-control uniInput my-2"
              v-model="name"
              v-bind:class="{
                'is-valid': name,
                'is-invalid': !requiredField(name) && nameLoad,
              }"
              v-on:keyup="nameLoad = true"
              v-on:blur="nameLoad = true"
              placeholder="Name"
            />
            <div class="invalid-feedback">Name is required!</div>
          </float-label>
        </div>
        <div class="form-group col-md-6">
          <float-label label="Email" style="width: 100%;">
            <input
              type="email"
              id="contactemail"
              v-model="contactemail"
              class="form-control uniInput my-2"
              v-bind:class="{
                'is-valid': contactemail,
                'is-invalid': !validEmail(contactemail) && contactemailLoad,
              }"
              v-on:keyup="contactemailLoad = true"
              v-on:blur="contactemailLoad = true"
              placeholder="Email"
            />
            <div class="invalid-feedback">Valid email address is required!</div>
          </float-label>
        </div>
        <div class="form-group col-md-6">
          <float-label label="Phone Number" style="width: 100%;">
            <input
              type="text"
              id="phone"
              v-model="phone"
              class="form-control uniInput my-2"
              v-bind:class="{ 'is-valid': phone }"
              placeholder="Phone Number"
            />
          </float-label>
        </div>
        <div class="form-group col-md-6">
          <float-label label="Property" style="width: 100%;">
            <input
              type="text"
              id="property"
              v-model="property"
              class="form-control uniInput my-2"
              v-bind:class="{ 'is-valid': property }"
              placeholder="Property"
            />
          </float-label>
        </div>
      </div>
      <div align="left" class="form-group">
        <label v-if="!mailSource" class="uniSelectLabel" for="SourceSelect">
          Heard About
        </label>
        <float-label label="Heard About" style="width: 100%;">
          <select
            id="SourceSelect"
            class="form-control uniSelect"
            v-model="mailSource"
            @change="sourceSelect()"
            v-bind:class="{
              'is-valid': mailSource,
              'is-invalid': !requiredField(mailSource) && mailSourceLoad,
            }"
            v-on:keyup="mailSourceLoad = true"
            v-on:blur="mailSourceLoad = true"
          >
            <option
              v-for="(source, s) in contactUsSources"
              :key="s"
              :value="source"
            >
              {{ source.description }}
            </option>
          </select>
          <div class="invalid-feedback">
            Please select a source!
          </div>
        </float-label>
      </div>

      <div class="form-group"></div>
      <textarea
        class="form-control"
        name="message"
        rows="5"
        data-rule="required"
        data-msg="Please write something for us"
        placeholder="Message"
        v-model="message"
        v-bind:class="{
          'is-valid': message,
          'is-invalid': !requiredField(message) && messageLoad,
        }"
        v-on:keyup="messageLoad = true"
        v-on:blur="messageLoad = true"
      ></textarea>
      <div class="invalid-feedback">
        Message is required!
      </div>
      <div align="left" class="custom-control custom-switch mb-2">
        <div class="row">
          <div align="center" class="col">
            <label class="mr-5">
              I wish to receive future listings and special campaigns?
            </label>
            <input
              type="checkbox"
              class="custom-control-input"
              id="contactUscustomSwitch1"
              :checked="consent"
              @change="changeConsent"
            />
            <label class="custom-control-label" for="contactUscustomSwitch1">
              {{ consent ? 'Yes' : 'No' }}
            </label>
          </div>
        </div>
      </div>
      <div class="text-center">
        <button class="btn-white-border" @click="sendMail()">Send</button>
      </div>
      <div v-if="boolSent">
        <alert :text="alertMsg" :type="'SUCCESS'" />
      </div>
    </div>
  </div>
</template>

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

export default {
  components: {
    alert,
  },
  data() {
    return {
      sourceObj: {},
      source: '',
      mailSource: '',
      mailSourceId: '',
      alertMsg: 'Sent! You can expect a reply soon!',
      name: '',
      contactemail: '',
      phone: '',
      property: '',
      consent: true,
      message: '',
      boolSent: false,
      errorOccurred: '',
      errorMessage: '',
      boolValidationError: false,
      nameLoad: false,
      contactemailLoad: false,
      messageLoad: false,
      mailSourceLoad: false,
    }
  },
  mounted() {
    this.boolSent = false
    console.log('Email')
    console.log(this.contactemail)
    this.getContactUsSources()
  },
  computed: {
    ...mapState('contactUsSources', ['contactUsSources']),
  },
  methods: {
    ...mapActions('contactUsSources', ['getContactUsSources']),
    sourceSelect() {
      console.log('sourceSelect')
      console.log(this.mailSource.id)
      console.log(this.mailSource)
      this.mailSourceId = this.mailSource ? this.mailSource.id : 1
    },
    async sendMail() {
      if (this.validatePage()) {
        var mailObj = {
          name: this.name,
          email: this.contactemail,
          phone: this.phone,
          property: this.property,
          message: this.message,
          mailSourceId: this.mailSourceId,
          consent: this.consent,
        }
        await axios
          .post('/api/mail/0', mailObj)
          .then((response) => {
            //console.log('Response');
            //console.log(response);
            this.boolSent = true
          })
          .catch((err) => {
            this.boolValidationError = true
            this.errorOccurred = 'ERROR'
            this.errorMessage = 'Server Error: CONNECTION_REFUSED'
            if (err) {
              //console.log('Error');
              //console.log(err.response.status);
              if (err.response.status) {
                this.boolSent = true
                this.boolValidationError = false
              } else {
                this.boolSent = false
              }
            } else {
              console.log('no err')
              boolSent = false
            }
          })
        this.name = ''
        this.contactemail = ''
        this.phone = ''
        this.property = ''
        this.message = ''
        this.consent = true
        this.mailSource = ''
        this.nameLoad = false
        this.contactemailLoad = false
        this.messageLoad = false
        this.mailSourceLoad = false
      }
    },
    countDownChanged(dismissCountDown) {
      this.dismissCountDown = dismissCountDown
    },
    validatePage: function () {
      //console.log('validatePage')
      if (this.name && this.contactemail && this.mailSource && this.message) {
        this.errorOccurred = ''
        this.errorMessage = ''
        this.boolValidationError = false
        return true
      } else {
        this.boolValidationError = true
        this.nameLoad = true
        this.contactemailLoad = true
        this.mailSourceLoad = true
        this.messageLoad = true
        this.errorOccurred = 'ERROR'
        this.errorMessage = 'Please check form and correct all input fields!'
        return false
      }
    },
    changeConsent() {
      this.consent = !this.consent
    },
    requiredField: function (tfield) {
      if (tfield) {
        return true
      } else {
        return false
      }
    },
    validEmail: function (temail) {
      var re = /(.+)@(.+){2,}\.(.+){2,}/
      return re.test(temail.toLowerCase())
    },
  },
}
</script>

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