<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../../common.jsp"/>
    <style>
        .layui-input, .layui-textarea {
            border-color: #606060;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" style="width: 80%; margin-top: 20px">
    <input name="id" value="${entity.id}" style="display: none"/>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单名称</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block">
                <input name="name" value="${entity.name}" placeholder="请输入菜单名称" lay-verify="required"
                       class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单code</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block">
                <input name="code" value="${entity.code}" placeholder="请输入菜单code" lay-verify="required"
                       lay-reqText="请输入菜单名称" maxlength="20" class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block">
                <c:choose>
                    <c:when test="${entity.sort == '' or entity.sort == null}">
                        <input name="sort" value="1" placeholder="排序" class="layui-input"/>
                    </c:when>
                    <c:otherwise>
                        <input name="sort" value="${entity.sort}" placeholder="排序" class="layui-input"/>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>
    </div>

    <input name="icon" value="${entity.icon}" style="display: none" lay-verify="required" lay-reqText="请选择图标"/>
    <div class="layui-form-item">
        <label class="layui-form-label">图标</label>
        <div class="layui-input-block">
            <button id="uploadIcon" type="button" class="layui-btn">选择图标</button>
        </div>
    </div>

    <div id="iconDiv" class="layui-form-item" style="display: ${empty entity.icon ? 'none' : 'black'}">
        <div class="layui-input-block">
            <img id="iconImg" src="${entity.icon}" alt="" height="50px" width="50px"/>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">导航选择</label>
            <div class="layui-input-inline">
                <select id="menuNavSelect" name="navCode" lay-verify="required">
                    <option value="">请选择导航</option>
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">分组选择</label>
            <div class="layui-input-inline">
                <select id="menuGroupSelect" name="menuGroup" lay-verify="required">
                    <option value="">请选择分组</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-inline">
            <select id="visible" name="visible" lay-verify="required">
                <c:choose>
                    <c:when test="${not empty entity}">
                        <option value="1" ${entity.visible == '1' ? 'selected' : ''}>是</option>
                        <option value="0" ${entity.visible == '0' ? 'selected' : ''}>否</option>
                    </c:when>
                    <c:otherwise>
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </c:otherwise>
                </c:choose>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单类型</label>
        <div class="layui-input-inline">
            <select id="menuType" name="menuType" lay-verify="required" lay-filter="menuType">
                <option value="0" ${entity.menuType == '0' ? 'selected' : ''}>原生菜单</option>
                <option value="1" ${entity.menuType == '1' ? 'selected' : ''}>H5网页</option>
                <option value="2" ${entity.menuType == '2' ? 'selected' : ''}>小程序</option>
                <option value="3" ${entity.menuType == '3' ? 'selected' : ''}>uniapp</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">安卓显示</label>
        <div class="layui-input-inline">
            <select id="isAndroidEnabled" name="isAndroidEnabled">
                <option value="1" ${entity.isAndroidEnabled == '1' || empty entity ? 'selected' : ''}>是</option>
                <option value="0" ${entity.isAndroidEnabled == '0' ? 'selected' : ''}>否</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">IOS显示</label>
        <div class="layui-input-inline">
            <select id="isIphoneEnabled" name="isIphoneEnabled">
                <option value="1" ${entity.isIphoneEnabled == '1' || empty entity ? 'selected' : ''}>是</option>
                <option value="0" ${entity.isIphoneEnabled == '0' ? 'selected' : ''}>否</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item isApplets" style="display: ${entity.menuType == "2" ? 'black' : 'none'}">
        <label class="layui-form-label">小程序ID</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block">
                <input name="userName" id="userName" value="${entity.userName}" placeholder="请输入小程序ID"
                       lay-reqText="请输入小程序ID" maxlength="50" class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item isApplets isUniapp"
         style="display: ${(entity.menuType == "2" || entity.menuType == "3") ? 'black' : 'none'}">
        <label class="layui-form-label">小程序/uniapp路径</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block">
                <input name="path" id="path" value="${entity.path}" placeholder="请输入小程序/uniapp路径" lay-reqText="请输入小程序/uniapp路径"
                       maxlength="500" class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item isHtml" style="display: ${entity.menuType == "1" ? 'black' : 'none'}">
        <label class="layui-form-label">菜单url</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block">
                <input name="url" value="${entity.url}" placeholder="请输入菜单url(原生菜单不填)" class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="button" id="close" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>

    <div id="uploadLoadingDiv" style="display: none;">
        <div class="layui-progress" lay-showpercent="true" lay-filter="js_upload_progress"
             style="margin: 10px;margin-top: 20px;">
            <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
        </div>
    </div>
</form>

<form id="ajaxForm" class="layui-form" style="width: 70%">
    <input type="hidden" name="OSSAccessKeyId" id="accessid" value="${uploadParam.accessid}">
    <input type="hidden" name="policy" id="policy" value="${uploadParam.policy}">
    <input type="hidden" name="signature" id="signature" value="${uploadParam.signature}">
    <input type="hidden" name="expire" id="expire" value="${uploadParam.expire}">
    <input type="hidden" name="success_action_status" id="success_action_status" value="200">
    <input type="hidden" name="key" id="key" value="${uploadParam.key}">
    <input type="hidden" name="callBack" id="key2_callBack" value="${uploadParam.callBack}">
    <input type="file" style="display: none" name="file" id="file"
           onchange="document.getElementById('fileName').value=this.files[0].name">
</form>

<script>
    var layer;
    var form;
    var upload;
    var element;

    //Demo
    layui.use(['form', 'layer', 'element', 'upload'], function () {
        form = layui.form;
        layer = layui.layer;
        upload = layui.upload;
        element = layui.element;

        var layerIndex = parent.layer.getFrameIndex(window.name);

        $('#close').click(function () {
            parent.layer.closeAll();
        });

        //监听select
        form.on('select(menuType)', function (data) {
            switch (data.value) {
                case "1":
                    $(".isApplets").hide();
                    $(".isUniapp").hide();
                    $(".isHtml").show();
                    break;
                case "2":
                    $(".isHtml").hide();
                    $(".isUniapp").hide();
                    $(".isApplets").show();
                    $("#userName").attr("lay-verify", "required");
                    $("#userName").val("gh_30406fa9c666");
                    break;
                case "3":
                    $(".isHtml").hide();
                    $(".isApplets").hide();
                    $(".isUniapp").show();
                    break;
            }

            form.render('select');//select是固定写法 不是选择器
        });

        //监听提交
        form.on('submit(formDemo)', function (data) {
            var index = layer.load(1, {shade: [0.1, '#000']});
            $.ajax({
                url: '${ctx}/appMenu/save',
                type: 'post',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: "application/json",
                success: function (res) {
                    layer.close(index);
                    if (res.code == 200) {
                        parent.layer.msg(res.message);
                        parent.layer.closeAll();
                        parent.table.reload('idTest', {//testReload是组件名称,表格名
                            page: {//page参数是指重新刷新后从第几页开始
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                    } else {
                        layer.msg(res.message, {
                            icon: 5
                        });
                    }
                },
                error: function (res) {
                    layer.msg("操作失败", {
                        icon: 5
                    });
                }
            })
            return false;
        });

        //创建监听函数
        var xhrOnProgress = function (fun) {
            xhrOnProgress.onprogress = fun; //绑定监听
            //使用闭包实现监听绑
            return function () {
                //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
                var xhr = $.ajaxSettings.xhr();
                //判断监听函数是否为函数
                if (typeof xhrOnProgress.onprogress !== 'function')
                    return xhr;
                //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
                if (xhrOnProgress.onprogress && xhr.upload) {
                    xhr.upload.onprogress = xhrOnProgress.onprogress;
                }
                return xhr;
            }
        }

        var data = {};
        upload.render({
            elem: '#uploadIcon', // 文件选择
            // accept:'apk',
            url: '${uploadParam.host}',
            exts: 'png|jpg|jpeg|bmp',
            method: 'POST',
            auto: true,
            before: function () {
                this.data.OSSAccessKeyId = $('#accessid').val();
                this.data.policy = $('#policy').val();
                this.data.signature = $('#signature').val();
                this.data.expire = $('#expire').val();
                this.data.success_action_status = $('#success_action_status').val();
                this.data.callBack = $("#key2_callBack").val();
                this.data.key = $("#key").val();

                element.progress('js_upload_progress', '0%');//设置页面进度条
                openIndex = layer.open({
                    type: 1,
                    title: '上传进度',
                    closeBtn: 1, //不显示关闭按钮
                    area: ['300px', '130px'],
                    shadeClose: false, //开启遮罩关闭
                    content: $("#uploadLoadingDiv").html(),
                    offset: '100px'
                });
            }
            , xhr: xhrOnProgress
            , progress: function (value) {//上传进度回调 value进度值
                element.progress('js_upload_progress', value + '%');//设置页面进度条
            }
            , choose: function (obj) {
                var loading = layer.load(1, {shade: [0.1, '#000']});
                obj.preview(function (index, file, result) {
                    var fileName = file.name;
                    var suffix = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length);
                    var key = '${uploadParam.key}' + "." + suffix;
                    $("#key").val(key);
                    $("#fileKey").val(key);
                    layer.close(loading);
                });
            },
            done: function (res) {
                layer.close(openIndex);
                if (res.code == 200) {
                    layer.msg("上传成功！")
                    $("#iconDiv").show();
                    $("#iconImg").attr("src", res.url);
                    $("input[name='icon']").val(res.url);
                    // parent.layer.style(layerIndex, {
                    //     width: '550px',
                    //     height: '510px'
                    // });
                } else {
                    layer.msg("上传失败！")
                }
            },
            error: function (res) {
                layer.close(openIndex);
                layer.msg(res.msg);
            }
        });

        getMenuNavInfo();
        getMenuGroupInfo();
    });

    function getMenuNavInfo() {
        $.ajax({
            url: ctx + "/dict/getGroup/APP_MENU_NAV"
            , dataType: "json"
            , success: function (data) {
                if (data.code === 200) {
                    data.data.forEach(function (item) {
                        <%--//var checkValue = '${entity.menuGroup}';--%>
                        var checkValue = '${entity.navCode}';
                        var content = '';
                        if (checkValue === item.id) {
                            content = '<option value="' + item.id + '" selected>' + item.name + '</option>';
                        } else {
                            content = '<option value="' + item.id + '">' + item.name + '</option>';
                        }
                        $("#menuNavSelect").append(content);
                        // 需要使用layui进行表单刷新
                        form.render();
                    });
                } else {
                    layer.msg(data.message);
                }
            }
            , error: function (info) {
                console.log(info);
                layer.msg("数据异常");
            }
        })
    }

    function getMenuGroupInfo() {
        $.ajax({
            url: ctx + "/dict/getGroup/APP_MENU_GROUP"
            , dataType: "json"
            , success: function (data) {
                if (data.code === 200) {
                    data.data.forEach(function (item) {
                        var checkValue = '${entity.menuGroup}';
                        var content = '';
                        if (checkValue === item.id) {
                            content = '<option value="' + item.id + '" selected>' + item.name + '</option>';
                        } else {
                            content = '<option value="' + item.id + '">' + item.name + '</option>';
                        }
                        $("#menuGroupSelect").append(content);
                        // 需要使用layui进行表单刷新
                        form.render();
                    });
                } else {
                    layer.msg(data.message);
                }
            }
            , error: function (info) {
                console.log(info);
                layer.msg("数据异常");
            }
        })
    }
</script>
</body>
</html>