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

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


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

checkEditMode: boolean = false;
departId: number;
adminstartionsList = [];
typeLink: string = '';
checkSaveDisabled:boolean = false;

section = {
  name: '',
  email: '',
  phone: '',
  switch_phone: '',
  status: '1',
  adminstration_id: '',
}

@ViewChild('f') addSectionForm: NgForm;

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.internalHeader = false;
  
  this.userService.getUserDataProfile();
    
    //get adminstartionsList
    this.sectionService.getAdinstration().subscribe(
      (responce) => {
        console.log(responce);
        this.adminstartionsList = responce['adminstrations'];
      },
      (error) => {
        console.log(error);
      }
    )

  this.route.params.subscribe(
    (params: Params) => {
      if(params['typeSectionMode'] == 'edit') {
        this.typeLink = 'تعديل';
        this.checkEditMode = true;
        this.departId = params['editSectionId'];
        this.sectionService.getDataSection(this.departId).subscribe(
          (responce) => {
            console.log(responce);
            this.section.name = responce['department'].name;
            this.section.email = responce['department'].email;
            this.section.phone = responce['department'].phone;
            this.section.switch_phone = responce['department'].switch_phone;
            this.section.status = responce['department'].status;
            this.section.adminstration_id = responce['department'].adminstration_id;
            this.spinner.hide();
          },
          (error) => {
            console.log(error);
          }
        )
      } else {
        this.typeLink = 'أنشاء جديد';
        this.spinner.hide();
      }
    }
  );


}

//add function 
onSubmitted() {

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

perviousLocation() {
  this.location.back();
}


}
