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-external-achievement',
  templateUrl: './add-external-achievement.component.html',
  styleUrls: ['./add-external-achievement.component.css']
})
export class AddExternalAchievementComponent 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;
    externalAchievementId:number;
    checkDisabledSave:boolean = false;

    externalAchievement = {
      name: '',
      name_en: '',
      achievement_time: '',
      description: '',
      description_en: '',
      ranking: '',
      status: 1,
    };

    higriDateVal = "";
    bindingDateSplit:any;
    ngOnInit() {

      this.route.params.subscribe(
        (params: Params) => {
          console.log(params)
          if(params['typeExternalAchievement'] == 'add'){
            this.typeMode = false;
            this.typeLink = 'إنشاء إنجاز';
          } else if(params['typeExternalAchievement'] == 'edit') {
            this.spinner.show();
            this.typeMode = true;
           this.externalAchievementId = parseInt(params.externalAchievementId);
            this.typeLink = 'تعديل إنجاز';
            this.dashboardSer.getItemData(this.externalAchievementId, 'externalAchievement').subscribe(
              (responce) => {
                console.log(responce);
                this.externalAchievement = responce['external_achievement'];
                const date = responce['external_achievement'].achievement_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;

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

          } else {
            console.log('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['achievement_time']  = this.higriDateVal;
            console.log(dataFormEventBar);

            if(this.typeMode) {

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



}
