import { AuthServiceService } from './../../../shared/auth-service.service';
import { Location } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { UserService } from '../../../shared/user.service';
import { ToastrService } from 'ngx-toastr';
import { NgxSpinnerService } from 'ngx-spinner';
import { FormGroup, NgForm } from '@angular/forms';
import { LectureService } from '../../../shared/lecture.service';


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

  @ViewChild('f') lectureForm: NgForm;
  typeMode: boolean = false;
  typeLink: string = '';
  lectureId: number;

  lecture = {
    name: '',
    name_en: '',
    ranking: '',
    display_location: '',
    end_time: '',
    lecture_time: '',
    status: '',
    description: '',
    description_en: '',
  }
  

  constructor(private lectureServices: LectureService,
    private userService: UserService,
    private location: Location,
    private toastr: ToastrService,
    private authSer: AuthServiceService,
    private route: ActivatedRoute, 
    private spineer: NgxSpinnerService) { }

  ngOnInit() {

    //show / hide notification search in header
    this.authSer.notificationLogin = true;
    this.authSer.showSearchHeader = false;
    this.authSer.showHeaderLogin = false;
    this.authSer.showHeaderDashBoard = true;
    this.authSer.showDashboardHeader = true;
    this.authSer.internalHeader = false;
    
    this.route.params.subscribe(
      (params: Params) => {
        if(params['typeLectureMode'] == 'edit') {
          this.typeLink = 'تعديل';
          this.spineer.show();
          this.typeMode = true;
          this.lectureId = params['editLectureId'];
          this.lectureServices.getLectureData(this.lectureId).subscribe(
            (responce) => {
              console.log(responce);
              const lectureData = responce['lecture'];
              console.log(lectureData);
              this.lecture.name = lectureData.name;
              this.lecture.name_en = lectureData.name_en;
              this.lecture.ranking = lectureData.ranking;
              this.lecture.display_location = lectureData.display_location;
              this.lecture.lecture_time = lectureData.lecture_time.split(' ').join("T");
              this.lecture.end_time = lectureData.end_time.split(' ').join("T");;
              this.lecture.status = lectureData.status;
              this.lecture.description = lectureData.description;
              this.lecture.description_en = lectureData.description_en;
              this.spineer.hide();
            },
            (error) => {
              console.log(error);
            }
          )
        } else {
          this.typeLink = 'إنشاء جديد';
        }
      }
    );
  }

  //submitted form
  onSubmitted() {
    console.log(this.lectureForm.value);
    
    this.lectureForm.value['lecture_time'] = this.lectureForm.value['lecture_time'].split('T').join(" ");
    this.lectureForm.value['end_time'] = this.lectureForm.value['end_time'].split('T').join(" ");
    console.log(this.lectureForm.value['lecture_time']);

    const startDate = this.lectureForm.value['lecture_time'].split('T')[0].split('-');
    console.log('start date', startDate);
    const endDate = this.lectureForm.value['end_time'].split('T')[0].split('-');
    console.log('end date', endDate);

  

    if(this.typeMode) {
      if(startDate[0] > endDate[0]) {
        this.toastr.warning('سنه البدايه يجب ان تكون أكبر من سنه النهايه !');
      } else if(startDate[0] == endDate[0] && startDate[1] > endDate[1]) {
        this.toastr.warning('شهر البدايه يجب ان تكون أقل من شهر النهايه !');
      } else if(startDate[0] == endDate[0] && startDate[1] > endDate[1] && startDate[2] > endDate[2]) {
        this.toastr.warning('يوم البدايه يجب ان تكون أقل من يوم النهايه !');
      }else {
        this.lectureServices.editLecture(this.lectureForm.value , this.lectureId).subscribe(
          (responce) => {
            console.log(responce);
            this.toastr.success('تمت التعديل بنجاح');
            this.location.back();
          },
          (error) => {
            this.toastr.error('حدث خطأ !');
            console.log(error);
          }
        );
      }
    } else {
      if(startDate[0] > endDate[0]) {
        this.toastr.warning('سنه البدايه يجب ان تكون أكبر من سنه النهايه !');
      } else if(startDate[0] == endDate[0] && startDate[1] > endDate[1]) {
        this.toastr.warning('شهر البدايه يجب ان تكون أقل من شهر النهايه !');
      } else if(startDate[0] == endDate[0] && startDate[1] > endDate[1] && startDate[2] > endDate[2]) {
        this.toastr.warning('يوم البدايه يجب ان تكون أقل من يوم النهايه !');
      }else {
        this.lectureServices.addLecture(this.lectureForm.value).subscribe(
          (responce) => {
            console.log(responce);
            this.toastr.success('تمت الاضافه بنجاح');
            this.location.back();
          },
          (error) => {
            this.toastr.error('حدث خطأ !');
            console.log(error);
          }
        );
      }
    }
  }

}
