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-bar-event',
  templateUrl: './add-bar-event.component.html',
  styleUrls: ['./add-bar-event.component.css']
})
export class AddBarEventComponent 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;
    barEventId:number;
    checkDisabledSave:boolean = false;

    barEvent = {
      name: '',
      name_en: '',
      description: '',
      description_en: '',
      status: 1,
    };

    higriDateVal = "";
    bindingDateSplit:any;


  ngOnInit() {

    this.route.params.subscribe(
      (params: Params) => {
        if(params['typeEventBar'] == 'add'){
          this.typeMode = false;
          this.typeLink = 'إنشاء حدث';
        } else if(params['typeEventBar'] == 'edit') {
          this.spinner.show();
          this.typeMode = true;
         this.barEventId = parseInt(localStorage.getItem('editbarEventIdStorage'));
         //alert(this.barEventId);
          this.typeLink = 'تعديل حدث';
          this.dashboardSer.getItemData(this.barEventId, 'barEvent').subscribe(
            (responce) => {
              console.log(responce);
              this.barEvent = responce['event'];
              console.log(this.barEvent);
              const date = responce['event'].event_time.split('-');

              this.bindingDateSplit = {
                year: parseInt(date[0]),
                month: parseInt(date[1]),
                day: parseInt(date[2])
              };

              this.higriDateVal = this.bindingDateSplit.year + '-' + this.bindingDateSplit.month + '-' + this.bindingDateSplit.day;

              this.spinner.hide();
            },
            (error) => {
              console.log(error);
            }
          );

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


  }//ngOnInit

  //get value date from child component
  public getDate(date: any):void {
    console.log( date);
    this.higriDateVal = date.year + '-' + date.month + '-' + date.day;
    console.log('higrii date', this.higriDateVal);
  }

  onSubmitted() {
    this.checkDisabledSave = true;
    const dataFormEventBar = this.dataForm.value;
    dataFormEventBar['event_time']  = this.higriDateVal;
    console.log(dataFormEventBar);



    if(this.typeMode) {

      this.dashboardSer.editItem(this.barEventId,dataFormEventBar, 'barEvent').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,'barEvent').subscribe(
        (responce) => {
          this.toastr.success('تم الاضافه بنجاح');
          this.checkDisabledSave = false;
          console.log(responce);
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.checkDisabledSave = false;
          this.toastr.error('خطأ في الاضافه');
        }
      );
    }
  }

}
