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

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

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

  absorpation = {
    name: '',
    status: '',
    department_id: '',
    count: null,
  }

  @ViewChild('f') formData: NgForm;

  startDate;
  endDate: string = '';
  checkSaveclick: boolean = false;
  typeMode: boolean = false;
  typeLink: string = '';
  departments= [];
  obsId: number;
  bindingDateSplitStart;
  bindingDateSplitEnd;

  ngOnInit() {
    
    this.route.params.subscribe(
      (params: Params) => {
        this.obsId = params['absorpationEditId'];
        if(this.obsId) {
          this.typeMode = true;
          this.typeLink = 'تعديل';
        } else {
          this.typeLink = 'إضافه';
        }
      }
    );

    //edit mode 
    if(this.obsId) {
      this.spinner.show();
      this.dashBoardSer.getItemData(this.obsId, 'absorpation').subscribe(
        (responce) => {
          console.log('data get item', responce);
          this.absorpation.name = responce['absorpation_plan'].name;
          this.absorpation.count = responce['absorpation_plan'].count;
          this.absorpation.status = responce['absorpation_plan'].status;
          this.absorpation.department_id = responce['absorpation_plan'].department_id;
          this.startDate = responce['absorpation_plan'].start_date;
          this.endDate = responce['absorpation_plan'].end_date;
          console.log('start date edit', this.startDate);
          console.log('end date edit', this.endDate);
         this.bindingDateSplitStart = {
           year: parseInt(responce['absorpation_plan'].start_date.split('-')[0]),
           month: parseInt(responce['absorpation_plan'].start_date.split('-')[1]),
           day: parseInt(responce['absorpation_plan'].start_date.split('-')[2]),
         }
         this.bindingDateSplitEnd = {
          year: parseInt(responce['absorpation_plan'].end_date.split('-')[0]),
          month: parseInt(responce['absorpation_plan'].end_date.split('-')[1]),
          day: parseInt(responce['absorpation_plan'].end_date.split('-')[2]),
        }
        this.spinner.hide();
         console.log(this.bindingDateSplitStart);
        },
        (error) => {
          console.log(error);
        }
      );
    }
    
    this.http.get(this.authSer.pathApi + '/training_adminstration_departments').subscribe(
      (responce) => {
        console.log(responce);
        this.departments = responce['departments'];
      },
      (error) => {
        console.log(error);
      }
    );

  }

    //get value date from child component 
    public getDate(date: any , type: string):void {
      console.log( date);
      if(type == 'start') {
        this.startDate = date.year + '-' + date.month + '-' + date.day;
        console.log('start date', this.startDate);
      } else if(type == 'end') {
        this.endDate = date.year + '-' + date.month + '-' + date.day;
        console.log('end date', this.endDate);
      } else {
        console.log('not type recieved !');
      }
    }


    onSubmitted() {

      const data = this.formData.value;
      data['start_date'] = this.startDate;
      data['end_date'] = this.endDate;
      
      const startDateData = this.startDate.split('-');
      const endDateData = this.endDate.split('-');
      
      console.log(startDateData[2]);
      console.log(endDateData[2]);
   
      console.log(data);

      if(this.typeMode) {
        if(startDateData[0] > endDateData[0]) {
          this.toastr.warning('تاريخ الإنتهاء يجب أن يكون أكبر من تاريخ الإبتداء !');
          this.checkSaveclick = false;
        } else if(startDateData[1] > endDateData[1]){
          this.toastr.warning('تاريخ الإنتهاء يجب أن يكون أكبر من تاريخ الإبتداء !');
          this.checkSaveclick = false;
        } else if(startDateData[2] > endDateData[2]){
          this.toastr.warning('تاريخ الإنتهاء يجب أن يكون أكبر من تاريخ الإبتداء !');
          this.checkSaveclick = false;
        } else {
          this.dashBoardSer.editItem(this.obsId ,data , 'absorpation').subscribe(
            (responce) => {
              console.log(responce);
              this.toastr.success('تمت الإضافه بنجاح');
              this.location.back();
            },
            (error) => {
              console.log(error);
              this.toastr.success('خطأ في الإضافه !');
            }
          );
        }
      } else {
        if(startDateData[0] > endDateData[0]) {
          this.toastr.warning('تاريخ الإنتهاء يجب أن يكون أكبر من تاريخ الإبتداء !');
          this.checkSaveclick = false;
        } else if(startDateData[1] > endDateData[1]){
          this.toastr.warning('تاريخ الإنتهاء يجب أن يكون أكبر من تاريخ الإبتداء !');
          this.checkSaveclick = false;
        } else if(startDateData[2] > endDateData[2]){
          this.toastr.warning('تاريخ الإنتهاء يجب أن يكون أكبر من تاريخ الإبتداء !');
          this.checkSaveclick = false;
        } else {
          this.dashBoardSer.addItem(data , 'absorpation').subscribe(
            (responce) => {
              console.log(responce);
              this.toastr.success('تمت الإضافه بنجاح');
              this.location.back();
            },
            (error) => {
              console.log(error);
              this.toastr.success('خطأ في الإضافه !');
            }
          );
        }
      }

    

    }



}
