import {  OnInit, Component, ViewChild } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { SharedService } from '../../../shared/shared.service';
import { ActivatedRoute, Params } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Location } from '@angular/common';
import { NgForm } from '@angular/forms';
import { PagesService } from '../../../shared/pages.service';

@Component({
  selector: 'ngx-system-admins-add',
  templateUrl: './system-admins-add.component.html',
  styleUrls: ['./system-admins-add.component.scss']
})



export class SystemAdminsAddComponent implements OnInit {
// @ViewChild('f') definitionForm : NgForm;
@ViewChild('f') superAdminForm : NgForm
  constructor(
    private toastr: ToastrService,
    private auth: SharedService ,
    private route: ActivatedRoute,
    private location: Location,
    private http: HttpClient,
    public pageserv: PagesService
  ) { }


  typePage: string = '';
  valueType: string = '';
  checkSaveclick:boolean = false;
  check : boolean = false;
  isEdit: boolean = false;
  editPageId: number;    
  notMatch: boolean = false;   

  formData = {
    name: '',
    email:'',
    password:'',
    password_confirmation:''
    
  }

  ngOnInit() {
    this.route.params.subscribe(
      (params: Params) => {
        this.editPageId = +params['adminId'];
        console.log('edit page id',this.editPageId);
      }
    );
    if(this.editPageId){
      this.isEdit = true;
      this.typePage = 'Edit Currunt Admin';
      this.pageserv.getItemData(this.editPageId, 'system-admins')
        .subscribe(
          res => {
            console.log('here',res);
            this.formData.name = res['user'].name;
            this.formData.email = res['user'].email;
          },
          err =>{
            console.log(err);
            
          }
        );
    }else{
      this.typePage = 'Add New Admin'
    }
  }

  onSubmitted(){
    this.superAdminForm.value['type'] = 'super_admin';
    // console.log(this.superAdminForm.value);
    if(this.superAdminForm.value['password'] != this.superAdminForm.value['password_confirmation']){
      this.notMatch = true;
    
    }
    else{
      this.notMatch = false;
      if(this.editPageId){
      this.pageserv.editItem(this.editPageId, this.superAdminForm.value, 'system-admins')
            .subscribe(
              res => {
                console.log(res);
                this.toastr.success('Done , edited successfully');
                this.location.back();

              },
              err => {
                console.log(err);
                this.toastr.error('sorry something went wrong !')
                
              }
            );
      }else{
        console.log(this.superAdminForm.value);
      this.pageserv.addItem(this.superAdminForm.value , 'system-admins')
          .subscribe(
            res => {
              console.log('ADD', res);
              this.toastr.success('Added Succesfully');
              this.location.back;
              this.checkSaveclick = false;
            },
            err =>{
              console.log(err);
              this.checkSaveclick = false;
              this.toastr.error('Sorry something went wrong !');
            }
          );
    }
    
  }
 }
}
