import { HttpClient } from '@angular/common/http';
import { NgxSpinnerService } from 'ngx-spinner';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { AuthServiceService } from './../../../shared/auth-service.service';
import { ToastrService } from 'ngx-toastr';
import { DashboardService } from './../../../shared/dashboard.service';
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';


@Component({
  selector: 'app-definition-time-add',
  templateUrl: './definition-time-add.component.html',
  styleUrls: ['./definition-time-add.component.css']
})
export class DefinitionTimeAddComponent implements OnInit {

  constructor(private dashBoardSer: DashboardService,
    private toastr: ToastrService,
    public authSer: AuthServiceService,
    private route: ActivatedRoute,
    private spinner: NgxSpinnerService,
    private location: Location,
    private http: HttpClient
  ) { }

  @ViewChild('f') definitionForm : NgForm;


  typePage: string = '';
  locations =[];
  valueType: string = '';
  checkSaveclick:boolean = false;
  check : boolean = false;
  isEdit: boolean = false;
  editPageId: number;

  formData = {
    period: null,
    name: '',
    // parent_name: ''
  }

  ngOnInit() {

    this.route.params.subscribe(
      (params: Params) => {
        this.editPageId = +params['timeEditPageId'];
        console.log(this.editPageId);
      }
    );

    if (this.editPageId) {
      this.spinner.show();
      this.isEdit = true;
      this.typePage = 'تعديل';
      this.dashBoardSer.getItemData(this.editPageId, 'definitionOfTime' ).subscribe(
        (response) => {
          console.log('ssss', response);
          this.formData.period = response['time_period'].period;
          this.formData.name  = response['time_period'].name;
          this.spinner.hide();
        },
        (error) => {
          console.log(error);
        }
      );
    }else {
      this.typePage = 'اضافة';
    }
    
  }

  onSubmitted(){
    console.log('HERE',this.definitionForm.value);
    if(this.editPageId) {
      this.dashBoardSer.editItem(this.editPageId, this.definitionForm.value, 'definitionOfTime').subscribe(
        (responce) => {
          console.log(responce);
          this.toastr.success('تم التعديل بنجاح');
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
        }
      )
    } else {
      this.dashBoardSer.addItem( this.definitionForm.value , 'definitionOfTime').subscribe(
        (responce) => {
          console.log('ADD',responce);
          this.toastr.success('تم الإضافه بنجاح');
          this.checkSaveclick = false;
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.checkSaveclick = false;
          this.toastr.error('خطأ في الخادم ، حاول لاحقا');
        }
      );
      }
  }

}
