import CheckBox from '../../../checkBox';
import BaseFormControl from '../../baseFormControl';
import { JDCheckBoxTpl } from './tpl';
class JDCheckBox extends BaseFormControl {
    constructor(config) {
        super();
        this.name = 'JDCheckBox';
        
        this.checkBoxData = [];
        this.key = null;
        this.eldUIGroupRadio;
        this.setConfig(config);
        this.render();
    }
    setConfig(config) {
        this.config = $.extend(true, {}, {
            domData: {},
            defaultChecked: null,
            change: () => {
            },
        }, config);
        this.key = this.config.domData.name;
        this.checkBoxData = this.config.domData.formType.option;
    }
    render() {
        let checkBoxOpts = this.config.domData.formType.option;
        let eldUICheckBoxInstances = [];
        let $CheckBoxNode;
        this.appendFormControl(JDCheckBoxTpl);
        $CheckBoxNode = this.element.find('.J_JDCheckBox_content');
        if (checkBoxOpts.length && $CheckBoxNode.size()) {
            for (let i = 0, Ln = checkBoxOpts.length; i < Ln; i++) {
                let item = checkBoxOpts[i];
                this.getDefaultChecked(item);
                eldUICheckBoxInstances.push(new CheckBox({
                    node: $CheckBoxNode,
                    selected: item.checked,
                    disableInitialCallback: true,
                    label: item.text,
                    value: item.value || Math.random(),
                    onChange: (selected, value) => {
                        this.setSelected(selected, value);
                        
                        this.config.change(selected, value);
                    },
                }));
            }
        }
    }
    getDefaultChecked(item) {
        
        if (this.config.defaultChecked == null) {
            return;
        }
        
        if ($.isArray(this.config.defaultChecked) && this.config.defaultChecked.length === 0) {
            item.checked = false;
        }
        item.checked = this.config.defaultChecked.includes(item.value);
        return false;
    }
    setSelected(selected, value) {
        let i = 0;
        let ln = this.checkBoxData.length;
        for (; i < ln; i++) {
            if (this.checkBoxData[i].value === value) {
                if (selected) {
                    this.checkBoxData[i].checked = true;
                } else {
                    this.checkBoxData[i].checked = false;
                }
            }
        }
    }
    
    checkIn(value) {
        let i = 0;
        let Ln = this.checkBoxData.length;
        let obj = {
            index: 0,
            isIn: false,
        };
        for (; i < Ln; i++) {
            obj.index = i;
            if (this.checkBoxData[i][this.key] == value) {
                obj.isIn = true;
                return obj;
            } else {
                obj.isIn = false;
            }
        }
        return obj;
    }
    
    del(value) {
        let checkItem = this.checkIn(value);
        if (checkItem.isIn) {
            this.checkBoxData.splice(checkItem.index, 1);
        }
    }
    add(value) {
        let checkBox = {};
        if (this.checkIn(value).isIn) {
            return false;
        }
        checkBox[this.key] = value;
        this.checkBoxData.push(checkBox);
    }
    getData() {
        let obj = {};
        obj[this.key] = [];
        if (this.checkBoxData.length) {
            for (let i = 0, Ln = this.checkBoxData.length; i < Ln; i++) {
                if (this.checkBoxData[i].checked) {
                    obj[this.key].push(this.checkBoxData[i].value);
                }
            }
            return obj;
        } else {
            return null;
        }
    }
}
export default JDCheckBox;