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

@Component({
  selector: 'app-add-department',
  templateUrl: './add-department.component.html',
  styleUrls: ['./add-department.component.css']
})

export class AddDepartmentComponent implements OnInit {

  constructor(private authService: AuthServiceService,
      private userService: UserService,
      private departmentSer: DepartmentService,
      private spinner: NgxSpinnerService,
      private toastr: ToastrService,
      private router: Router,
      private authSer: AuthServiceService,
      private route: ActivatedRoute,
      private location: Location) {
        this.spinner.show();
       }

  checkEditMode: boolean = false;

  departId: number;

  admin = {
    name: '',
    description: '',
    email: '',
    phone: '',
    switch_phone: '',
    status: '1',
  }

  typeLink: string = '';
  checkSaveDisabled: boolean = false;
  @ViewChild('f') addDepertmentForm;

  ngOnInit() {


    this.userService.getUserDataProfile();
    //show / hide notification search in header
    this.authSer.notificationLogin = true;
    this.authSer.showSearchHeader = false;
    this.authSer.showHeaderLogin = false;
    this.authSer.showHeaderDashBoard = true;
    //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.route.params.subscribe(
      (params: Params) => {
        if(params['typeAdminMode'] == 'edit') {
          this.typeLink = 'تعديل';
          this.checkEditMode = true;
          this.departId = params['editAdminId'];
          this.departmentSer.getDataAdmin(this.departId).subscribe(
            (responce) => {
              console.log(responce);
              this.admin.name = responce['adminstration'].name;
              this.admin.email = responce['adminstration'].email;
              this.admin.phone = responce['adminstration'].phone;
              this.admin.switch_phone = responce['adminstration'].switch_phone;
              this.admin.status = responce['adminstration'].status;
              this.admin.description = responce['adminstration'].description;
              this.spinner.hide();
            },
            (error) => {
              console.log(error);
            }
          )
        } else {
          this.typeLink = 'إنشاء جديد';
          this.spinner.hide();
        }
      }
    )
  }

  //add function 
  onSubmitted() {

    this.checkSaveDisabled = true;

    console.log(this.addDepertmentForm.value);
    if(this.checkEditMode) {
      this.departmentSer.editAdmin(this.addDepertmentForm, this.departId).subscribe(
        (responce) => {
          this.checkSaveDisabled = false;
          console.log(responce);
          this.toastr.success('تم التعديل');
          this.location.back();
        },
        (error) => {
          this.toastr.error('خطأ تعديل');
          this.checkSaveDisabled = false;
          console.log(error);
        }
      );
    } else {
      this.departmentSer.addDepartment(this.addDepertmentForm).subscribe(
        (responce) => {
          this.checkSaveDisabled= false;
          console.log(responce);
          this.toastr.success('تم الاضافه بنجاح');
          this.location.back();
        },
        (error) => {
          this.checkSaveDisabled= false;
          console.log(error);
          this.toastr.error('خطأ الاضافه !');
        }
      );
    }
  }

}
