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


@Component({
  selector: 'app-females-request-types-add',
  templateUrl: './females-request-types-add.component.html',
  styleUrls: ['./females-request-types-add.component.css']
})
export class FemalesRequestTypesAddComponent implements OnInit {
  editPageId: number;
  isEdit: boolean;
  typePage: string;
  checkSaveclick: boolean;
  disable: boolean;

  constructor(
    private dashBoardSer: DashboardService,
    private toastr: ToastrService,
    public authSer: AuthServiceService,
    private route: ActivatedRoute,
    private spinner: NgxSpinnerService,
    private location: Location,
    private http: HttpClient
  ) { }

  // @ViewChild('f') definitionForm : NgForm;

  formData = {
    name: '',
    description:'',
    require_attachments:'1',
    require_approval:'1',
    require_date:'0',
  }

  ngOnInit() {
 
    //init the values of permision boolean
  this.authSer.showAddBtn = false;
  this.authSer.showDeleteBtn = false;
  this.authSer.showEditBtn = false;
  //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) => {
        this.editPageId = +params['editPageId'];
        console.log(this.editPageId);
        
      }
    );


    if (this.editPageId) {
      this.spinner.show();
      this.isEdit = true;
    this.typePage = 'تعديل';
      this.dashBoardSer.getItemData(this.editPageId, 'femalesRequestTypes' ).subscribe(
        res => {
          console.log('ssss', res);
         this.formData.name = res['type'].name;
         this.formData.description = res['type'].description;
         this.formData.require_attachments = res['type'].require_attachments;
         this.formData.require_approval = res['type'].require_approval;
         this.formData.require_date = res['type'].require_date;
       

   
          this.spinner.hide();
        },
        (error) => {
          console.log(error);
        }
      );
    }else {
      this.typePage = 'اضافة';
    }

  }

  // make_require_date_true(){
  //     this.require_date = true;
      
  // }
  // make_require_date_false(){
  //   this.require_date = false;

  // }
 

  onSubmitted() {
    console.log('HERE',this.formData);
 
    this.disable = false;
    if(this.editPageId) {
      this.dashBoardSer.editItem(this.editPageId, this.formData, 'femalesRequestTypes').subscribe(
        (responce) => {
          this.disable = true;
          console.log(responce);
          this.toastr.success('تم التعديل بنجاح');
          this.location.back();
        },
        (error) => {
          this.disable = false;
          console.log(error.error);
          if(error.error.error  == 'Requests already uses this type can not modify it'){
            this.toastr.warning('لا يوجد صلاحية لتعديل الطلب')
          }else{
            console.log(error);
          this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
          }
          
        }
      )
    } else {
    this.dashBoardSer.addItem( this.formData , 'femalesRequestTypesAdd').subscribe(
      (responce) => {
        console.log('ADD',responce);
        this.toastr.success('تم الإضافه بنجاح');
        this.checkSaveclick = false;
        this.location.back();
      },
      (error) => {
        console.log(error);
        this.checkSaveclick = false;
        this.toastr.error('خطأ في الخادم ، حاول لاحقا');
      }
    );
    }
  }

}
