<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.icomsys.main_vm.db.mybatis.alias.LoginVO" %>
<%@ page import="com.icomsys.main_vm.biz.common.login.res.UserVo" %>


<script>
    let contextPath = '<c:url value="/" />'
    contextPath = contextPath == "/" ? '' : contextPath.substring(0, contextPath.length - 1);
    if (sessionStorage.contextPath == undefined) {
        sessionStorage.contextPath = contextPath
    }

    $(document).ajaxComplete(function (evnet, xhr, settings) {
        if (xhr.status == "500") {
            document.location.href = "<c:url value='/adm/main/egovLoginUsr.do'/>";
        }
        if (xhr.status == "401") {

            document.location.href = "<c:url value='/adm/main/egovLoginUsr.do'/>";
        }
    });


    $(document).ready(function () {
        getMainOpr();
        getUv();
        $('#mainOpr').change(function () {
            var json = {};
            json.serviceGroup = this.value;
            json.userSeq = $('#naviseq').val();
            getMainOprUpdate(json);
        });
        $('.user_name_btn').on('click', function () {
            $('.user_info').toggleClass('on');
        });
        $('#pwdChange').on('click', function () {
            // console.log("비밀번호변경");
            document.location.href = "<c:url value='/adm/main/changePwd'/>";
        });
        $('#logout').on('click', function () {
            // console.log("로그아웃");
            document.location.href = "<c:url value='/adm/main/egovLoginUsr.do'/>";
        });

    });

    function getUv() {
        var uv = $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "JSON",
            url: "<c:url value='/adm/main/uv'/>",
            success: function (data) {
                $('#naviseq').val(data.userSeq);
                return data;
            },
            error: function (){
                alert("NO SESSION");
                document.location.href = "<c:url value='/adm/main/egovLoginUsr.do'/>";
            }
        })
        return uv;

    }

    function getMainOpr() {
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "JSON",
            async: false,
            url: "<c:url value='/adm/menu/main/oprmng'/>",
            success: function (data) {
                // console.log(JSON.stringify("main ?  "+data))
                data.forEach(function (e, f) {
                    if (e.serviceGroup == '${UserVO.lastUseServiceGroup}') {
                        $('#mainOpr').append("<option value=" + e.serviceGroup + " selected>" + e.serviceGroupName + "</option>");
                    } else {
                        $('#mainOpr').append("<option value=" + e.serviceGroup + ">" + e.serviceGroupName + "</option>");
                    }
                });
                $('#mainOpr').niceSelect('update');
            },
            error: function () {
                window.location.href = "<c:url value='/adm/signin/signin'/>";
            }
        })
    };

    function getMainOprUpdate(data) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "JSON",
            url: "<c:url value='/adm/menu/main/oprmng/update'/>",
            data: JSON.stringify(data)
        }).complete(function (data) {
            // console.log(data.status);
            if (data.status == 200) {
                $('#mainOpr').niceSelect('update');
                window.location.href = "<c:url value='/adm/main/actionMain.do'/>";
            }
            if (data.status == 400) {
                // swalAlert('info', "이미 존재하는 코드입니다");
            }
        });
    };


    function getContextPath() {
        return sessionStorage.contextPath;
    }

    function checkPermissionPolicy(policyName) {
        let pTemp = '<c:out value="${sessionScope.PolicyList}" />';
        let sPolicyList = pTemp.substr(1, pTemp.length - 2).split(", ");

        return sPolicyList.indexOf(policyName) === -1 ? false : true;
    }

</script>

<style>
    /*#pdfdown{*/
    /*    background-image: url("/lib/assets/images/ico-menual-download.png");*/
    /*    background-repeat: no-repeat;*/

    /*}*/

    header .login_info .user_info ul li a.user_info_btn.menual:after {
        top: 12px;
        left: 7px;
        width: 22px;
        height: 20px;
        background: url("/lib/assets/images/ico-menual-download.png") center center / contain no-repeat;
        background-position: center center;
    }

</style>


<div class="header_wrap">
    <div class="logo">
        <a href="<c:url value='/admin/common/dashboard/manage.do'/>">
            <img src="<c:url value='/lib/assets/images/logo.png' />">
        </a>
    </div>

    <div class="global-select-ai sel_box">
        <div class="global-store-logo"><img class="ai-store-logo" src="<c:url value='/lib/assets/images/img-bot.png' />"
                                            alt=""></div>
        <div class="global-store-slt">
            <select class="wide" id="mainOpr">
                <%--				<option value="1" data-logo="assets/images/img-bot.png">우리가게 Ai</option>--%>
                <%--				<option value="2" data-logo="assets/images/img-counselor.jpg">너네가게 Ai</option>--%>
            </select>
        </div>
    </div>

    <div class="login_info">
        <div class="user_name">
            <input type="hidden" id="naviseq">
            <a class="user_name_btn" href="#">
                <em class="user_name_btn">${LoginVO.userName}</em> 님 반갑습니다.
            </a>
        </div>
        <div class="user_info">
            <ul class="logininfotitle">
                <li>
                    <a href="#" class="user_info_btn" id="pwdChange">비밀번호변경</a>
                </li>
                <li>
                    <a href="<c:url value='/adm/main/pdfdownload?name=Ourstore_Ai_Manager_User_Manual.pdf'/>" target="_blank"
                       class="user_info_btn menual" id="pdfdown">메뉴얼다운로드</a>
                </li>
                <li>
                    <a href="#" class="user_info_btn" id="logout">로그아웃</a>
                </li>

            </ul>
        </div>

    </div>


</div>
