import BaseFormControl from '../../baseFormControl';
import JDRadio from '../../formType/radio';
import JDSelect from '../../formType/select/select';
import JDSwitch from '../../formType/switch';
import JDInput from '../../formType/input';
import { tpl } from './tpl';
import './style.scss';
class ShopSearchGoods extends BaseFormControl {
    constructor(config) {
        super(config);
        this.config = $.extend(true, {}, {
            domData: {},
            moduleData: null,
        }, config);
        this.showType = this.config.moduleData && this.config.moduleData.showType ? this.config.moduleData.showType : 1;
        this.sortType = this.config.moduleData && this.config.moduleData.sortType ? this.config.moduleData.sortType : 1;
        this.defaultSearchSwitch = this.config.moduleData && this.config.moduleData.defaultSearchSwitch ? this.config.moduleData.defaultSearchSwitch : false;
        this.defaultSearchWord = this.config.moduleData && this.config.moduleData.defaultSearchWord ? this.config.moduleData.defaultSearchWord : '';
        this.render();
        this.renderShowType();
        this.renderSortType();
        this.renderDefaultSearchSwitch();
        this.renderDefaultSearchWord();
    }
    render() {
        this.appendFormControl(template.render(tpl));
    }
    renderShowType() {
        this.showTypeInstance = new JDRadio({
            domData: {
                name: 'showType',
                node: this.element.find('.J_showType'),
                formType: {
                    type: 'radio',
                    option: [{
                        text: 'SPU',
                        value: 1,
                    }, {
                        text: 'SKU',
                        value: 2,
                    }],
                },
            },
            defaultChecked: this.showType,
        });
    }
    renderSortType() {
        this.sortTypeInstance = new JDSelect({
            domData: {
                title: '默认排序',
                node: this.element.find('.J_sortType'),
                name: 'sortType',
                formType: {
                    option: [{
                        text: '相关度',
                        value: 1,
                    }, {
                        text: '上架时间',
                        value: 2,
                    }, {
                        text: '好评度',
                        value: 3,
                    }, {
                        text: '价格',
                        value: 4,
                    }, {
                        text: '销量',
                        value: 5,
                    }],
                },
            },
            defaultSelected: this.sortType,
        });
    }
    renderDefaultSearchSwitch() {
        this.defaultSearchSwitchInstance = new JDSwitch({
            domData: {
                name: 'defaultSearchSwitch',
                title: '店内搜索框',
                node: this.element.find('.J_defaultSearchSwitch'),
            },
            moduleData: this.defaultSearchSwitch,
            onStateChange: (state) => {
                state ?
                    this.defaultSearchWordInstance.element.show() :
                    this.defaultSearchWordInstance.element.hide();
            },
        });
    }
    renderDefaultSearchWord() {
        this.defaultSearchWordInstance = new JDInput({
            name: 'defaultSearchWord',
            domData: {
                name: 'defaultSearchWord',
                title: '默认搜索词',
                node: this.element.find('.J_defaultSearchWord'),
            },
            placeholder: '请输入默认搜索词',
            defaultValue: this.defaultSearchWord,
        });
        !!this.defaultSearchSwitchInstance.getData().defaultSearchSwitch ?
            this.defaultSearchWordInstance.element.show() :
            this.defaultSearchWordInstance.element.hide();
    }
    bindEvent() {
    }
    getData() {
        const data = {};
        const key = this.config.domData.name;
        data[key] = {
            ...this.showTypeInstance.getData(),
            ...this.sortTypeInstance.getData(),
            ...this.defaultSearchSwitchInstance.getData(),
            ...this.defaultSearchWordInstance.getData(),
        };
        return data;
    }
}
export default ShopSearchGoods;