import './style.scss'
import BaseFormControl from "../../baseFormControl";
import {JDBiItemTpl, JDBiListTpl, JDBiTpl} from "./tpl";
import JDLink from "../link";
import JDBiConfig from "../biConfig";
import JDCategorySelect from "../categorySelect";
class JDBiNavigation extends BaseFormControl {
    constructor(config) {
        super();
        this.biNavMaxPC = 10;
        this.biNavMaxM = 4;
        this.setConfig(config);
        this.render();
    }
    
    setConfig(config) {
        this.config = $.extend(true, {}, {
            domData: {}
        }, config);
    }
    render() {
        let $Node = template.render(JDBiListTpl, {});
        this.appendFormControl($Node);
        
        if (this.config.moduleData) {
            this.addBiNav(this.config.moduleData);
        }
        this.addEvent();
    }
    addEvent() {
        this.element.find('.J_addNavBtn').on('click', () => {
            this.addBiNav([]);
        });
        this.element.delegate('.J_biEdit', 'click', (evt) => {
            let $Node = $(evt.target);
            $Node.parents('.cd-bi-item-container').addClass('cd-show-detail');
        });
        this.element.delegate('.J_biRemove', 'click', (evt) => {
            let currentTabItem = $(evt.target).parents('.J_bi_nav_item');
            currentTabItem.remove();
            this.updateTabTextTitleNum();
            this.biNavToMax();
        });
        this.element.delegate('.J_biGoUp', 'click', (evt) => {
            let currentTabItem = $(evt.target).parents('.J_bi_nav_item'),
                beforeCurrentTabItem = currentTabItem.prev('.J_bi_nav_item');
            if (!beforeCurrentTabItem.length) {
                return;
            }
            beforeCurrentTabItem.before(currentTabItem);
            this.updateTabTextTitleNum();
            return false;
        });
        this.element.delegate('.J_biGoDown', 'click', (evt) => {
            let currentTabItem = $(evt.target).parents('.J_bi_nav_item'),
                afterCurrentTabItem = currentTabItem.next('.J_bi_nav_item');
            if (!afterCurrentTabItem.length) {
                return;
            }
            afterCurrentTabItem.after(currentTabItem);
            this.updateTabTextTitleNum();
            return false;
        });
        this.element.delegate('.J_done', 'click', (evt) => {
            let $Node = $(evt.target);
            $Node.parents('.J_bi_nav_item').removeClass('cd-show-detail');
            this.saveNavInfo($Node.parents('.J_bi_nav_item'));
            this.updateMainInfo($Node.parents('.J_bi_nav_item'));
            this.clearCategorySelect($Node.parents('.J_bi_nav_item'));
            return false;
        });
        this.element.delegate('.J_cancel', 'click', (evt) => {
            let $Node = $(evt.target);
            $Node.parents('.J_bi_nav_item').removeClass('cd-show-detail');
            this.cancelNavInfo($Node.parents('.J_bi_nav_item'));
            this.updateMainInfo($Node.parents('.J_bi_nav_item'));
            this.clearCategorySelect($Node.parents('.J_bi_nav_item'));
            return false;
        })
    }
    addBiNav(navModules) {
        let $BiNavItem = template.render(JDBiItemTpl, {
            moduleData: navModules
        });
        this.element.find('.J_bi_nav_list').append($BiNavItem);
        this.element.find('.J_bi_nav_item').each((i, el) => {
            let $Node = $(el);
            console.log(navModules[i]);
            this.renderNavLink($Node.find('.J_bi_link'), navModules[i] ? navModules[i] : null);
            this.renderNavBi($Node.find('.J_bi_category'), navModules[i] ? navModules[i] : null);
        });
        this.updateTabTextTitleNum();
        this.biNavToMax();
    }
    
