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

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

  param: any;
  mode: any;
  valForm: FormGroup;
  @ViewChild('elParamName')
  elParamName: ElementRef;

  @ViewChild('frameParam')
  frameParam: FramePagerComponent;

  constructor(fb: FormBuilder) {
    this.valForm = fb.group({
      'param_name': [null, Validators.required],
      'param_value': [null, Validators.required],
      'param_desc': [null, Validators.required],
      'param_value_type': [null, Validators.required],
      'protect_status': [null, Validators.required],
      'project_owner': [null, null]
    });
    this.mode = 'page';
    this.param = {};
  }

  ngOnInit() {
  }


  showDetail(param, mode) {
    this.mode = mode;
    this.param = param;
    setTimeout(() => {
      for (const c in this.valForm.controls) {
        this.valForm.controls[c].markAsUntouched();
      };
      this.elParamName.nativeElement.focus();
     }, 100);

  }

  cancleDetail() {
    this.mode = 'page';
  }


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

    FrameRequest.requestEntity('frame/param/' + this.mode + 'Gx.htm', this.param, () => {
      AppComponent.GobalComponent.successMsg('字典项保存成功');
      this.mode = 'page';
      this.frameParam.reload();
    });
  }

   carouselContentFilter(str) {
    
        if(str){
            var carContent ='';
            if(str.length >= 20){
               
                carContent = str.substring(0,20) + '...';
            }
            else {
                carContent = str
            }
            return carContent;
        }else{
          return str;
        }
  }

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