import {Directive, ElementRef, Input, OnChanges, OnDestroy, OnInit, Renderer2} from '@angular/core';
import {FrameVariable} from '../../frame/frame-variable';

@Directive({
  selector: '[frameDict]'
})
export class FrameDictDirective implements OnInit {

  @Input() dictName: string;
  @Input() dictValue;
  @Input() tipFlag: boolean;
  @Input() tipMsg: string;
  @Input() showCode: string;


  dictValues: any;
  constructor(private renderer: Renderer2, public element: ElementRef) {
  }

  ngOnInit(): void {
    this.dictValues = FrameVariable.getFrameDictArray(this.dictName);
    if (this.dictValues === undefined ) {
      return ;
    }
    if (this.tipFlag !== false) {
      const defalut = this.renderer.createElement('option');
      defalut.value = '';
      if (this.tipMsg) {
        defalut.text = this.tipMsg;
      } else {
        defalut.text = '请选择';
      }
      this.renderer.appendChild(this.element.nativeElement, defalut);
    }
    for (const dict of this.dictValues) {
      const opt = this.renderer.createElement('option');
      opt.value = dict.dict_value;
      
      if(this.showCode){
        opt.text = `${dict.dict_value} | ${dict.dict_text}` ;
      }else{
        opt.text = dict.dict_text;
      }
      
      if(this.dictValue == dict.dict_value) opt['selected'] ="selected";
      this.renderer.appendChild(this.element.nativeElement, opt);
    }
  }


}
