import { Router } from '@angular/router';
import { DiaryService } from './../diary.service';
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { Diary } from '../Diary';
import { FormGroup,  FormBuilder,  Validators } from '@angular/forms';

@Component({
  selector: 'app-add',
  templateUrl: './add.component.html',
  styleUrls: ['./add.component.css']
})
export class AddComponent implements OnInit {
  diarys: any;
  title = 'Add';
  angForm: FormGroup;
  constructor(private diaryservice: DiaryService, private fb: FormBuilder, private router: Router, private http: HttpClient) {
    this.createForm();
   }

   getDiarys() {
    this.diaryservice.getDiarys().subscribe(res => {
      this.diarys = res;

    });
  }

  createForm() {
    this.angForm = this.fb.group({
      topic: ['', Validators.required ],
      story: ['', Validators.required ],
      date: ['', Validators.required ]
   });
  }

  addDiary(topic, story, date) {
    if(confirm("ยืนยันการเพิ่มข้อมูล : "+topic)){
      this.diaryservice.addDiary(topic, story, date);
      this.router.navigate(['home']);
  }
}

  ngOnInit() {
  }

}
