import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {CustomValidators} from 'ng2-validation';
import {FramePagerComponent} from '../../../core/pager/pager.component';
import {FrameRequest} from '../../../shared/frame/frame-request';
import {FrameFunction} from '../../../shared/frame/frame_function';
import {AppComponent} from '../../../app.component';

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

  viewStep = 'page';
  valForm: FormGroup;
  optr: any;
  roleArray = [];
  deptArray = [];
  orgArray = [];
  DELETE_URL = "frame/optr/delete.htm"; //删除
  RESET_URL = "frame/optr/reset.htm";//重置密码
  ENABLE_URL = "frame/optr/enable.htm"; //启用\禁用

  @ViewChild('frameOptr')
  frameOptr: FramePagerComponent;
  @ViewChild('elLoginName')
  elLoginName: ElementRef;

  constructor(fb: FormBuilder) {
    this.optr = {};
    const password = new FormControl('', Validators.compose([Validators.required, CustomValidators.rangeLength([6, 24])]));
    const certainPassword = new FormControl('', CustomValidators.equalTo(password));

    this.valForm = fb.group({
      'passwordGroup': fb.group({
        'login_name': [null, Validators.compose([Validators.required, CustomValidators.rangeLength([2, 16])])],
        'login_pass': password,
        'confirmPassword': certainPassword
      }),
      'optr_name': [null, Validators.compose([Validators.required, CustomValidators.rangeLength([2, 32])])],
      'optr_alias': [],
      'optr_sex': [null, null],
      'optr_status': [null, Validators.required],
      'optr_role': [],
      'optr_email': [null, Validators.email],
      'optr_mobile': [null, null],
      'optr_tel': [],
      'optr_addr': [],
      'optr_fax': [],
      'optr_post': [],
      'optr_dept': [null, Validators.required],
      'optr_desc': [],
      'dept_type': []
    });
  }

  ngOnInit() {
  }


  goViewStep(optr, step) {
    // 获取角色
    FrameRequest.requestMap('frame/optr/edit/initGx.htm',{ },(map) => {
      this.roleArray = map['RoleArray'];
      this.deptArray = map['DeptArray']
      FrameFunction.UntouchedForm(this.valForm);
      if(optr.optr_id === undefined) {
        this.optr = {'optr_status' : 'Active'};
      } else {
        FrameRequest.requestEntity('frame/optr/findGx.htm',{optr_id: optr.optr_id},(entity) => {
          this.optr = entity;
          this.optr.confirmPassword = this.optr.login_pass = '12345678';
          this.findDept(this.optr.dept_type);
        });
      }


      setTimeout(() => {
        this.viewStep = step;
        this.elLoginName.nativeElement.focus(); }, 100);
    });


  }


  submitForm() {
    if (!FrameFunction.validForm(this.valForm)) {
      return;
    }

    delete this.optr.resMap;
    FrameRequest.requestEntity('frame/optr/' + this.viewStep + '.htm', this.optr, () => {
      AppComponent.GobalComponent.successMsg('操作员信息保存成功');
      this.viewStep = 'page';
      this.frameOptr.reload();
    });
  }

  //通过类别查找部门
  findDept(org_type: any) {
    FrameRequest.requestArray('cf/cfOrganization/list.htm', {org_type: org_type}, (array) => {
      this.orgArray = array;
    });
  }

  //删除用户
  delete(optr?: any) {
    if(optr.optr_status === 'Active') {
      AppComponent.alert({title: '提示', text: '启用的用户不能删除！'});
      return ;
    }
    AppComponent.confirm("确定删除吗，删除后不可恢复！",()=>{
      if(optr.optr_id) {
        FrameRequest.request(this.DELETE_URL, {optr_id: optr.optr_id}, () => {
          AppComponent.GobalComponent.successMsg('删除成功'); 
          this.frameOptr.reload();
        });
      }
    });
    
  }
  //重置密码
  resetPassword(optr? : any) {
    AppComponent.confirm("确定重置密码吗，重置后不可恢复！",()=>{
      if(optr.optr_id) {
        FrameRequest.request(this.RESET_URL, {optr_id: optr.optr_id}, () => {
          AppComponent.GobalComponent.successMsg('密码重置成功'); 
        });
      }
    });
  }

  //启用和禁用
  enable(optr : any, flag: string) {
    let title =  flag === 'Active' ? '确定启用用户吗？' : '确定禁用用户吗？';
    let text =  flag === 'Active' ? '启用' : '禁用';
    AppComponent.confirm(title ,()=>{
      if(optr.optr_id) {
        FrameRequest.request(this.ENABLE_URL, {optr_id: optr.optr_id, optr_status: flag}, () => {
          AppComponent.GobalComponent.successMsg(text+'成功'); 
          this.frameOptr.reload();
        });
      }
    });
  }
}
