<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>\
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ 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"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/page/umeditor/themes/default/css/umeditor.css">
    <script type="text/javascript" src="${ctx}/page/umeditor/third-party/template.min.js"></script>
    <script type="text/javascript" src="${ctx}/page/umeditor/umeditor.config.js"></script>
    <script type="text/javascript" src="${ctx}/page/umeditor/umeditor.min.js"></script>
    <script type="text/javascript" src="${ctx}/page/umeditor/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript">
        window.UMEDITOR_HOME_URL = ctx + "/page/umeditor/";
        var URL = window.UEDITOR_HOME_URL;
    </script>
    <style>
        .layui-input, .layui-textarea {
            border-color: #606060;
        }
        .layui-form-selectup {
            z-index: 899999;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" style="width: 80%; margin-top: 20px">
    <input name="id" value="${entity.id}" hidden/>

    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block" style="margin-left:0">
            <div class="layui-input-block">
                <input name="title" value="${entity.title}" placeholder="请输入标题" 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:0">
            <div class="layui-input-block">
                <input name="desc" value="${entity.desc}" maxlength="50" placeholder="请输入简介" class="layui-input" lay-verify="required"  lay-reqText="请输入简介"/>
            </div>
        </div>
    </div>

    <input name="imgs" value="${entity.imgs}" style="display: none"/>
    <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.imgs ? 'none' : 'black'}">
        <div class="layui-input-block">
            <img id="iconImg" src="${entity.imgs}" alt="" height="50px" width="50px"/>
        </div>
    </div>

    <c:set value="${fn:split(entity.type, ',') }" var="typeArray" />
    <div class="layui-form-item">
        <label class="layui-form-label">类别</label>
        <div class="layui-input-block">
            <input type="checkbox" name="type" value="1" title="商户攻略"
            <c:forEach items="${typeArray}" var="item">
            <c:if test="${item == '1'}">
                   checked="true"
            </c:if>
            </c:forEach>
            >
            <input type="checkbox" name="type" value="4" title="服务商攻略"
            <c:forEach items="${typeArray}" var="item">
            <c:if test="${item == '4'}">
                   checked="true"
            </c:if>
            </c:forEach>
            >
            <input type="checkbox" name="type" value="2" title="资讯"
            <c:forEach items="${typeArray}" var="item">
            <c:if test="${item == '2'}">
                   checked="true"
            </c:if>
            </c:forEach>
            >
            <input type="checkbox" name="type" value="3" title="活动"
            <c:forEach items="${typeArray}" var="item">
            <c:if test="${item == '3'}">
                   checked="true"
            </c:if>
            </c:forEach>
            >
        </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">
                <input name="seq" value="${entity.seq}" placeholder="请输入序号" type="number" class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容链接</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block" style="width: 975px">
                <input placeholder="内容链接" value="${entity.url}" id="url" name="url" lay-verify="url"  class="layui-input" />
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block" style="margin-left:0px">
            <div class="layui-input-block" style="width: 975px">
                <textarea placeholder="请输入内容" id="content" name="content" lay-verify="content"  class="layui-textarea">${entity.content}</textarea>
            </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">
                <select id="status" name="status">
                    <option value="">请选择</option>
                    <option value="0" <c:if test="${entity.status == 0}">selected</c:if>>显示</option>
                    <option value="1" <c:if test="${entity.status == 1}">selected</c:if>>不显示</option>
                </select>
            </div>
        </div>
    </div>

    <c:if test="${empty entity.id}">
        <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">
                    <select id="pushType" name="pushType">
                        <option value="0">请选择</option>
                        <option value="1">是</option>
                    </select>
                </div>
            </div>
        </div>
    </c:if>

    <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>
</form>

<script>
    //Demo
    layui.use(['form','layer', 'upload'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
        var um = UM.getEditor('content');

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

        //监听提交
        form.on('submit(formDemo)', function (data) {
            let params  = data.field;
            let checkIds = $("input[name='type']:checked");
            let type = null;
            if (checkIds.length > 0) {
                type = "";
                checkIds.each(function() {
                    type = type + $(this).val() + ",";
                })
                type = type.substring(0, type.length - 1)
                params.type = type
            }
            console.log("type : " + params.type)
            params.content = um.getContent();
            var index = layer.load(1, {shade: [0.1, '#000']});
            $.ajax({
                url: '${ctx}/bestNewActivity/saveOrUpdate',
                method: 'post',
                data: JSON.stringify(params),
                dataType: 'json',
                contentType: "application/json",
                success: function (res) {
                    layer.close(index);
                    if (res.code == 200) {
                        layer.alert(res.message, function () {
                            parent.location.reload();
                        })
                    } else {
                        layer.msg(res.message, {
                            icon: 5
                        });
                    }
                }
            })
            return false;
        });

        <%--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);--%>
        <%--    }--%>
        <%--});--%>

        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;
            }
        }

        upload.render({
            elem: '#uploadIcon', // 文件选择
            url: ctx + '/upload/ossUploadFile',
            exts: 'jpg|png|gif|bmp|jpeg',
            method: 'POST',
            auto: true,
            before: function () {

                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 + '%');//设置页面进度条
            }
            , done: function (res) {
                layer.close(openIndex);
                if (res.code == 200) {
                    layer.msg("上传成功！")
                    $("#iconDiv").show();
                    $("#iconImg").attr("src", res.url);
                    $("input[name='imgs']").val(res.url);
                } else {
                    layer.msg("上传失败！");
                }
            }
            , error: function (res) {
                layer.close(openIndex);
                layer.msg(res.msg);
            }
        });
    });


</script>
</body>
</html>