import { AuthServiceService } from './../../../shared/auth-service.service';
import { NgxSpinnerService } from 'ngx-spinner';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { DashboardService } from './../../../shared/dashboard.service';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Modal } from 'ngx-modialog/plugins/bootstrap';
import { UserService } from '../../../shared/user.service';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-contact-us-list',
  templateUrl: './contact-us-list.component.html',
  styleUrls: ['./contact-us-list.component.css']
})
export class ContactUsListComponent implements OnInit {

  constructor(private http: HttpClient,
    private route: ActivatedRoute,
    private router: Router,
    private modal: Modal,
    private userSer: UserService,
    private spinner: NgxSpinnerService,
    private authSer: AuthServiceService,
    private toastr: ToastrService,
    private dashboardSer: DashboardService) { }

    contacUsList = [];
    pages= [];
    pageId: number;
    contacUsListIds = [];
    count: number;
    perPagePagenation: number;
    currentPage:number = 1;
    filtterStatus = '';
    selectedAll: any;
    userLoginId:number;
    serviceId:number;
    dataTableNumber: number = 5;

    ngOnInit() {
      //init values of permisions
      this.authSer.showAddBtn = false;
      this.authSer.showDeleteBtn = false;
      this.authSer.showEditBtn = false;
      this.authSer.showPermissionsBtn = 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;
      //to show / hide permissions

      this.route.params.subscribe(
        (parmas: Params) => {
          this.pageId = parmas['listPageId'];
          localStorage.setItem('pageIdActive', parmas['listPageId']);
        }
      );


      this.route.parent.params.subscribe(
        (params:Params) => {
          this.userLoginId = params['userID'];
          this.serviceId = params['serviceID'];
          this.userSer.getPagesPermetiotns(this.userLoginId, this.serviceId).subscribe(
            (responce) => {
              console.log(responce);
              this.pages = responce['pages'];
              for(let i = 0; i< this.pages.length; i++) {
                if(this.pages[i].id == 32) {
                  for(let j = 0; j < this.pages[i].permissions.length; j++) {
                    if(this.pages[i].permissions[j].name == "edit_contact_us"){
                      this.authSer.showEditBtn = true;
                    }
                  }
                }else {
                  console.log('no contact_us');
                }
              }
              this.spinner.hide();
            },
            (error) => {console.log(error)}
          );
        }
      );

     
      this.dashboardSer.getListData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
        (responce) => {
          console.log(responce);
          this.contacUsList = responce['contact_us'];
          this.contacUsList[0].field = this.contacUsList[0].field.substring(0,50) + '........';
          this.count = responce['count'];
          this.perPagePagenation = responce['per_page'];
        },
        (error) => {
          console.log(error);
        }
      );

    } //ngOnInit

          //make all checkbox of user checked
          selectAll() {
          for (var i = 0; i < this.contacUsList.length; i++) {
            this.contacUsList[i].selected = this.selectedAll;
          }
        };

        checkIfAllSelected() {
          this.selectedAll = this.contacUsList.every(function(item:any) {
            return item.selected == true;
          });
        };


      //edit function
      onEdit(editTabID) {
      localStorage.setItem('editcontactUsIdStorage', editTabID);

      this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/contactUs/edit/' +editTabID]);
      console.log('service/' + this.userLoginId + '/' + this.serviceId + '/contactUs/edit/' + editTabID);
      };



  }
