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-statistic',
  templateUrl: './add-statistic.component.html',
  styleUrls: ['./add-statistic.component.css']
})
export class AddStatisticComponent 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) { }

    months = [];
    typeMode:boolean = false; //default false for create page
    typeLink: string;
    statisticId:number;
    checkDisabledSave:boolean = false;

    statistic = {
      name: '',
      name_en: '',
      value: '',
      month_id: '',
      year: '',
      status: 1,
    };


    ngOnInit() {

      //get adminstration
      this.userSer.getMonth().subscribe(
        (responce) => {
          console.log(responce);
          this.months = responce['months'];
        },
        (error) => {
          console.log(error)
        }
      );

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

          } else {
            console.log('Catch Error Go To Home !');
          }
        }
      );


    }//ngOnInit

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

      if(this.typeMode) {

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

}
