import { Component, OnInit, ViewChild, ElementRef, NgModule } from '@angular/core';
import { HyperService } from '../../core/services/http.service';
import { Router, RouterModule, Routes, ActivatedRoute } from '@angular/router';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { DatePipe } from '@angular/common';
import * as moment from 'moment';
import { LocalStorage } from '../../core/services/local_storage.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { DatepickerOptions } from 'ng2-datepicker';
import * as enLocale from 'date-fns/locale/en';
import { ConfirmPopupComponent } from '../../sales_executive/confirm-popup/confirm-popup.component';
// declare var $: any;
import { Subscription } from "rxjs";
import { Observable } from "rxjs/Observable";
import { Location } from '@angular/common';
import { ToasterService } from '@service/toaster.service';
import $ from 'jquery';
declare var $: $;
import { CONFIG } from '../../config';
import { TaskstatusManagerComponent } from "../taskstatus-manager/taskstatus-manager.component";


@Component({
  selector: 'app-manager-dselist',
  templateUrl: './manager-dselist.component.html',
  styleUrls: ['./manager-dselist.component.scss']
})
export class ManagerDselistComponent implements OnInit {


  @ViewChild(TaskstatusManagerComponent) taskmodal: TaskstatusManagerComponent;

  remarkoption : any ='';
  pricedata: any = [];
  pricelist: any = [];
  modelarr: any = [];
  dealerlist: any = [];
  selecteddealer: any = '';
  loader: boolean = false;
  modelselected: any = '';
  dsename: any = '';
  dsecityvalue: any = '';
  smpinselected: any = '';
  smpinselectedname: any = '';
  remarks: any = '';
  tempvalueobj: any ='';
  submittedvalue:boolean=false;
  constructor(
    private server: HyperService,
    private router: Router,
    private _eref: ElementRef,
    private route: ActivatedRoute,
    private formBuilder: FormBuilder,
    private toaster: ToasterService
  ) {


  }

  ngOnInit() {
    this.smpinselected = LocalStorage.getValue('setdseselected');
    this.smpinselectedname = LocalStorage.getValue('setdseselectedname');
    //  console.log("setdseselected--->", this.smpinselected);

    //console.log("CONFIG.selecteddsename-->",CONFIG.selecteddsename)
    this.dsename = LocalStorage.getValue('selecteddsename');
    this.dsecityvalue = LocalStorage.getValue('selecteddsecity');

    // console.log("this.dsename-->",this.dsename);
    // console.log("this.dsecityvalue-->",this.dsecityvalue);
    this.getcommondetails();
    // this.getpricelistdetails();
    // this.taskmodal.relaodtaskstatus();
    this.getdealerlist();
  }

  getpricelistdetails() {
    // let modelvalue=this.modelselected;
    this.pricedata = [];
    this.loader = true;
    this.server.post("dealer/userlist", { dealer_code: this.selecteddealer })
      .then((data) => {
        this.loader = false;
        if (data.status == 200 && data.result.response != 'failed') {
          this.pricedata = data.result.data;
        } else if (data.result.status == 406) {
        }
        else if (data.result.status == 401 && data.result.response == 'failed') { }
      });
  }
  //===
  getcommondetails() {
    // this.loader = true;
    this.server.get("dealer/dseactivetinactivelist/1")
    .then((data) => {
      this.loader = false;
        if (data.status == 200 && data.result.response != 'failed') {
        this.modelarr=data.result.data;
        // console.log(this.modelarr)
        }else if (data.result.status == 406 ) {
              //alert(data.result.message)
        }
         else if (data.result.status == 401 && data.result.response == 'failed') {

         }
    });
  }
  selectmodel(temp) {
    this.getpricelistdetails();
  }
  //==
  changetoactivestatus(tempvalue) {
    this.remarkoption='';
    this.remarks='';
    this.tempvalueobj = {};
    this.tempvalueobj = tempvalue;
    this.submittedvalue=false;
  }

  changestatus() {

    if (this.remarkoption == '') {
      this.submittedvalue=false;
      this.toaster.show('Please select an option in remarks');
    } else {
      this.submittedvalue=false;
      if (this.remarkoption == '0' && this.remarks == '') {
        this.toaster.show('Please enter remarks');
      } else {
        this.submittedvalue=true;


        let tempvalue = this.tempvalueobj;
        console.log("tempvalue--->", tempvalue);
        let smpinval = tempvalue.smpin;
        let currentstatusval = tempvalue.userstatus;
        let changestatusval: number;
        let statusdisplay = '';
        ///  alert("currentstatusval-->"+currentstatusval);
        if (currentstatusval == '1') {
          statusdisplay = tempvalue.smpin + "-" + tempvalue.fname + " " + tempvalue.lname + " has been inactivated successfully";
          changestatusval = 2;
        } else if (currentstatusval == '2') {
          statusdisplay = tempvalue.smpin + "-" + tempvalue.fname + " " + tempvalue.lname + " has been activated successfully";
          changestatusval = 1;
        } else {

        }

        let req = {
          smpin: smpinval,
          status: changestatusval,
          dealer_code: this.selecteddealer,
          remark: this.remarks,
          remarkoption: this.remarkoption
        }
        //  console.log("req--->",req);
        this.server.post("dealer/dsestatusupdate", req).then((data) => {
          $('#remarksModal').modal('hide');
          if (data.status == 200 && data.result.response != 'failed') {
            this.getpricelistdetails();
            this.toaster.show(statusdisplay);
          } else if (data.result.status == 401 && data.result.response == 'failed') {
            // this.router.navigateByUrl('');
          } else if (data.result.status == 406 && data.result.response == 'failed') {
            let result = [];

          }
        });
      }
    }

  }
  //==
  
  remarkschange() {

  }
  //==
  getdealerlist() {
    //alert("tempvar--333->");



    this.loader = true;
    let spin = '';
    let req = {
      zmemail: "",
      rmemail: "",
      amemail: ""
    }
    this.server.post("managerenquiry/getdealerlist", req).then((data) => {
      this.loader = false;
      if (data.status == 200 && data.result.response != 'failed') {


        this.dealerlist = data.result.data;
        // this.dealerlist.unshift({name:'Select',value:''});


        // let dealerselected=LocalStorage.getValue("selecteddealervalue");
        // if ((dealerselected != '') && (dealerselected != undefined) && (dealerselected != null)) {
        //   this.selecteddealer=dealerselected;
        // }else{
        //   this.selecteddealer='';
        // }


        // this.getdselist('');


      } else if (data.result.status == 406) {
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {

      }
    });
  }



}

