<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">Mobile Number </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"> Select Group </label>        
            <multiselect  name="group_id" v-model="fields.group_id" track-by="group_id" label="name" placeholder="Assign Group" :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': '/at/contacts/storeContact',
      'text': 'Contact added succesfully',
      'redirect': '/at/contacts',
        
          group_names: [],
    }
  },

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

      getGroups: function(){
        axios.get('/at/groups/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(){  
    }
}

</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>

