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

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

  @ViewChild('f') dataForm: NgForm;

  constructor(private userSer:UserService,
    private authSer:AuthServiceService,
    private toastr: ToastrService,
    private spinner: NgxSpinnerService,
    private dashboardSer: DashboardService,
    private location: Location,
    private route:ActivatedRoute) { }
    typeMode:boolean = false; //default false for create page
    typeLink: string;
    eventCalendarId:number;
    checkDisabledSave:boolean = false;

    eventCalendar = {
      title: '',
      title_en: '',
      description: '',
      description_en: '',
      event_time: '',
      status: 1,
    };

    ngOnInit() {

      this.route.params.subscribe(
        (params: Params) => {
          console.log(params)
          if(params['typeEventCalendar'] == 'add'){
            this.typeMode = false;
            this.typeLink = 'إنشاء حدث';
          } else if(params['typeEventCalendar'] == 'edit') {
            this.spinner.show();
            this.typeMode = true;
           this.eventCalendarId = parseInt(params.eventCalendarId);
            this.typeLink = 'تعديل حدث';
            this.dashboardSer.getItemData(this.eventCalendarId, 'eventCalendar').subscribe(
              (responce) => {
                console.log(responce);
                this.eventCalendar = responce['event_calendar'];
                console.log(this.eventCalendar);
                this.spinner.hide();
              },
              (error) => {
                console.log(error);
              }
            );

          } else {
            this.toastr.warning('Catch Error Go To Home !');
          }
        }
      );


    }//ngOnInit



      onSubmitted() {
        this.checkDisabledSave = true;
        const dataFormEventBar = this.dataForm.value;
        console.log(dataFormEventBar);



        if(this.typeMode) {

          this.dashboardSer.editItem(dataFormEventBar, this.eventCalendarId, 'eventCalendar').subscribe(
            (responce) => {
              console.log(responce);
              this.toastr.success('تم التعديل بنجاح ');
              this.checkDisabledSave = false;
              this.location.back();
            },
            (error) => {
              console.log(error);
              this.checkDisabledSave = false;
              this.toastr.error(' خطأ في التعديل !');
            }
          );
        } else {

          this.dashboardSer.addItem(dataFormEventBar,'eventCalendar').subscribe(
            (responce) => {
              this.toastr.success('تم الاضافه بنجاح');
              this.checkDisabledSave = false;
              console.log(responce);
              this.location.back();
            },
            (error) => {
              console.log(error);
              this.checkDisabledSave = false;
              this.toastr.error('خطأ في الاضافه');
            }
          );
        }
      }



}