import BaseFormControl from '../../baseFormControl';
import {selectTpl} from './selectTpl';
class JDSelect extends BaseFormControl {
    constructor(config) {
        super();
        let _this = this;
        _this.config = _this._getConfig(config);
        _this.render();
    }
    
    _getConfig(config) {
        return $.extend(true, {}, {
            domData: {},
            singleAutoHide: false,      
            defaultSelected: ''
        }, config);
    }
    
    render() {
        let _this = this;
        let dom = template.render(selectTpl, {
            options: _this.config.domData.formType.option,
            value: _this.config.defaultSelected
        });
        if(_this.config.singleAutoHide && _this.config.domData.formType.option.length <= 1){
            this.element.hide();
        }
        this.appendFormControl(dom);
        this.element.find('select').simSelect();
        this.element.find('select').on("change", function () {
            let value = $(this).val();
            if(_this.config && _this.config.onChange){
                _this.config.onChange(value);
            }
        });
    }
    
    getData() {
        let result = {};
        result[this.config.domData.name] = this.element.find('select').val();
        return result;
    }
}
export default JDSelect;