"use strict";
import { util } from '../../util/util.js'
import {verify} from '../../util/verify.js'
import {icon} from './icon/icon.js'
import {config} from '../../config.js'
import {fullMap} from "../../fullMap.js";

class header {
    constructor(data) {
        this.eleID = util.getUUID();
        this.objID = this.eleID + 'div';
        this.showMenuStyleConfig1=config.getConfigJson().showMenuStyleConfig1;
        this.showMenuStyleConfig2=config.getConfigJson().showMenuStyleConfig2;
        this.showMenuStyleConfig3=config.getConfigJson().showMenuStyleConfig3;
        this.userName=data.userName;
        let toSecondmentUserShowFunc=function (){
            let isUserSecondment=util.getLocalStorageItem('isUserSecondment');
            if(isUserSecondment=='Y'){
                return true;
            }else{
                return false;
            }
        }
        this.userDropdownButtonData=[//这里为了统一命名是button实质上是a标签
            { button_id: 'toUserinfo', button_method: 'toUserinfo', button_title: '个人信息', button_class: 'dropdown-item',button_icon: 'ion ion-ios-person text-lightest', button_event: 'click', js_path: ''},
            { button_id: 'toSecondmentUser', button_method: 'toSecondmentUser', button_title: '切换身份', button_class: 'dropdown-item',button_icon: 'ion ion-ios-shuffle text-lightest', button_event: 'click', js_path: '',row_button_show_func:toSecondmentUserShowFunc},
            { button_id: 'rePassword', button_method: 'rePassword', button_title: '修改密码', button_class: 'dropdown-item',button_icon: 'ion ion-md-sync text-lightest', button_event: 'click', js_path: ''},
            //{ button_id: 'toMessage', button_method: 'toMessage', button_title: '我的消息(待开发)', button_class: 'dropdown-item',button_icon: 'ion ion-ios-mail text-lightest', button_event: 'click', js_path: ''},
            //{ button_id: 'toUserSettings', button_method: 'toUserSettings', button_title: '账号设置(待开发)', button_class: 'dropdown-item',button_icon: 'ion ion-md-settings text-lightest', button_event: 'click', js_path: ''},
            { button_id: 'logout', button_method: 'logout', button_title: '退出', button_class: 'dropdown-item',button_icon: 'ion ion-ios-log-out text-danger', button_event: 'click', js_path: ''}
        ];

    }
    render() {
        let that=this;
        return new Promise((resolve, reject) => {
            //右上角头像html模板
            let userDropdownDivHtmlTemplate=`
            <a class="nav-link dropdown-toggle text-reset d-block" style="padding:0;" href="#" data-toggle="dropdown">
            <span class="d-inline-flex flex-lg-row-reverse align-items-center align-middle">
            <div class="d-flex flex-column ml-lg-2 mr-2 mr-lg-0" style="min-width:60px;max-width:120px;height: 50px;">
                <span class="px-1 ml-2 ml-lg-0">${this.userName}</span>
                <span class="px-1 ml-2 ml-lg-0 text-truncate">${util.getLocalStorageItem('orgName')}</span>
            </div>
            <img src="${util.getLocalStorageItem('userPic')}" alt="" class="d-block ui-w-40 rounded-circle flex-shrink-0" style="height:40px;">
            </span>
            </a>
            <div class="dropdown-menu dropdown-menu-right" id="${that.objID}showUserDropdownButton"></div>
            `;
            document.getElementById('showUserDropdownDiv').innerHTML=userDropdownDivHtmlTemplate;
            document.getElementById(that.objID+'showUserDropdownButton').innerHTML=this.getUserDropdownButtonHtml();
            let thisIcon=new icon({data:'thisHeaderIcon'});
            thisIcon.render();
            fullMap.push('icon', thisIcon);
            //给图标添加事件隐藏和展示
            let menuRenderType=util.getLocalStorageItem('menuRenderType')
            if(menuRenderType=='1'){
                document.getElementById('headerIcon').addEventListener('click',()=> {
                    let htmlObj=document.querySelector('html');
                    let htmlClassName=htmlObj.className;
                    if(window.innerWidth>=991.11){
                        if(htmlClassName==this.showMenuStyleConfig1){
                            setTimeout(()=>{
                                htmlObj.className=this.showMenuStyleConfig2;
                            },500)
                        }else{
                            htmlObj.className=this.showMenuStyleConfig1;
                        }
                    }else{
                        htmlObj.className=this.showMenuStyleConfig3;
                    }

                })
            }
            for (let i = 0; i < that.userDropdownButtonData.length; i++) {
                let button=that.userDropdownButtonData[i];
                if(button.row_button_show_func){
                    if(!button.row_button_show_func()){
                        continue;
                    }
                }
                document.getElementById(that.objID+button.button_id).addEventListener(button.button_event,()=> {
                    let jsonConfig={objID:button.button_id,button_method:button.button_method}
                    if (util.isNull(button.js_path)) {
                        util.import2023('../component/header/headerAction.js',jsonConfig);
                    } else {
                        util.import2023(button.js_path,jsonConfig);
                    }
                })
            }
            resolve();
        });
    }
    menuShowByType(type){
        // type=1 全部展示(展示icon图标+菜单名称)
        // type=2 左侧菜单为简易(只展示icon图标)
        // type=3 都不展示
        if(type=='3'){
            document.getElementById('layout-navbar').style.display='none';
            document.getElementById('showMenuLayout').style.display='none';
            document.getElementById('showInputDiv').parentNode.style.paddingLeft='0px';
            document.getElementById('showInputDiv').parentNode.style.paddingRight='0px';
            document.getElementById('showInputDiv').parentNode.style.setProperty('padding-top', '0px', 'important');
        }
        if(window.innerWidth>=991.11){
            if(type=='1'){
                document.querySelector('html').className=this.showMenuStyleConfig1;
            }
            if(type=='2'){
                setTimeout(()=>{
                    document.querySelector('html').className=this.showMenuStyleConfig2;
                },500)
            }
        }else{
            if(type=='1'){
                document.querySelector('html').className=this.showMenuStyleConfig1;

            }
            if(type=='2'){
                setTimeout(()=>{
                    document.querySelector('html').className=this.showMenuStyleConfig3;
                },500)
            }
        }
    }
    //右上角头像下拉框HTML
    getUserDropdownButtonHtml(){
        let userDropdownButtonHtmlTemplate=``;
        for (let i = 0; i < this.userDropdownButtonData.length; i++) {
            let button=this.userDropdownButtonData[i];
            if(button.row_button_show_func){
                /*let myselfFuncStr=util.toOldChar(button.row_button_show_func);
                if (!util.executeDynamicCode(myselfFuncStr,{})){
                    continue;
                }*/
                if(!button.row_button_show_func()){
                    continue;
                }
            }
            userDropdownButtonHtmlTemplate+=`
            <span href="javascript:void(0)" id="${this.objID}${button.button_id}" class="${button.button_class}">
            <i class="${button.button_icon}"></i>
            &nbsp;${button.button_title}
            </span>`
        }
        return userDropdownButtonHtmlTemplate;
    }
}
export { header }
