adduser.component.ts 6.21 KB
import { Component, OnInit, AfterViewInit,ViewChild } from '@angular/core';
import { UserService } from '../UserEntity/user.service';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { User } from '../UserEntity/datamodel';
import { Http, Response } from '@angular/http';
import { GlobalService } from '../../Shared/global';
//import { DBOperation } from 'S';
import { Observable } from 'rxjs/Observable';
import { ConfirmService } from '../../Shared/Confirm/confirm.service';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import { LoadingService } from '../../shared/loading.service';

@Component({
  templateUrl:'./adduser.component.html' // '../../../../../wwwroot/html/UpdateProfile/updateuserprofile.component.html'
})

export class AddUser implements OnInit { 
  user: User;  
  baseUrl: string = "User";
  adduserFrm: FormGroup;
  useFname: string;
  error;
  status: boolean;
  alerts: string;
  emailPattern = "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$";
  public UserTypeList: any;
  public AccountNumberList: any;
  public ProductEditionList: any;
  modalTitle: string;
  //@ViewChild("profileModal")
  //profileModal: ModalComponent;
  //errorMessage: any;
  constructor(private _loadingService: LoadingService,private userservice: UserService, private router: Router, private fb: FormBuilder, private http: Http,
    private _confirmService: ConfirmService, private commonService: GlobalService 
  ) { }
 
  ngOnInit(): void {
    
    this.user = new User();
    this.alerts = '';
      //this.userservice.GetUserById(this.UserId);
    this.adduserFrm = this.fb.group({
      id: [''],
      UserName: ['', Validators.required],
      Password: ['', [Validators.required, Validators.minLength(8)]],
      ConfirmPassword: ['', Validators.required],
      FirstName: ['', Validators.required],
      LastName: ['', Validators.required],
      EmailId: ['', Validators.required],
      AccountNumberId: ['', Validators.required],
      UserTypeId: ['', Validators.required],
      ProductEditionId: ['', Validators.required]    
    });
    debugger
    this._loadingService.ShowLoading("global-loading");
    this.bindUsers();
    this.GetAccountNumber();
    this.GetUserTypeByLicenseId();
    this._loadingService.HideLoading("global-loading");
  }
  redirect() {
    this.router.navigate(['/']);
  }
  GetUserTypeByLicenseId() {
    debugger;
    var Accountnumber = this.adduserFrm.controls['AccountNumberId'].value;
    if (Accountnumber == "") { Accountnumber=0}
    this.userservice.GetUserTypeByLicenseType({     
      AccountNumberId: Accountnumber
    }).subscribe(x => { this.UserTypeList = x; }, error => this.error = <any>error);
  } 
  GetAccountNumber() {
    
    this.userservice.GetAccountNumber()
      .subscribe(x => { console.log(x); this.AccountNumberList=x }, error => this.error = <any>error);    
  }
  GetProductEdition() {
    this.userservice.GetProductEdition({
      AccountNumberId: this.adduserFrm.controls['AccountNumberId'].value})
      .subscribe(x => { console.log(x); this.ProductEditionList=x }, error => this.error = <any>error);
  }
  BindUserTypeAndProductEdition(deviceValue) {
    
    this.GetUserTypeByLicenseId();
    this.GetProductEdition();
  }
  public AddUser(this) {
    this.alerts = '';
    if (this.adduserFrm.value.UserName == '') {
      this.alerts += '<span>User Name is required.</span>';
    }
    if (this.adduserFrm.value.Password == '') {
      this.alerts += '</br><span>Password of minimum 8 characters is required.</span>';
    }
    if (this.adduserFrm.value.ConfirmPassword == '') {
      this.alerts += '</br><span>Confirm Password is required.</span>';
    }
    if (this.adduserFrm.value.EmailId == '') {
      this.alerts += '</br><span>Email Id is required.</span>';
    }
    if (this.adduserFrm.value.FirstName == '') {
      this.alerts += '</br><span>First Name is required.</span>';
    }
    if (this.adduserFrm.value.LastName == '') {
      this.alerts += '</br><span>Last Name is required.</span>';
    }
    if (this.adduserFrm.value.newPassword != this.adduserFrm.value.confirmPassword) {
      this.alerts += '</br><span>Password and confirm password must be same</span>';
    }
    if (this.adduserFrm.value.AccountNumberId == '0') {
      this.alerts += '</br><span>Please select account number</span>';
    }
    if (this.adduserFrm.value.UserTypeId == '0') {
      this.alerts += '</br><span>Please select user type</span>';
    }
    if (this.adduserFrm.value.ProductEditionId == '0') {
      this.alerts += '</br><span>Please select product edition</span>';
    }
    if (this.adduserFrm.valid && this.alerts == '') {
      var AddUserEntity = this.adduserFrm.value;
      return this.userservice.InsertUser(AddUserEntity)
        .subscribe(
        n => (this.AfterInsertData(n)),
        error => {
          this.error = <any>error;
          this.alerts = "<span>" + this.error + "</span>";
        });
    }
   
  }
  AfterInsertData(data) {
    //debugger;
    if (data == "User added successfully") {
      this.alerts = '';
      this._confirmService.activate("User added successfully.", "alertMsg");
    } 
    
  }
  ResetForm() {
    this._buildForm();
  }
  _buildForm() {
    this.adduserFrm = this.fb.group({
      id: [''],
      UserName: [""],
      Password: [""],
      ConfirmPassword: [""],
      FirstName: [""],
      LastName: [""],
      EmailId: [""],
      AccountNumberId: ["0"],
      UserTypeId: ["0"],
      ProductEditionId: ["0"] 
      
    });
  }
  bindUsers() {
    
    //console.log(data);
    //alert(JSON.stringify(data));
    //this.user = data[0];

       
    this.adduserFrm.controls['id'].setValue(0)
    this.adduserFrm.controls['FirstName'].setValue('')
    this.adduserFrm.controls['LastName'].setValue('')
    this.adduserFrm.controls['EmailId'].setValue('')
    this.adduserFrm.controls['UserName'].setValue('')
    this.adduserFrm.controls['Password'].setValue('')
    this.adduserFrm.controls['ConfirmPassword'].setValue('')
    this.adduserFrm.controls['AccountNumberId'].setValue(0)
    this.adduserFrm.controls['UserTypeId'].setValue(0)
    this.adduserFrm.controls['ProductEditionId'].setValue(0)
   
  }  
}