import { Component, OnInit, Inject } from '@angular/core';
import { MD_DIALOG_DATA, MdDialog, MdDialogRef } from '@angular/material';
import { ContactService } from '../../contact.service';
import { ResetPasswordComponent } from '../../add/reset-password/reset-password.component';

@Component({
  selector: 'app-dealer-permission',
  templateUrl: './dealer-permission.component.html',
  styleUrls: ['./dealer-permission.component.scss']
})
export class DealerPermissionComponent implements OnInit {
  DeviceSetting = { Add: true, Edit: true, Delete: false }
  UserSetting = { Add: true, Edit: true, Delete: false }
  DeviceOptions = [{ name: "Add", value: true }, { name: "Edit", value: true }, { name: "Delete", value: false }];
  UserOptions = [{ name: "Add", value: true }, { name: "Edit", value: true }, { name: "Delete", value: false }]
  constructor(private contactService: ContactService,public dialog: MdDialog,public dialogRef: MdDialogRef<ResetPasswordComponent>, @Inject(MD_DIALOG_DATA) public data: any) { 
 

  }

  dev_permission:boolean = true;
  cust_permission : boolean = true;
  ngOnInit() {
    console.log(this.data);
    console.log(this.data.device_add_permission);
    console.log(this.data.cust_add_permission);
    if (this.data.devicePermission) {
      this.DeviceSetting = this.data.devicePermission;
      for (let i = 0; i < this.DeviceOptions.length; i++) {
        // if(this.DeviceOptions[i].name==this.data.DevicePermission[this.DeviceOptions[i].name]){
        this.DeviceOptions[i].value = this.data.devicePermission[this.DeviceOptions[i].name]
        // }
      }
    }
    if (this.data.userPermission) {
      this.UserSetting = this.data.userPermission;
      for (let i = 0; i < this.UserOptions.length; i++) {
        // if(this.UserOptions[i].name==this.data.DevicePermission[this.UserOptions[i].name]){
        this.UserOptions[i].value = this.data.userPermission[this.UserOptions[i].name]
        // }
      }
    }
    if(this.data.device_add_permission != undefined){
      this.dev_permission = this.data.device_add_permission ; 
      console.log(this.dev_permission);
    }
    if(this.data.cust_add_permission != undefined){
        this.cust_permission = this.data.cust_add_permission ; 
        console.log(this.cust_permission);
    }  
  }

save(){
  console.log('dev_permission',this.dev_permission);
  console.log('cust_permission',this.cust_permission);
  let uid = this.data._id ; 
  var permission_payLoad = {
    device_add_permission : this.dev_permission,
    cust_add_permission : this.cust_permission,
    devicePermission: this.DeviceSetting,
    userPermission: this.UserSetting,
    uid : uid
  }

  this.contactService.setDealerPermission(permission_payLoad).subscribe(res=>{
    console.log(res);
    this.dialogRef.close('succ');
  },err=>{
    console.log(err);
  })
  
}
cancel(){
  this.dialogRef.close('close'); 

}


  updateCheckedOptions(value, e, type) {
    console.log(value, e);
    if (type == 'user') {
      for (var i = 0; i < this.UserOptions.length; i++) {
        if (this.UserOptions[i].name == value) {
          this.UserOptions[i].value = e;
          this.UserSetting[value] = e
          console.log(this.UserOptions, this.UserSetting)
        }
      }


    } else {
      for (var i = 0; i < this.DeviceOptions.length; i++) {
        if (this.DeviceOptions[i].name == value) {
          this.DeviceOptions[i].value = e;
          this.DeviceSetting[value] = e;
          console.log(this.DeviceOptions, this.DeviceSetting)
        }
      }
    }

  }
}
