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

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

  dictType: any;
  dict: any;
  mode: any;
  detail: any;
  show: boolean = false;
  dictTypes: any;
  valForm: FormGroup;
  typeForm: FormGroup;
  @ViewChild('elDictText')
  elDictText: ElementRef;

  @ViewChild('frameDict')
  frameDict: FramePagerComponent;
  constructor(fb: FormBuilder) {
    this.valForm = fb.group({
      'dict_text': [null, Validators.required],
      'dict_value': [null, Validators.required],
      'dict_order': [null, Validators.compose([Validators.required, CustomValidators.number])]
    });
    this.typeForm = fb.group({
      'dict_name': [null, Validators.required],
      'modify_flag': [null, Validators.required],
      'frame_flag': [null, Validators.required],
      'dict_desc': []
    });
    this.mode = 'page';
    this.dictType = {};
    this.dict = {};
    this.dictTypes = {};
  }

  ngOnInit() {
  }

  listFrameDict(dict) {
    this.dictType = dict;
    this.detail = undefined;
    this.mode = 'list';
    this.frameDict.pageParams['dict_name']  = this.dictType.dict_name;
    this.frameDict.reload();
  }

  showDetail(dict, mode) {
    this.detail = mode;
    this.dict = dict;
    this.dict.dict_name = this.dictType.dict_name;
    setTimeout(() => {this.elDictText.nativeElement.focus(); }, 100);

    // this.renderer.invokeElementMethod(this.elDictText.nativeElement, 'focus');
  }

  showDictDetail(mode) {
    this.show = true;
  }



  submitForm(value: any) {
    for(let c in this.valForm.controls) {
      this.valForm.controls[c].markAsTouched();
    }
    if (!this.valForm.valid) {
      return;
    }

    FrameRequest.requestEntity('frame/dict/' + this.detail + '.htm', this.dict, () => {
      AppComponent.GobalComponent.successMsg('字典项保存成功');
      this.detail = undefined;
      this.frameDict.reload();
    });
  }

  submitTypeForm(value: any) {
    for(let c in this.typeForm.controls) {
      this.typeForm.controls[c].markAsTouched();
    }
    if (!this.typeForm.valid) {
      return;
    }

    FrameRequest.requestEntity('frame/dict/type/add.htm' , this.dictTypes, () => {
      AppComponent.GobalComponent.successMsg('字典类型保存成功');
      this.show = false;
      this.frameDict.reload();
    });
  }

  deleteDictType(key: any) {
    AppComponent.confirm("确定要删除该字典类型吗？此操作不可恢复！",()=>{
      FrameRequest.request('frame/dict/type/delete.htm', {key: key}, () => {
        AppComponent.GobalComponent.successMsg('字典类型删除成功！');
        this.frameDict.reload();
      });
    });
  }

  //刷新考生端字典缓存
  freshSesEnrolCache() {
    AppComponent.confirm("确定刷新考生端的字典缓存吗！",()=>{
      FrameRequest.request('std/stdRegInfo/freshCache.htm', {}, () => {
        AppComponent.GobalComponent.successMsg('刷新考生端字典缓存成功！');
        this.frameDict.reload();
      });
    });
  }


}
