import TPL from './tpl';
import './style.scss';
import BaseFormControl from "../../baseFormControl";
import JDSwitch from "../../formType/switch";
import JDSelect from "../../formType/select/select";
import JDRadio from "../../formType/radio";
class JDShopAdvanceSearch extends BaseFormControl{
    constructor(config){
        super();
        this.config = $.extend(true, {}, {
            domData: {},
            moduleData: null,
        }, config);
        this.inStockDisp = this.config.moduleData && this.config.moduleData.inStockDisp == 1 ? true : false;
        this.pageSize   = this.config.moduleData && this.config.moduleData.pageSize ? this.config.moduleData.pageSize : 60;
        this.eachLineNum = this.config.moduleData && this.config.moduleData.eachLineNum ? this.config.moduleData.eachLineNum : 5;
        this.hotSearchWords = this.config.moduleData && this.config.moduleData.hotSearchWords ? this.config.moduleData.hotSearchWords : [];
        this.render();
        this.renderShowType();
        this.renderPageSize();
        this.renderEachLineNum();
    }
    
    
    render(){
        let node = template.render(TPL, {
            hotSearchWords: this.hotSearchWords.join(','),
        });
        this.appendFormControl(node);
    }
    renderShowType(){
        this.showInstance = new JDSwitch({
            domData: {
                name: 'inStockDisp',
                title: '仅显示有货',
                node: this.element.find('.J_inStockDisplay'),
            },
            moduleData:this.inStockDisp
        })
    }
    renderPageSize(){
        this.pageSizeInstance = new JDSelect({
            domData: {
                title: '每页商品数',
                node: this.element.find('.J_pageSize'),
                name: 'pageSize',
                formType: {
                    option: [{
                        text: '60个',
                        value: 60,
                    }, {
                        text: '80个',
                        value: 80,
                    }, {
                        text: '100个',
                        value: 100,
                    }],
                },
            },
            defaultSelected: this.pageSize,
        })
    }
    renderEachLineNum(){
        this.eachLineNumInstance = new JDRadio({
            domData: {
                name: 'eachLineNum',
                node:this.element.find('.J_eachLineNum'),
                title: '每行显示商品数',
                validate: {
                    must: true
                },
                formType:{
                    option:[
                        {text:"5个商品", value:5},
                        {text:"4个商品",value:4}
                    ]
                }
            },
            defaultChecked: this.eachLineNum,
        })
    }
    
    getData() {
        this.hotSearchWords = this.element.find('.J_hotSearchWordsArray').val();
        const data = {};
        const key = this.config.domData.name;
        data[key] = {
            sortType:5,
            ...this.showInstance.getData(),
            ...this.pageSizeInstance.getData(),
            ...this.eachLineNumInstance.getData(),
        };
        return data;
    }
}
export default JDShopAdvanceSearch;