import { Component, OnDestroy, OnInit } from '@angular/core';
import { CommonFunctionService } from '../services/commonfunctions.service';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
import { DataTableDirective } from 'angular-datatables';
import { ViewChild } from '@angular/core'
import { log } from 'console';

// import * as $ from 'jquery';
// import 'datatables.net';
// import 'datatables.net-bs4';

//declare var $:any; 
@Component({
  selector: 'dvr-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  @ViewChild(DataTableDirective, { static: false })
  dtElement: DataTableDirective;


  user_id: any;
  username: any;
  no_of_visit: any;
  com_entry: any;
  no_contact: any;
  incom_entry: any;
  logindata: any = [];
  last_login_date: any;
  countData: any = [];
  dvrList: any = [];
  conformlist: any = [];
  startDate: any = "";
  endDate: any = "";
  current_date: any;
  dtOptions: any = {};
  dtTrigger: Subject<any> = new Subject();
  // dataTable: any;
  add_css: any;
  add_css1: any;
  add_css2: any;
  sale_person: any;
  customer: any;
  check_role: boolean = false;
  role_check: any;
  status_msg: any;
  error_msg: any;
  displayStyle = "none";
  salePersonlist: any = [];
  datalist: any = [];
  cust_data: any = [];

  constructor(private commonservice: CommonFunctionService, private router: Router) {

    console.log("in home");
  }


  ngOnInit() {
    localStorage.removeItem("autofilldata");
    localStorage.removeItem("contactlastinsert")


    this.add_css1 = "w3-red";
    // this.current_date = new Date();
    // var dd = this.current_date.getDate();
    // var mm = this.current_date.getMonth() + 1; //January is 0!
    // var yyyy = this.current_date.getFullYear();
    // if (dd < 10) {
    //   dd = '0' + dd
    // }
    // if (mm < 10) {
    //   mm = '0' + mm
    // }

    // this.current_date = yyyy + '-' + mm + '-' + dd;
    // document.getElementById("startDate").setAttribute("max", this.current_date);
    // document.getElementById("endDate").setAttribute("max", this.current_date);

    //console.log("auth =====>",this.auth.isAuthenticated());
    // if(this.auth.isAuthenticated){

    // }




    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 15,
      processing: true,
      order: [],
      ordering: false,
      // order: [[ 2, "asc" ]],
      // columnDefs : [{"targets":2, "type":"date"}],
      // order: [[2, 'desc']],//order column in descending order.
      // columnDefs: [
      //   { "targets": 2, "type": "date-eu" }//date column formatted like "03/23/2018 10:25:13 AM".
      // ],

      //   columnDefs: [
      //     {
      //         "type": "date",
      //         "targets": 2
      //     }

      // ],

      // order: [{column: "2", dir: "desc"}]

      lengthMenu: [15, 25, 35],
      dom: 'Blfrtip',
      buttons: [
        'csv', 'excel', 'print'
      ]

      // buttons: [
      //   {
      //     extend: 'print',
      //     title: 'Data export'
      //   },
      //   {
      //     extend: 'excelHtml5',
      //     title: 'Data export'
      //   }

      // ]

    }


    this.commonservice.getAllData(this.commonservice.projectpath + "/getsalepersonlist").subscribe(response => {
      if (response.status == 'tokenfalse') {
        this.router.navigate(['/login'])

      }
      else {

        console.log("response of sale person list", response);
        console.log("response========================>", response.data[0]);

        this.salePersonlist.push(response.data[0].salePerson);
        // this.datalist.push(this.salePersonlist[0]); 
        for (var i = 0; i < this.salePersonlist.length; i++) {
          this.datalist.push(this.salePersonlist[i])
        }

        console.log("salePersonlist---------->", this.salePersonlist)
        console.log("this.salePersonlist", this.datalist);
      }
    });

    this.logindata.push(JSON.parse(localStorage.getItem("loginData")));
    this.username = this.logindata[0]["user_name"];
    this.last_login_date = this.logindata[0]["last_loginDate"];
    this.user_id = this.logindata[0]["user_id"];
    this.role_check = this.logindata[0]["role"]
    console.log(" this.logindata", this.logindata);
    console.log("role_check=====>", this.role_check);
    if (this.role_check == 'admin') {
      console.log("admin use dashboard")
      this.check_role = true
    }

    this.commonservice.getAllData(this.commonservice.projectpath + "/getselectionData").subscribe(response => {
      if (response.status == 'tokenfalse') {
        this.router.navigate(['/login'])

      }
      else {
        // this.cust_data = response.data[0].customerData;
        this.cust_data = response.data[0].customerData;
        if(!this.check_role) {
          this.cust_data = this.cust_data.filter(item => item.first_name + " " + item.last_name === this.username);
        }
        this.cust_data = Array.from(
        new Map(
          this.cust_data.map(item => [
            `${item.customer_id}-${item.customer_name}`,
            {
              customer_id: item.customer_id,
              customer_name: item.customer_name
            }
          ])
        ).values()
      );
        console.log(this.cust_data);
      }
    });

    this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?login_id=" + this.user_id).subscribe(response => {


      // console.log("status",response.status)
      if (response.status == 'tokenfalse') {
        console.log("uuuuuuuuuuuuuuuuu")
        this.router.navigate(['/login'])
      } else {
        console.log("uuuuuuuuuuuuu");
        console.log("respose", response);
        //   console.log("uuuuuuuuuuu",response.data[0]);
        console.log("uuuuuuuuuuu", response.data[0].getdvrDataStatus);

        if (response.data[0].getdvrDataStatus == '0') {

          //  console.log("iiiiiiiiiiiiiiiii",response);

          this.countData = response.data[0]["countdata"];
          //  console.log("this.countData",this.countData);
          this.dvrList = response.data[0]["dvrlist"];
          this.conformlist = this.dvrList;
          this.no_of_visit = this.countData[0].no_of_visit;
          this.com_entry = this.countData[0].no_of_completed;
          this.incom_entry = this.countData[0].no_of_incompleted;
          this.no_contact = this.countData[0].no_of_contact;

          console.log(" this.dvrList", this.dvrList);


        } else {
          this.no_of_visit = 0;
          this.com_entry = 0;
          this.incom_entry = 0;
          this.no_contact = 0;

          $(document).ready(function () {
            $('.odd').show();

          });
        }

      }

      this.dtTrigger.next(this.dtOptions)



    });



  }


  rerender(): void {
    this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
      dtInstance.clear();
      // Destroy the table first
      dtInstance.destroy();
      // Call the dtTrigger to rerender again
      this.dtTrigger.next(this.dtOptions);
    });
  }



  searchDvr(searchfilter: any) {
    this.sale_person = "undefined";


    //this.sale_person = "";
    if (searchfilter == '0') {

      this.add_css = "w3-red";
      this.add_css1 = "";
      this.add_css2 = "";
    } else if (searchfilter == '1') {
      this.add_css1 = "w3-red";
      this.add_css = "";
      this.add_css2 = "";
    } else {
      this.add_css1 = "";
      this.add_css = "";
      this.add_css2 = "w3-red";
    }

    this.dvrList = [];

    console.log("searchfilter", searchfilter);

    console.log("dvrlist", this.dvrList);
    this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id).subscribe(response => {

      if (response.status == 'tokenfalse') {
        this.router.navigate(['/login'])


      } else {
        if (response.data[0].getdvrDataStatus == '0') {
          // this.ngOnDestroy();
          this.rerender();
          // this.countData = response["countdata"];
          // this.dvrList = response["dvrlist"];

          // console.log(" this.dvrList", this.dvrList);
          // this.no_of_visit = this.countData[0].no_of_visit;
          // this.com_entry = this.countData[0].no_of_completed;
          // this.incom_entry = this.countData[0].no_of_incompleted;
          // this.no_contact = this.countData[0].no_of_contact;

          this.countData = response.data[0]["countdata"];
          //  console.log("this.countData",this.countData);
          this.dvrList = response.data[0]["dvrlist"];

          this.no_of_visit = this.countData[0].no_of_visit;
          this.com_entry = this.countData[0].no_of_completed;
          this.incom_entry = this.countData[0].no_of_incompleted;
          this.no_contact = this.countData[0].no_of_contact;

        } else {
          this.no_of_visit = 0;
          this.com_entry = 0;
          this.incom_entry = 0;
          this.no_contact = 0;
          $(document).ready(function () {
            $('.odd').show();

          });
        }


      }



    });

  }

  onSearch() {

    console.log("00000000000", this.sale_person);

    // if(this.customer == undefined){
    // console.log("first one")

    // }else if(this.customer == 'undefined'){
    //   console.log("second one")

    // }else{
    //   console.log("third one");
    // }

    console.log("customer", this.customer);
    console.log("start date,", this.startDate);
    console.log("start date,", this.endDate);

    //  console.log("customer name",this.customer.customer_name);

    var searchfilter, result, result1;

    this.dvrList = [];
    if (this.startDate != undefined) {
      result = this.startDate.replace("-", "/");
      console.log("result", result);
      result = result.replace("-", "/");
      console.log("result1", result);
    }
    if (this.endDate != undefined) {
      result1 = this.endDate.replace("-", "/");
      console.log("result--->", result1);
      result1 = result1.replace("-", "/");
      console.log("result1-->", result1);
    }

    console.log("endDate date--->", searchfilter);

    if (this.endDate == "" && this.startDate != "") {
      //need show pop-up
      //try to enter date
      console.log('in date filters for 1');
      this.openPopup();
      this.status_msg = "Warning";
      this.error_msg = "Please enter Dates correctly.";
      this.dvrList = this.conformlist;
    } else if (this.endDate != "" && this.startDate == "") {
      // need to show pop-up
      console.log('in date filters for 2');
      this.openPopup();
      this.status_msg = "Warning";
      this.error_msg = "Please enter Dates correctly.";
      this.dvrList = this.conformlist;

    }
    else {
      this.status_msg = "";
      this.error_msg = "";
      searchfilter = result + "-" + result1;

      console.log("this.sale_person", this.sale_person, "this.customer", this.customer, "searchfilter", searchfilter)
      if (this.sale_person != undefined && this.customer == undefined && this.startDate == "" && this.endDate == "" && this.sale_person != 'undefined') {
        console.log("only sales person");
        this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=undefineddate&login_id=" + this.user_id + "&salePerson=" + this.sale_person).subscribe(response => {
          if (response.status == 'tokenfalse') {
            this.router.navigate(['/login'])
          } else {
            if (response.data[0].getdvrDataStatus == '0') {
              //  this.countData = response["countdata"];
              // this.dvrList = response["dvrlist"];
              this.countData = response.data[0]["countdata"];
              this.dvrList = response.data[0]["dvrlist"];
              this.rerender();
              console.log(" this.dvrList", this.dvrList);
              this.no_of_visit = this.countData[0].no_of_visit;
              this.com_entry = this.countData[0].no_of_completed;
              this.incom_entry = this.countData[0].no_of_incompleted;
              this.no_contact = this.countData[0].no_of_contact;

            } else {
              console.log("in else part");

              this.dvrList = [];

              this.rerender();
              console.log(" this.dvrList", this.dvrList);


              $(document).ready(function () {
                $('.odd').show();

              });
              this.no_of_visit = 0;
              this.com_entry = 0;
              this.incom_entry = 0;
              this.no_contact = 0;

            }


          }

        });
      }
      else if (this.sale_person == undefined && this.customer != undefined && this.endDate == "" && this.startDate == "" && this.sale_person != 'undefined' && this.customer != 'undefined') {
        console.log("only customer")
        this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=undefineddate&login_id=" + this.user_id + "&customer=" + this.customer.customer_name + "&customerid=" + this.customer.customer_id).subscribe(response => {
          if (response.status == 'tokenfalse') {
            this.router.navigate(['/login'])
          } else {
            if (response.data[0].getdvrDataStatus == '0') {
              console.log("response", response);
              //  this.countData = response["countdata"];
              // this.dvrList = response["dvrlist"];
              this.countData = response.data[0]["countdata"];
              this.dvrList = response.data[0]["dvrlist"];
              this.rerender();
              console.log(" this.dvrList", this.dvrList);
              this.no_of_visit = this.countData[0].no_of_visit;
              this.com_entry = this.countData[0].no_of_completed;
              this.incom_entry = this.countData[0].no_of_incompleted;
              this.no_contact = this.countData[0].no_of_contact;

            } else {

              this.dvrList = [];

              this.rerender();
              console.log(" this.dvrList", this.dvrList);


              $(document).ready(function () {
                $('.odd').show();

              });
              this.no_of_visit = 0;
              this.com_entry = 0;
              this.incom_entry = 0;
              this.no_contact = 0;

            }


          }

        });
      } else if (this.sale_person != undefined && this.customer != undefined && this.endDate == "" && this.startDate == "" && this.sale_person != 'undefined') {
        console.log("for sales person and customer");
        this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=undefineddate&login_id=" + this.user_id + "&customer=" + this.customer.customer_name + "&salePerson=" + this.sale_person + "&customerid=" + this.customer.customer_id).subscribe(response => {
          if (response.status == 'tokenfalse') {
            this.router.navigate(['/login'])
          } else {
            if (response.data[0].getdvrDataStatus == '0') {
              //  this.countData = response["countdata"];
              // this.dvrList = response["dvrlist"];
              this.countData = response.data[0]["countdata"];
              this.dvrList = response.data[0]["dvrlist"];
              this.rerender();
              console.log(" this.dvrList", this.dvrList);
              this.no_of_visit = this.countData[0].no_of_visit;
              this.com_entry = this.countData[0].no_of_completed;
              this.incom_entry = this.countData[0].no_of_incompleted;
              this.no_contact = this.countData[0].no_of_contact;

            } else {
              this.no_of_visit = 0;
              this.com_entry = 0;
              this.incom_entry = 0;
              this.no_contact = 0;
              $(document).ready(function () {
                $('.odd').show();

              });
            }


          }

        });

      } else if (this.sale_person == 'undefined' && this.customer == undefined) {
        this.onClear()

      } else if (this.customer == 'undefined' && this.sale_person == undefined) {
        console.log("in else condition for customer");
        this.onClear()

      } else if (this.sale_person != undefined && this.customer == undefined && this.startDate != "" && this.endDate != "" && this.sale_person != 'undefined') {
        console.log("for date and sales_person")
        this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id + "&salePerson=" + this.sale_person).subscribe(response => {
          if (response.status == 'tokenfalse') {
            this.router.navigate(['/login'])
          } else {

            console.log("in searchfilterd");
            if (response.data[0].getdvrDataStatus == '0') {
              console.log(" this.dvrList", this.dvrList);


              this.countData = response.data[0]["countdata"];

              this.dvrList = response.data[0]["dvrlist"];
              this.rerender();

              this.no_of_visit = this.countData[0].no_of_visit;
              this.com_entry = this.countData[0].no_of_completed;
              this.incom_entry = this.countData[0].no_of_incompleted;
              this.no_contact = this.countData[0].no_of_contact;

            } else {

              this.dvrList = [];

              this.rerender();
              console.log(" this.dvrList", this.dvrList);


              $(document).ready(function () {
                $('.odd').show();

              });
              this.no_of_visit = 0;
              this.com_entry = 0;
              this.incom_entry = 0;
              this.no_contact = 0;
            }


          }

        });

      } else if (this.sale_person == undefined && this.customer != undefined && this.endDate != "" && this.startDate != "" && this.sale_person != 'undefined' && this.customer != 'undefined') {
        console.log("for date and customer");
        this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id + "&customer=" + this.customer.customer_name + "&customerid=" + this.customer.customer_id).subscribe(response => {
          if (response.status == 'tokenfalse') {
            this.router.navigate(['/login'])
          } else {

            console.log("in searchfilterd");
            if (response.data[0].getdvrDataStatus == '0') {
              console.log(" this.dvrList", this.dvrList);


              this.countData = response.data[0]["countdata"];

              this.dvrList = response.data[0]["dvrlist"];
              this.rerender();

              this.no_of_visit = this.countData[0].no_of_visit;
              this.com_entry = this.countData[0].no_of_completed;
              this.incom_entry = this.countData[0].no_of_incompleted;
              this.no_contact = this.countData[0].no_of_contact;

            } else {

              this.dvrList = [];

              this.rerender();
              console.log(" this.dvrList", this.dvrList);


              $(document).ready(function () {
                $('.odd').show();

              });
              this.no_of_visit = 0;
              this.com_entry = 0;
              this.incom_entry = 0;
              this.no_contact = 0;
            }


          }

        });

      } else if (this.sale_person != undefined && this.customer != undefined && this.startDate != undefined && this.endDate != undefined) {
        console.log("all filters date,customer and sales_person");
        this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id + "&salePerson=" + this.sale_person + "&customer=" + this.customer.customer_name + "&customerid=" + this.customer.customer_id).subscribe(response => {
          if (response.status == 'tokenfalse') {
            this.router.navigate(['/login'])
          } else {
            console.log("in searchfilterd");
            if (response.data[0].getdvrDataStatus == '0') {
              console.log(" this.dvrList", this.dvrList);

              this.countData = response.data[0]["countdata"];

              this.dvrList = response.data[0]["dvrlist"];
              this.rerender();

              this.no_of_visit = this.countData[0].no_of_visit;
              this.com_entry = this.countData[0].no_of_completed;
              this.incom_entry = this.countData[0].no_of_incompleted;
              this.no_contact = this.countData[0].no_of_contact;

            } else {

              this.dvrList = [];

              this.rerender();
              console.log(" this.dvrList", this.dvrList);


              $(document).ready(function () {
                $('.odd').show();

              });
              this.no_of_visit = 0;
              this.com_entry = 0;
              this.incom_entry = 0;
              this.no_contact = 0;
            }


          }

        });

      }
      else {
        console.log("only date");
        if (this.startDate != "" && this.endDate != "") {
          var strtDt = new Date(this.startDate);
          var endDt = new Date(this.endDate);
          var flag = 0; // false

          if (endDt <= strtDt) {
            flag = 1; // true
            this.openPopup();
            this.status_msg = "Warning";
            this.error_msg = "Please enter Dates correctly ,End date need to be bigger then start date.";
            this.dvrList = this.conformlist;
          } else {
            console.log("valid date");
            this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id).subscribe(response => {
              if (response.status == 'tokenfalse') {
                this.router.navigate(['/login'])
              } else {
                if (response.data[0].getdvrDataStatus == '0') {
                  this.countData = response.data[0]["countdata"];
                  this.dvrList = response.data[0]["dvrlist"];
                  this.rerender();
                  console.log(" this.dvrList", this.dvrList);
                  this.no_of_visit = this.countData[0].no_of_visit;
                  this.com_entry = this.countData[0].no_of_completed;
                  this.incom_entry = this.countData[0].no_of_incompleted;
                  this.no_contact = this.countData[0].no_of_contact;

                } else {

                  this.dvrList = [];

                  this.rerender();
                  console.log(" this.dvrList", this.dvrList);


                  $(document).ready(function () {
                    $('.odd').show();

                  });
                  this.no_of_visit = 0;
                  this.com_entry = 0;
                  this.incom_entry = 0;
                  this.no_contact = 0;
                }


              }

            });
          }
        } else {
          console.log("invalid date");
          this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?login_id=" + this.user_id).subscribe(response => {
            if (response.status == 'tokenfalse') {
              this.router.navigate(['/login'])
            } else {
              if (response.data[0].getdvrDataStatus == '0') {
                this.countData = response.data[0]["countdata"];
                this.dvrList = response.data[0]["dvrlist"];
                this.rerender();
                console.log(" this.dvrList", this.dvrList);
                this.no_of_visit = this.countData[0].no_of_visit;
                this.com_entry = this.countData[0].no_of_completed;
                this.incom_entry = this.countData[0].no_of_incompleted;
                this.no_contact = this.countData[0].no_of_contact;

              } else {

                this.dvrList = [];

                this.rerender();
                console.log(" this.dvrList", this.dvrList);


                $(document).ready(function () {
                  $('.odd').show();

                });
                this.no_of_visit = 0;
                this.com_entry = 0;
                this.incom_entry = 0;
                this.no_contact = 0;
              }


            }

          });
        }



      }






      // if (this.sale_person != undefined && searchfilter != undefined && this.startDate != undefined && this.endDate != undefined && this.customer != undefined) {
      //   console.log("in all filters");

      //   console.log("for sale person and date filters and customer filter");

      //   this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id + "&salePerson=" + this.sale_person + "&customer=" + this.customer.customer_name + "&customerid=" + this.customer.customer_id).subscribe(response => {
      //     if (response.status == 'tokenfalse') {
      //       this.router.navigate(['/login'])
      //     } else {

      //       console.log("in searchfilterd");
      //       if (response.data[0].getdvrDataStatus == '0') {
      //         console.log(" this.dvrList", this.dvrList);


      //         this.countData = response.data[0]["countdata"];

      //         this.dvrList = response.data[0]["dvrlist"];
      //         this.rerender();

      //         this.no_of_visit = this.countData[0].no_of_visit;
      //         this.com_entry = this.countData[0].no_of_completed;
      //         this.incom_entry = this.countData[0].no_of_incompleted;
      //         this.no_contact = this.countData[0].no_of_contact;

      //       } else {
      //         this.no_of_visit = 0;
      //         this.com_entry = 0;
      //         this.incom_entry = 0;
      //         this.no_contact = 0;
      //         $(document).ready(function () {
      //           $('.odd').show();

      //         });
      //       }


      //     }

      //   });

      //   // for three search filters
      // } else if (this.sale_person != 'undefined' && this.sale_person !=undefined && this.customer != undefined) {
      //   // for customer and sale_person
      //   console.log("for sale_person and customer");
      //   this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=undefineddate&login_id=" + this.user_id + "&customer=" + this.customer.customer_name + "&salePerson=" + this.sale_person + "&customerid=" + this.customer.customer_id).subscribe(response => {
      //     if (response.status == 'tokenfalse') {
      //       this.router.navigate(['/login'])
      //     } else {
      //       if (response.data[0].getdvrDataStatus == '0') {
      //         //  this.countData = response["countdata"];
      //         // this.dvrList = response["dvrlist"];
      //         this.countData = response.data[0]["countdata"];
      //         this.dvrList = response.data[0]["dvrlist"];
      //         this.rerender();
      //         console.log(" this.dvrList", this.dvrList);
      //         this.no_of_visit = this.countData[0].no_of_visit;
      //         this.com_entry = this.countData[0].no_of_completed;
      //         this.incom_entry = this.countData[0].no_of_incompleted;
      //         this.no_contact = this.countData[0].no_of_contact;

      //       } else {
      //         this.no_of_visit = 0;
      //         this.com_entry = 0;
      //         this.incom_entry = 0;
      //         this.no_contact = 0;
      //         $(document).ready(function () {
      //           $('.odd').show();

      //         });
      //       }


      //     }

      //   });

      // }else if(this.sale_person != 'undefined' && this.sale_person != undefined && this.startDate != undefined && this.endDate != undefined){
      //  //for sale_person and date
      //  console.log("for sale_person and date filter");
      //  this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id + "&salePerson=" + this.sale_person).subscribe(response => {
      //   if (response.status == 'tokenfalse') {
      //     this.router.navigate(['/login'])
      //   } else {

      //     console.log("in searchfilterd");
      //     if (response.data[0].getdvrDataStatus == '0') {
      //       console.log(" this.dvrList", this.dvrList);


      //       this.countData = response.data[0]["countdata"];

      //       this.dvrList = response.data[0]["dvrlist"];
      //       this.rerender();

      //       this.no_of_visit = this.countData[0].no_of_visit;
      //       this.com_entry = this.countData[0].no_of_completed;
      //       this.incom_entry = this.countData[0].no_of_incompleted;
      //       this.no_contact = this.countData[0].no_of_contact;

      //     } else {
      //       this.no_of_visit = 0;
      //       this.com_entry = 0;
      //       this.incom_entry = 0;
      //       this.no_contact = 0;
      //       $(document).ready(function () {
      //         $('.odd').show();

      //       });
      //     }


      //   }

      // });

      // }
      // else if (this.customer != undefined  && this.sale_person ==undefined && searchfilter != undefined && this.startDate != undefined && this.endDate != undefined) {
      //   //for customer and date filter 
      //   console.log("for customer and date filter")
      //   this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id + "&customer=" + this.customer.customer_name + "&customerid=" + this.customer.customer_id).subscribe(response => {
      //     if (response.status == 'tokenfalse') {
      //       this.router.navigate(['/login'])
      //     } else {

      //       console.log("in searchfilterd");
      //       if (response.data[0].getdvrDataStatus == '0') {
      //         console.log(" this.dvrList", this.dvrList);


      //         this.countData = response.data[0]["countdata"];

      //         this.dvrList = response.data[0]["dvrlist"];
      //         this.rerender();

      //         this.no_of_visit = this.countData[0].no_of_visit;
      //         this.com_entry = this.countData[0].no_of_completed;
      //         this.incom_entry = this.countData[0].no_of_incompleted;
      //         this.no_contact = this.countData[0].no_of_contact;

      //       } else {
      //         this.no_of_visit = 0;
      //         this.com_entry = 0;
      //         this.incom_entry = 0;
      //         this.no_contact = 0;
      //         $(document).ready(function () {
      //           $('.odd').show();

      //         });
      //       }


      //     }

      //   });
      // }
      // else if (this.sale_person != undefined && this.sale_person != 'undefined') {
      //   // only for sale_person
      //   console.log("only for sale_person")
      //   this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=undefineddate&login_id=" + this.user_id + "&salePerson=" + this.sale_person).subscribe(response => {
      //     if (response.status == 'tokenfalse') {
      //       this.router.navigate(['/login'])
      //     } else {
      //       if (response.data[0].getdvrDataStatus == '0') {
      //         //  this.countData = response["countdata"];
      //         // this.dvrList = response["dvrlist"];
      //         this.countData = response.data[0]["countdata"];
      //         this.dvrList = response.data[0]["dvrlist"];
      //         this.rerender();
      //         console.log(" this.dvrList", this.dvrList);
      //         this.no_of_visit = this.countData[0].no_of_visit;
      //         this.com_entry = this.countData[0].no_of_completed;
      //         this.incom_entry = this.countData[0].no_of_incompleted;
      //         this.no_contact = this.countData[0].no_of_contact;

      //       } else {
      //         this.no_of_visit = 0;
      //         this.com_entry = 0;
      //         this.incom_entry = 0;
      //         this.no_contact = 0;
      //         $(document).ready(function () {
      //           $('.odd').show();

      //         });
      //       }


      //     }

      //   });
      // }  else if (this.customer != undefined) {
      //   //for customer filter
      //   console.log("only for customer filter")
      //   this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=undefineddate&login_id=" + this.user_id + "&customer=" + this.customer.customer_name + "&customerid=" + this.customer.customer_id).subscribe(response => {
      //     if (response.status == 'tokenfalse') {
      //       this.router.navigate(['/login'])
      //     } else {
      //       if (response.data[0].getdvrDataStatus == '0') {
      //         console.log("response",response);
      //         //  this.countData = response["countdata"];
      //         // this.dvrList = response["dvrlist"];
      //         this.countData = response.data[0]["countdata"];
      //         this.dvrList = response.data[0]["dvrlist"];
      //         this.rerender();
      //         console.log(" this.dvrList", this.dvrList);
      //         this.no_of_visit = this.countData[0].no_of_visit;
      //         this.com_entry = this.countData[0].no_of_completed;
      //         this.incom_entry = this.countData[0].no_of_incompleted;
      //         this.no_contact = this.countData[0].no_of_contact;

      //       } else {
      //         console.log("in else part")
      //         $(document).ready(function () {
      //           $('.odd').show();

      //         });
      //         this.dvrList=[];
      //         this.no_of_visit = 0;
      //         this.com_entry = 0;
      //         this.incom_entry = 0;
      //         this.no_contact = 0;

      //       }


      //     }

      //   });
      // } 
      // else {
      //   //only for date filters
      //   console.log("in no filters");
      //   if (result != undefined && result1 != undefined) {
      //     console.log("valid date")
      //     this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?searchfiltertype=" + searchfilter + "&login_id=" + this.user_id).subscribe(response => {
      //       if (response.status == 'tokenfalse') {
      //         this.router.navigate(['/login'])
      //       } else {
      //         if (response.data[0].getdvrDataStatus == '0') {
      //           this.countData = response.data[0]["countdata"];
      //           this.dvrList = response.data[0]["dvrlist"];
      //           this.rerender();
      //           console.log(" this.dvrList", this.dvrList);
      //           this.no_of_visit = this.countData[0].no_of_visit;
      //           this.com_entry = this.countData[0].no_of_completed;
      //           this.incom_entry = this.countData[0].no_of_incompleted;
      //           this.no_contact = this.countData[0].no_of_contact;

      //         } else {
      //           this.no_of_visit = 0;
      //           this.com_entry = 0;
      //           this.incom_entry = 0;
      //           this.no_contact = 0;
      //           $(document).ready(function () {
      //             $('.odd').show();

      //           });
      //         }


      //       }

      //     });
      //   } else {
      //     console.log("invalid date");
      //     this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?login_id=" + this.user_id).subscribe(response => {
      //       if (response.status == 'tokenfalse') {
      //         this.router.navigate(['/login'])
      //       } else {
      //         if (response.data[0].getdvrDataStatus == '0') {
      //           this.countData = response.data[0]["countdata"];
      //           this.dvrList = response.data[0]["dvrlist"];
      //           this.rerender();
      //           console.log(" this.dvrList", this.dvrList);
      //           this.no_of_visit = this.countData[0].no_of_visit;
      //           this.com_entry = this.countData[0].no_of_completed;
      //           this.incom_entry = this.countData[0].no_of_incompleted;
      //           this.no_contact = this.countData[0].no_of_contact;

      //         } else {
      //           this.no_of_visit = 0;
      //           this.com_entry = 0;
      //           this.incom_entry = 0;
      //           this.no_contact = 0;
      //           $(document).ready(function () {
      //             $('.odd').show();

      //           });
      //         }


      //       }

      //     });
      //   }

      // }
    }



  }
  openPopup() {
    this.displayStyle = "block";
  }
  closePopup() {
    this.displayStyle = "none";


  }

  onClear() {
    this.add_css1 = "w3-red";
    this.add_css = "";
    this.add_css2 = "";
    this.dvrList = this.conformlist;
    this.startDate = "";
    this.endDate = "";
    this.sale_person = undefined;
    this.customer = undefined;

    this.commonservice.getAllData(this.commonservice.projectpath + "/getdashboarddetails?login_id=" + this.user_id).subscribe(response => {


      // console.log("status",response.status)
      if (response.status == 'tokenfalse') {
        console.log("uuuuuuuuuuuuuuuuu")
        this.router.navigate(['/login'])
      } else {
        console.log("uuuuuuuuuuuuu");
        console.log("respose", response);
        //   console.log("uuuuuuuuuuu",response.data[0]);
        console.log("uuuuuuuuuuu", response.data[0].getdvrDataStatus);

        if (response.data[0].getdvrDataStatus == '0') {

          //  console.log("iiiiiiiiiiiiiiiii",response);
          this.rerender();

          this.countData = response.data[0]["countdata"];
          //  console.log("this.countData",this.countData);
          this.dvrList = response.data[0]["dvrlist"];
          this.conformlist = this.dvrList;
          this.no_of_visit = this.countData[0].no_of_visit;
          this.com_entry = this.countData[0].no_of_completed;
          this.incom_entry = this.countData[0].no_of_incompleted;
          this.no_contact = this.countData[0].no_of_contact;

          console.log(" this.dvrList", this.dvrList);

        } else {
          this.no_of_visit = 0;
          this.com_entry = 0;
          this.incom_entry = 0;
          this.no_contact = 0;
          $(document).ready(function () {
            $('.odd').show();

          });
        }

      }




    });

  }

  salepersonDetails(sale_person){
    console.log(sale_person)
    if (sale_person) {
      this.commonservice.getAllData(this.commonservice.projectpath + "/mappingCustomerDetails?sale_person=" + sale_person).subscribe(response => {
        if (response.status == 'tokenfalse') {
          this.router.navigate(['/login'])
        } else {
          console.log("sale_person------------>",response);
          this.cust_data = response.data
          console.log("respocne",response.data)
        }
      });

    } else {
      console.log("sale person not selected yet");
    }
  }

}
