<template>
    <form @submit.prevent="submit">
        
        <div class="form-group row">
     

          <div class="col-md-12">
            <label for="name">Contact Name</label>
            <input type="text" class="form-control" name="name" id="name" v-model="fields.name" placeholder="Enter name..."/>
            <div v-if="errors && errors.name" class="text-danger">{{ errors.name[0] }}</div>
          </div>
        
          <div class="col-md-12">
            <label for="phone">Phone </label>
            <input type="text" class="form-control" name="phone" id="phone" v-model="fields.phone" placeholder="Enter phone..."/>
            <div v-if="errors && errors.phone" class="text-danger">{{ errors.phone[0] }}</div>
          </div>




    <div class="col-md-12">
            <label for="group_id"> Group  Name</label>        
            <multiselect  name="group_id" v-model="fields.group_id" track-by="group_id" label="name" placeholder="Select Group name" :options="group_names"  :allow-empty="true" :multiple="false" :hide-selected="true" :max-height="150" @input="onChange" @close="onTouch" @select="onSelect">
              
            </multiselect>
            <div v-if="errors && errors.group_id" class="text-danger">{{ errors.group_id[0] }}</div>
        </div>




     
        </div>





        <div class="form-group">
            <button type="submit" class="btn btn-success btn-round">Save</button>
            <button type="reset" class="btn btn-danger btn-round">Clear</button>
        </div>       
        

        
    </form>
</template>

<script>
import FormMixin from '../shared/FormMixin';
import Multiselect from 'vue-multiselect';

// register globally
Vue.component('multiselect', Multiselect)


export default {
  mixins: [ FormMixin ],
  components: { Multiselect },
  data() {
    return {
      'action': '/contacts/storeContact',
      'text': 'Contact added succesfully',
      'redirect': '/contacts',
        group_id: [],
          group_names: [],
    }
  },

   computed: {
    isInvalid () {
      return this.isTouched && this.value.length === 0
    },
  },
    methods: {

      getGroups: function(){
        axios.get('/group/get')
        .then(function(response){
          this.group_names = response.data;
        }.bind(this));
      },
    nameWithphone ({ cust_client_fullname, cust_client_phone }) {
      return `${cust_client_fullname} — [${cust_client_phone}]`
    },
    onChange (value) {
      this.value = value
      if (value.indexOf('Reset me!') !== -1) this.value = []
    },
    onSelect (option) {
      if (option === 'Disable me!') this.isDisabled = false
    },
    onTouch () {
      this.isTouched = true
    },
    },
    created: function(){
      this.getCounties(),this.getGroups()
    }
}

</script>
<!--Add Multiselect CSS. Can be added as a static asset or inside a component. -->
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>

