import './style.scss';
import guid from './guid';
import BaseFormControl from '../../baseFormControl';
import { JDInputMaxTpl } from './tpl';
import StringUtil from '../../../../util/StringUtil';
class JDInput extends BaseFormControl {
    constructor(config) {
        super();
        this.uuid = guid();
        this.name = 'JDInput';
        
        this.typeClass = 'cd-text-input';
        
        this.iptId = this.name + this.uuid;
        
        this.$iptNode = null;
        
        this.setConfig(config);
        
        this.setInputType(config.type);
        this.render();
    }
    
    setInputType(type = 'text') {
        this.type = type;
        switch (this.type.toLocaleUpperCase()) {
            case 'TEXT':
                this.typeClass = 'cd-text-input';
                break;
            case 'SEARCH':
                this.typeClass = 'cd-text-search-input';
                break;
            default:
                break;
        }
    }
    
    setConfig(config) {
        this.config = $.extend(true, {}, {
            placeholder: config.domData && config.domData.placeholder || '请输入信息',
            domData: {},
            defaultValue: '',
            ...config.domData,
            change: () => {
            },
        }, config);
    }
    
    render() {
        let iptNode = template.render(JDInputMaxTpl, {
            typeClass: this.typeClass,
            value: this.config.defaultValue,
            maxlength: this.config.maxlength,
            placeholder: this.config.placeholder,
            char2Modal: this.config.char2Modal,
            iptId: this.iptId,
        });
        this.appendFormControl(iptNode);
        this.setData(this.config.defaultValue);
        this.addEvent();
    }
    
    addEvent() {
        this.$iptNode = this.element.find('#' + this.iptId);
        if (this.$iptNode) {
            this.$iptNode.on('keyup', (evt) => {
                let $input = $(evt.target);
                let value = $input.val();
                if (this.validateData(value)) {
                    value = this.checkMaxLength($input);
                    this.setData(value);
                    this.config.change(value);
                } else {
                    this.setData(null);
                }
            });
        }
    }
    
    checkMaxLength($input) {
        let value = $input.val();
        if (this.config.maxlength > 0 && this.config.char2Modal) {
            let temp = StringUtil.realSubstring(value, this.config.maxlength);
            if (temp != value) {
                value = temp;
                $input.val(value);
            }
        }
        return value;
    }
    
    validateData() {
        return true;
    }
    
    setDisable(flag) {
        if (this.$iptNode) {
            if (flag) {
                this.$iptNode.addClass('cd-disable');
                this.$iptNode.attr('disabled', true);
            } else {
                this.$iptNode.removeClass('cd-disable');
                this.$iptNode.removeAttr('disabled');
            }
        }
    }
    setData(value) {
        this.iptValue = value;
        this.$iptNode && this.$iptNode.val(this.iptValue);
    }
    
    getData() {
        let dataObj = {};
        if (this.iptValue !== null) {
            dataObj[this.config.domData.name] = this.iptValue;
        } else {
            return null;
        }
        return dataObj;
    }
}
export default JDInput;