import {Directive, ElementRef, Input,  OnInit, Renderer2} from '@angular/core';
import {FrameService} from '../../../core/frame/frame.service';

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

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

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

  ngOnInit(): void {
    this.dictValues = FrameService.getFrameDictArray(this.dictName);
    if (this.dictValues === undefined ) {
      return ;
    }
    if (this.tipFlag !== false) {
      const def = this.renderer.createElement('option');
      def.value = '';
      if (this.tipMsg) {
        def.text = this.tipMsg;
      } else {
        def.text = '请选择';
      }
      this.renderer.appendChild(this.element.nativeElement, def);
    }
    for (const dict of this.dictValues) {
      const opt = this.renderer.createElement('option');
      opt.value = dict.dict_value;
      opt.text = dict.dict_text;
      this.renderer.appendChild(this.element.nativeElement, opt);
    }
  }



}
