import { Component, Input, Output, EventEmitter } from '@angular/core';
import { CodeList } from '@global/service/codelist';

@Component({
    selector: 'app-select',
    templateUrl: './app-select.html',
    styleUrls: ['./app-select.scss']
})
export class AppSelect {

    /**
     * 代码表
     */
    public items:any;

    constructor(private $codelist:CodeList){

    }

    /**
     * 编辑器值
     *
     * @type {string}
     * @memberof AppSelect
     */
    @Input()
    public value: string;
    @Output()
    public valueChange: EventEmitter<any> = new EventEmitter();

    /**
     * 是否启用
     *
     * @type {boolean}
     * @memberof AppSelect
     */
    @Input()
    disabled: boolean;

    /**
     * 提示信息
     *
     * @type {string}
     * @memberof AppSelect
     */
    @Input()
    placeholder: string;

    /**
     * 选中值
     *
     * @type {*}
     * @memberof AppSelect
     */
    @Input()
    set config(val:any){
        if(val){
            this.items =this.$codelist.getCodeItemByTag(val).items;
        }
    }

    /**
     * 选中值
     *
     * @memberof AppSelect
     */
    set selectValue(val: any) {
        this.valueChange.emit(val);
    }
    get selectValue() {
        return this.value;
    }

    /**
     * 获取选择文本值
     *
     * @param {*} detail
     * @memberof AppSelect
    */
    public getSelectText(items: any[], value): string {
        if (items && items.length > 0) {
            const element = items.find(item => Object.is(item.value, value+''));
            if (element) {
                return element.text;
            }
        }
    }

}