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

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

  @ViewChild('f') definitionForm : NgForm;

  typePage: string = '';
  categories =[];
  valueType: string = '';
  checkSaveclick:boolean = false;
  check : boolean = false;
  isEdit: boolean = false;
  types = [
    {
      name : 'تصنيف رئيسي',
      value : 'main'
    },
    {
      name : 'تصنيف فرعي',
      value : 'sub'
    }
  ];

  
  editPageId: number;



  formData = {
    type : '',
    status :'1',
    code: '',
    name: '',
    parent_id: null,
    // parent_name: ''
  }


  ngOnInit() {
    this.http.get(this.authSer.pathApi + '/parent_categories_list').subscribe(
      (response) => {
        
        this.categories = response['categories']
        console.log('XXXX',this.categories);
      },
      (error) => {
        console.log(error);
      }
    );
    this.route.params.subscribe(
      (params: Params) => {
        this.editPageId = +params['editPageId'];
      }
    );

      if (this.editPageId) {
        this.spinner.show();
        this.isEdit = true;
      this.typePage = 'تعديل';
        this.dashBoardSer.getItemData(this.editPageId, 'definitionOfClassification' ).subscribe(
          (response) => {
            console.log('ssss', response);
            this.formData.code  = response['category'].code;
            this.formData.name  = response['category'].name;
            this.formData.parent_id = response['category'].parent_id;
            this.formData.status = response['category'].status;
            if (this.formData.parent_id != null) {
              this.formData.type = 'sub';
              this.check = true;
            } else {
              this.formData.type = 'main';
            }
            // this.formData.parent_name = response['category'].parent_name;
            // console.log(this.formData.parent_name );
            this.spinner.hide();
          },
          (error) => {
            console.log(error);
          }
        );
      }else {
        this.typePage = 'اضافة';
      }

  }
  onChangeType(event){
    this.valueType = event.target.value;
    console.log(this.valueType)
  }

 
  onSubmitted() {
    console.log('HERE',this.definitionForm.value);
    if(this.editPageId) {
      this.dashBoardSer.editItem(this.editPageId, this.definitionForm.value, 'definitionOfClassification').subscribe(
        (responce) => {
          console.log(responce);
          this.toastr.success('تم التعديل بنجاح');
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
        }
      )
    } else {
    this.dashBoardSer.addItem( this.definitionForm.value , 'definitionOfClassification').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('خطأ في الخادم ، حاول لاحقا');
      }
    );
    }
  }
 
}
