import { Component, Input, Output, EventEmitter } from '@angular/core';
import { FormBuilderProvider } from '../../providers/form-builder/form-builder';
import { ModalController } from 'ionic-angular';


@Component({
  selector: 'ws-date-picker',
  template: `
    <ion-item class="ws-input {{fieldClass}}" [ngClass]="(!isFieldValid && showError) ? 'field-error' : ''">
      <ion-label color="primary" floating>{{question.question}}</ion-label>
      <ion-datetime displayFormat="{{dateFormat}}" (ionChange)="setAnswer($event,question)" [(ngModel)]="fieldValue"></ion-datetime>
    </ion-item>
  `,
  styles: [`

  `]
})
export class WsDatePickerComponent {

  @Input('question') question;
  @Output() answerGiven = new EventEmitter();

  fieldValue:string = '';
  isFieldValid:boolean = false;
  showError:boolean = false;
  dateFormat:string = 'DD/MM/YYYY';
  fieldClass:string = '';

  constructor(
  	public formBuilder: FormBuilderProvider,
    public modalCtrl: ModalController
  	//private datePickerProvider: DatePickerProvider
  	){
    	this.fieldValue = '';
  }

  ngOnInit() {
      if(typeof this.question.value !== 'undefined'){
          this.fieldValue = this.question.value;
      }
       if(typeof this.question.dateFormat !== 'undefined'){
          this.dateFormat = this.question.dateFormat;
      } 
      
      if(typeof this.question.inputClass !== 'undefined'){
          this.fieldClass = this.question.inputClass;
      }
  }

  
  setAnswer(event,question){
  	//let data = moment(date).format('DD/MM/YYYY');
	  question.date = this.fieldValue;
	  question.value = this.fieldValue;
    question.valid = true;
  	//this.fieldValue = data;
	  this.answerGiven.emit(question);


  	//let isStringValid = this.validateAnswer(date);


  	/*if(!isStringValid){
		this.isFieldValid = false;
  		this.showError = true;
  	}else{
		this.isFieldValid = true;
  		this.showError = false;
  	}


  	if(this.isFieldValid){*/
  		
  	//}
  		
  }


  validateAnswer(question,string){	
  		return this.formBuilder.validateAnswer(question,string);
  }

  

}
