import { Component, OnInit, ViewChild } from '@angular/core';
import * as moment from 'moment';
import { HyperService } from '../../core/services/http.service';
import { Router } from "@angular/router";
import { LocalStorage } from '../../core/services/local_storage.service';
import { StatusPopupComponent } from '../status-popup/status-popup.component';
@Component({
  selector: 'app-myusers',
  templateUrl: './myusers.component.html',
  styleUrls: ['./myusers.component.scss']
})
export class MyusersComponent implements OnInit {
  sales_date: any;
  user_dashboard: any;
  percentage: any;
  loader: boolean;
  leader_deatail: any;
  user_data: any;
  total_count: any;
  leader_type :any;
  is_filter: boolean;
  filter_year: any;
  filter_month: any;
  filter_date: any;
  selected_type: any = 1;
  times = {
    year: 31557600,
    month: 2629746,
    day: 86400,
    hr: 3600,
    min: 60,
    sec: 1
  }
  user_role:any;
  rolewise: string = '';
  date_now:any;
  dealerlist: any;
  selected_title_dealer: string = '';
  selected_dealer_code: string = '';
  dselist: any;
  @ViewChild(StatusPopupComponent) content_modal: StatusPopupComponent;

  constructor(private server: HyperService, private router: Router) {
    if (LocalStorage.getValue('user_data') != undefined){
      this.user_role = LocalStorage.getValue('user_data').role;
    }
    this.is_filter = this.user_role == 7 ? true : false;
  }

  ngOnInit() {
    if(this.user_role == 7 ){
      this.getdealerlist()
    }else{
      this.selected_dealer_code = LocalStorage.getValue('user_data').dealer_code;
      this.getdselist();
    }
  }

  getdealerlist(){
    this.loader = true;
    this.server.get('manager/dealerlist').then(data=>{
      this.loader = false;
      if(data.status == 200 && data.result.response == 'success'){
        this.dealerlist = data.result.data;
        this.selected_title_dealer = this.dealerlist[0].dealer_name;
        this.selected_dealer_code=this.dealerlist[0].dealer_code;
        this.getdselist()
      }else if (data.result.status == 406  ) {
        this.loader = false
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {
        this.router.navigateByUrl('');
      }
    })
  }

  radioSelectDealer(val) {
    for (let i = 0; i < this.dealerlist.length; i++) {
        if (val == i) {
            this.dealerlist[i].is_selected = 1;
            this.selected_title_dealer = this.dealerlist[i].dealer_name
            this.selected_dealer_code=this.dealerlist[i].dealer_code
        } else {
            this.dealerlist[i].is_selected = 0;
            this.dealerlist[i].selected_title_dealer = 0;
        }
    }
    this.getdselist();
  }

  getdselist(){
    this.loader = true;
    this.server.post('manager/userlist',{ 'dealer_code' : this.selected_dealer_code }).then(data=>{
      this.loader = false;
      if(data.status == 200 && data.result.response == 'success'){
        this.dselist = data.result.data;
        console.log(this.dselist)
      }else if (data.result.status == 406  ) {
        this.loader = false
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {
        this.router.navigateByUrl('');
      }
    })
  }

  changestatus(status,list){
    console.log(status)
    this.content_modal.showconfirmModal('Result', status,list,(data) => {
      this.getdselist();
    })
  }
}
