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';

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

  pricedata: any = [];
  pricelist: any = [];
  modelarr: any = [];
  loader: boolean = false;
  modelselected: any = '';
  dsename: any = '';
  dsecityvalue: any = '';
  smpinselected: any = '';
  smpinselectedname: any = '';
  tempvalueobj: any='';
  remarkoption: any = '';
  remarks: 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();
  }

  getpricelistdetails() {
    // let modelvalue=this.modelselected;
    this.pricedata = [];
    this.loader = true;
    this.server.get("dealer/userlist/0/0")
      .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/2")
    .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();
  }
  //==
  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 tempvalueorg = this.tempvalueobj;
        let smpinval = tempvalueorg.smpin;
        let currentstatusval = tempvalueorg.userstatus;
        let changestatusval: number;
        let statusdisplay = '';
        ///  alert("currentstatusval-->"+currentstatusval);
        if (currentstatusval == '1') {
          statusdisplay = tempvalueorg.smpin + "-" + tempvalueorg.fname + " " + tempvalueorg.lname + " has been deactivated successfully";
          changestatusval = 2;
        } else if (currentstatusval == '2') {
          statusdisplay = tempvalueorg.smpin + "-" + tempvalueorg.fname + " " + tempvalueorg.lname + " has been activated successfully";
          changestatusval = 1;
        } else {

        }

        let req = {
          smpin: smpinval,
          status: changestatusval,
          remark: this.remarks,
          remarkoption: this.remarkoption
        }
        //  console.log("req--->",req);
        this.server.post("dealer/dsestatusupdate", req).then((data) => {
          if (data.status == 200 && data.result.response != 'failed') {
            $('#remarksModal').modal('hide');
            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 = [];

          }
        });
      }
    }
  }

  changetoinactivestatus(tempvalue) {
    this.remarkoption='';
    this.remarks='';
    this.tempvalueobj = {};
    this.tempvalueobj = tempvalue;
    this.submittedvalue=false;
  }
  //==
  remarkschange() {

  }

}
