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


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

  @ViewChild('f') dataForm: NgForm;

  constructor(private userSer: UserService, 
    private authSer: AuthServiceService, 
    private toastr: ToastrService,
    private route: ActivatedRoute,
    private spinner: NgxSpinnerService,
    private location: Location,
    private achievService: AchievementsService) { }
  
    typeMode: boolean = false;
    typeLink: string = '';
    achievId: number;
    checkDisabledSave:boolean = false;
  
    achiv = {
      title: '',
      title_en: '',
      description: '',
      description_en: '',
      date: '',
      status: '',
      ranking: '',
    }

  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.userSer.getUserDataProfile();
    this.route.params.subscribe(
      (params: Params) => {
        if(params['typeAchieventMode'] == 'edit') {
          this.spinner.show();
          this.typeLink = 'تعديل';
          this.typeMode = true;
          this.achievId = params['editAchievementId'];
          this.achievService.getAchievementData(this.achievId).subscribe(
            (responce) => {
              console.log(responce);
              this.achiv.title = responce['achievement'].title;
              this.achiv.title_en = responce['achievement'].title_en;
              this.achiv.description = responce['achievement'].description;
              this.achiv.description_en = responce['achievement'].description_en;
              this.achiv.date = responce['achievement'].achievement_time;
              this.achiv.status = responce['achievement'].status;
              this.achiv.ranking = responce['achievement'].ranking;
              this.spinner.hide();
            }
          );
        } else {
          this.typeLink = 'إنشاء جديد';
        }
      }
    )
  }

  onSubmitted() {
    
    this.checkDisabledSave = true;

    const dataFormAchiev = this.dataForm.value;
    if(this.typeMode) {
      this.achievService.editAchievement(dataFormAchiev, this.achievId).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.achievService.addAchievements(dataFormAchiev).subscribe(
        (responce) => {
          this.toastr.success('تم الاضافه بنجاح');
          this.checkDisabledSave = false;
          console.log(responce);
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.checkDisabledSave = false;
          this.toastr.error('خطأ في الاضافه');
        }
      );
    }
  }

}
