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

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

  @ViewChild('f') formTabData: NgForm;

  typeMode: boolean = false;
  eventId: number;
  tabId: number;
  typeLink: string = '';

  tab = {
    name: '',
    name_en: '',
    ranking: '',
    type: '',
    status: '',
  }
  

  constructor(private tabService: TabsService,
    private userService: UserService,
    private location: Location,
    private authSer: AuthServiceService,
    private toastr: ToastrService,
    private route: ActivatedRoute, 
    private spineer: NgxSpinnerService) { }

  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.route.params.subscribe(
      (params: Params) => {
        if(params['typeTabMode'] == 'edit') {
          this.spineer.show();
          this.typeMode = true;
          this.typeLink = 'تعديل';
          this.tabId = params['editTabId'];
          this.tabService.getTabData(this.tabId).subscribe(
            (responce) => {
              console.log(responce);
              const tabData = responce['tab'];
              console.log(tabData);
              this.tab.name = tabData.name;
              this.tab.name_en = tabData.name_en;
              this.tab.ranking = tabData.ranking;
              this.tab.type = tabData.type;
              this.tab.status = tabData.status;
              this.spineer.hide();
            },
            (error) => {
              console.log(error);
            }
          )
        } else {
          this.typeLink = 'إنشاء جديد';
        }
      }
    );
  }



  //on submitted form
  onSubmitted() {
    console.log(this.formTabData.value);
    if(this.typeMode) {
      this.tabService.editEvent(this.formTabData.value, this.tabId).subscribe(
        (responce) => {
          console.log(responce);
          this.toastr.success('تم التعديل بنجاح');
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.toastr.error('خطأ في التعديل ');
        }
      )
    } else {
      this.tabService.addTab(this.formTabData.value).subscribe(
        (responce) => {
          console.log(responce);
          this.toastr.success('تم الاضافه بنجاح');
          this.location.back();
        },
        (error) => {
          console.log(error);
          this.toastr.error('خطأ في الأضافه ');
        }
      );
    }
  };

}
