import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { securityFunctions } from 'src/app/core/common/security';
import { API } from 'src/app/core/services/api.service';

@Component({
  selector: 'app-user-view',
  templateUrl: './user-view.component.html',
})
export class UserViewComponent implements OnInit {

  cols: any[];
  user_name = '';
  user_email = '';
  vendor_id = null;
  AllExtranetUser;
  @ViewChild('dt') myTable;
  constructor(
    private router: Router,
    private ApiServ: API,
    public commonserv: CommonFunctionService
  ) { }
  ngOnInit(): void {
    this.commonserv.LoginUserData = securityFunctions.getSessionStorage('LoginUserData') ? JSON.parse(securityFunctions.getSessionStorage('LoginUserData')) : null
    this.cols = [
      { field: "user_id", header: "User Id" },
      { field: "first_name", header: "Name" },
      { field: "email", header: "Email Id" },
      { field: "vendor_name", header: "Vendor" },
      { field: "role_name", header: "User Role" },
      { field: "user_role", header: "User Type" },
      { field: "updated_by", header: "Updated By" },
      { field: "updated_on", header: "Updated On" },
      { field: "status", header: "Status" },
    ];
  };

  Search() {
    let searchObj = {
      first_name: this.user_name ? this.user_name : '',
      email: this.user_email ? this.user_email : '',
      extranet_vendor_id: this.commonserv.LoginUserData.extranet_vendor_id,
    };
    this.ApiServ.AdminGoPost('/ExtranetUserMaster/GetAll', searchObj).subscribe(res => {
      if (res) {
        this.myTable.first = 0;
        this.AllExtranetUser = res.Result;
        this.AllExtranetUser.forEach(e => {
          e['status'] = e['is_active'] ? 'Active' : 'Deactive'
          e['custom_updated_on'] = this.commonserv.UtcToLocaleTime(e.updated_on, "DD MMM YYYY, HH:mm:ss");
        });
      }
    })
  };

  UpdateStatus(id, status) {
    let obj = {
      Id: id,
      Status: status ? false : true,
    };
    this.ApiServ.AdminGoPost("/ExtranetUserMaster/UpdateStatus", obj).subscribe(
      (data) => {
        if (data) {
          this.commonserv.showMessage(data['Resp'], data['Message']);
          this.Search();
        }
      }
    );
  };

  Reset() {
    this.user_name = '';
    this.user_email = '';
    this.vendor_id = null;
  };

  edit(user_id) {
    this.router.navigate([`/UserConfiguration/UserManagement/Edit/${user_id}`]);
  };
  ddLoader = false
  timer;
  AllVendor
  ExtranetVendorMaster(val) {
    if (val.length >= 2) {
      let searchObj = {
        vendor_name: val,
      };
      this.ddLoader = true;
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.ApiServ.AdminPost('/ExtranetVendorMaster/GetAll', searchObj).subscribe(res => {
          if (res) {
            this.AllVendor = res.Result;
            this.ddLoader = false;
          }
        });
      }, 500);
    }
    if (!val) {
      this.AllVendor = [];
    }
  };

  btnClick() {
    this.router.navigateByUrl("/UserConfiguration/UserManagement/Add");
  }
}