    clearCategorySelect(Node) {
        Node.find('.J_cate').each((i, el) => {
            let $Node = $(el);
            if ($Node.data('categorySelectInstance') instanceof JDCategorySelect) {
                $Node.data('categorySelectInstance')._hide();
            }
        })
    }
    renderNavBi(node, moduleData) {
        if (node.data('biConfigInstance') instanceof JDBiConfig) {
            return;
        }
        let biConfigModuleData = null;
        if (moduleData) {
            biConfigModuleData = moduleData.biConfig;
        }
        console.log(biConfigModuleData);
        node.data('biConfigInstance', new JDBiConfig({
            domData: {
                name: 'biConfig',
                node: node
            },
            moduleData: biConfigModuleData
        }));
    }
    renderNavLink(node, moduleData) {
        if (node.data('linkInstance') instanceof JDLink) {
            return;
        }
        let _moduleData = {};
        if (moduleData) {
            _moduleData['title'] = moduleData.title;
            _moduleData['href'] = moduleData.href;
        }
        node.data('linkInstance', new JDLink({
            domData: {
                name: 'biLinkInfo',
                node: node
            },
            tab: this.config.tab || [],
            operation: false,
            addLink: false,
            moduleData: _moduleData
        }));
    }
    updateTabTextTitleNum() {
        let navNodes = this.element.find('.J_bi_nav_list .J_bi_nav_item');
        let navNum = navNodes.size();
        navNodes.each((index, el) => {
            let $Node = $(el);
            let navIndex = index + 1;
            $Node.find('.cd-nav-title').text('导航' + navIndex);
            if (navNum == 1) {
                $Node.find('.J_biGoUp').hide();
                $Node.find('.J_biGoDown').hide();
            } else {
                if (navIndex == 1) {
                    $Node.find('.J_biGoUp').hide();
                    $Node.find('.J_biGoDown').show();
                } else if (navIndex == navNum) {
                    $Node.find('.J_biGoUp').show();
                    $Node.find('.J_biGoDown').hide();
                } else {
                    $Node.find('.J_biGoUp').show();
                    $Node.find('.J_biGoDown').show();
                }
            }
        })
    }
    biNavToMax() {
        let maxNum = this.config.platForm === 'PC' ? this.biNavMaxPC : this.biNavMaxM;
        if (this.element.find('.J_bi_nav_list .J_bi_nav_item').size() >= maxNum) {
            this.element.find('.J_addNavBtn').hide();
        } else {
            this.element.find('.J_addNavBtn').removeAttr('style');
        }
    }
    
    saveNavInfo(target) {
        let $Node = target;
        $Node.find('.J_bi_link').data('linkInstance').saveInfoToDefInfo();
        $Node.find('.J_bi_category').data('biConfigInstance').saveInfoToDefInfo();
    }
    
    cancelNavInfo(target) {
        let $Node = target;
        $Node.find('.J_bi_link').data('linkInstance').resetDefInfoToInfo();
        $Node.find('.J_bi_category').data('biConfigInstance').resetDefInfoToInfo();
    }
    updateMainInfo(target) {
        let $Node = target;
        let linkInfo = $Node.find('.J_bi_link').data('linkInstance').getData();
        let BiConfigInfo = $Node.find('.J_bi_category').data('biConfigInstance').getData();
        if (linkInfo) {
            $Node.find('.J_show_name').html(linkInfo['biLinkInfo'].title);
            $Node.find('.J_show_moduleLinkUrl').html(linkInfo['biLinkInfo'].href.link);
        }
        if (BiConfigInfo) {
            let _catNames = [];
            BiConfigInfo.biConfig.biCategoryList.forEach(function (item) {
                let concatNames = '';
                if (item.firstName) {
                    concatNames += item.firstName;
                }
                if (item.secondName) {
                    concatNames += '/';
                    concatNames += item.secondName;
                }
                if (item.thirdName) {
                    concatNames += '/';
                    concatNames += item.thirdName;
                }
                _catNames.push(concatNames);
            });
            $Node.find('.J_show_moduleName').html('BI分类：' + _catNames.join(', '));
            $Node.find('.J_show_moduleNum').html('(' + _catNames.length + ')');
        }
    }
    getData() {
        let result = {};
        result[this.config.domData.name] = [];
        this.element.find('.J_bi_nav_item').each((i, el) => {
            let $linkNode = $(el).find('.J_bi_link');
            let $biConfig = $(el).find('.J_bi_category');
            let biNavData = {};
            if ($linkNode.data('linkInstance').getData()) {
                biNavData = $.extend(biNavData, {
                    ...$linkNode.data('linkInstance').getData().biLinkInfo
                });
            }
            biNavData = $.extend(biNavData, $biConfig.data('biConfigInstance').getData(), {
                id: i + 1
            });
            result[this.config.domData.name].push(biNavData);
        });
        return result;
    }
}
export default JDBiNavigation;