import './style.scss'
import BaseFormControl from '../../baseFormControl'
import { JDBiCatItemTpl, JDBiCatListTpl } from './tpl'
import JDCategorySelect from '../categorySelect'
class JDBiConfig extends BaseFormControl {
    constructor(config) {
        super()
        this.maxAddCat = 3
        this.setConfig(config)
        this.render()
    }
    
    setConfig(config) {
        this.config = $.extend(true, {}, {
            domData: {},
            baseZIndex: 9,
            moduleData: null
        }, config)
    }
    render() {
        let $Node = template.render(JDBiCatListTpl, {
            maxAddCat: this.maxAddCat
        })
        this.appendFormControl($Node)
        if (this.config.moduleData) {
            console.log(this.config.moduleData.biCategoryList)
            this.addCatItem(this.config.moduleData.biCategoryList)
            this.updateCatNames(this.config.moduleData.biCategoryList)
        }
        this.addEvent()
    }
    addEvent() {
        this.element.find('.J_addCat').on('click', () => {
            this.addCatItem([])
            this.hideCategorySelect()
        })
        this.element.delegate('.J_cate', 'click', (evt) => {
            let $Node = $(evt.target)
            if (!($Node.data('categorySelectInstance') instanceof JDCategorySelect)) {
                $Node.data('categorySelectInstance', new JDCategorySelect({
                    node: $Node,
                    baseZIndex: this.config.baseZIndex,
                    scrollNode: '.J_editorInstanceArea,.J_mainContent,.J_content-middle,body'
                }))
            }
            $Node.data('categorySelectInstance').show()
            this.hideCategorySelect($Node)
            return false
        })
        this.element.delegate('.J_cate_del', 'click', (evt) => {
            let $Node = $(evt.target)
            this.hideCategorySelect()
            $Node.parents('.J_cat_item').remove()
            this.updateAddCatStatus()
            return false
        })
    }
    
    resetDefInfoToInfo() {
        this.element.find('.J_bi_cat_list .J_cate').each((i, el) => {
            let $Node = $(el)
            if ($Node.data('defCatInfo')) {
                $Node.data('catInfo', $Node.data('defCatInfo'))
                this.setCatInfo($Node, $Node.data('catInfo'))
            } else {
                $Node.removeData('catInfo')
                this.setCatInfo($Node, $Node.data('catInfo'))
            }
        })
    }
    
    saveInfoToDefInfo() {
        this.element.find('.J_bi_cat_list .J_cate').each((i, el) => {
            let $Node = $(el)
            let catInfo = $Node.data('catInfo')
            if ($.isArray(catInfo)) {
                let defCatInfo = [].concat(catInfo)
                $Node.data('defCatInfo', defCatInfo)
            } else {
                $Node.data('defCatInfo', catInfo)
            }
        })
    }
    
    addCatItem(moduleData) {
        if (this.element.find('.J_cat_item').size() >= this.maxAddCat) {
            return
        }
        let $NodeItem = template.render(JDBiCatItemTpl, {
            moduleData: moduleData
        })
        this.element.find('.J_bi_cat_list').append($NodeItem)
        this.updateAddCatStatus()
    }
    
    hideCategorySelect(node) {
        let $CatItemNodes = this.element.find('.J_cat_item')
        $CatItemNodes.each((i, el) => {
            let $Node = $(el)
            if (node && node.size()) {
                if ($Node.find('.J_cate')[0] === node[0]) {
                    return
                }
            }
            let catInstance = $Node.find('.J_cate').data('categorySelectInstance')
            if (catInstance) {
                catInstance._hide()
            }
        })
    }
    
    updateAddCatStatus() {
        if (this.element.find('.J_cat_item').size() >= this.maxAddCat) {
            this.element.find('.J_addCat').addClass('disabled')
        } else {
            this.element.find('.J_addCat').removeClass('disabled')
        }
    }
    
    setCatInfo(node, moduleData) {
        let catNames = []
        if (!moduleData) {
            node.html('')
            return
        }
        if ($.isArray(moduleData) && moduleData.length) {
            moduleData.forEach((item) => {
                catNames.push(item.catName)
            })
        } else {
            moduleData.firstName && catNames.push(moduleData.firstName)
            moduleData.secondName && catNames.push(moduleData.secondName)
            moduleData.thirdName && catNames.push(moduleData.thirdName)
        }
        node.html(catNames.join('/'))
    }
    updateCatNames(moduleData) {
        if (!moduleData) {
            return
        }
        this.element.find('.J_bi_cat_list .J_cate').each((i, el) => {
            let $Node = $(el)
            if (moduleData) {
                let catItem = moduleData[i]
                let catNames = []
                catItem.firstName && catNames.push(catItem.firstName)
                catItem.secondName && catNames.push(catItem.secondName)
                catItem.thirdName && catNames.push(catItem.thirdName)
                $Node.data('catInfo', catItem)
                $Node.data('defCatInfo', catItem)
                $Node.html(catNames.join('/'))
            }
        })
    }
    getData() {
        let result = {}
        let $CatNodes = this.element.find('.J_cat_item .J_cate')
        if ($CatNodes.size() == 0) {
            return null
        }
        result[this.config.domData.name] = {
            biCategoryList: []
        }
        $CatNodes.each((i, el) => {
            let catItem = {}
            let $Node = $(el)
            let catInfo = $Node.data('catInfo')
            if ($.isArray(catInfo) && catInfo.length) {
                catInfo.forEach((item, i) => {
                    switch (i) {
                        case 0:
                            catItem['firstCateId'] = item.catId
                            catItem['firstName'] = item.catName
                            break
                        case 1:
                            catItem['secondCateId'] = item.catId
                            catItem['secondName'] = item.catName
                            break
                        case 2:
                            catItem['thirdCateId'] = item.catId
                            catItem['thirdName'] = item.catName
                            break
                    }
                })
                result[this.config.domData.name].biCategoryList.push(catItem)
            } else if (!$.isEmptyObject(catInfo)) {
                result[this.config.domData.name].biCategoryList.push(catInfo)
            }
        })
        return result
    }
}
export default JDBiConfig