<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<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: 438px">
    <input name="id" value="${officialActivity.id}" hidden/>

    <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="officName" value="${officialActivity.officName}" lay-verify="required"
                       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:0px">
            <div class="layui-input-block">
                <input name="officTitle" value="${officialActivity.officTitle}" lay-verify="required"
                       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:0px">
            <div class="layui-input-block">
                <c:if test="${not empty actTypeList}">
                    <select  lay-verify="required" name="actType" id="actType" class="layui-select">
                        <option value="">请选择</option>
                        <c:forEach items="${actTypeList}" var="type">
                            <option value="${type.value}" ${officialActivity.actType eq type.value ? 'selected' : ''}>${type.name}</option>
                        </c:forEach>
                    </select>
                </c:if>
            </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">
                <input name="officContent" value="${officialActivity.officContent}" 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:0px">
            <div class="layui-input-block">
                <input name="startTime" lay-verify="required"
                       value="<fmt:formatDate  value='${officialActivity.startTime}' pattern='yyyy-MM-dd'/>"
                       autocomplete="off" placeholder="请输入开始时间" class="layui-input loan-date"/>
            </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">
                <input name="endTime" lay-verify="required"
                       value="<fmt:formatDate  value='${officialActivity.endTime}' pattern='yyyy-MM-dd'/>"
                       autocomplete="off" placeholder="请输入结束时间" class="layui-input loan-date"/>
            </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="${empty officialActivity}">
                        <c:forEach items="${dictList}" var="dict">
                            <input type="checkbox" name="activityGroupNo" title="${dict.name}"
                                   value="${dict.value}">
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <select  lay-verify="required" name="value" id="value" class="layui-select">
                            <option value="">请选择</option>
                            <c:forEach items="${dictList}" var="dict">
                                <option value="${dict.value}" <c:if test="${officialActivity.activityGroupNo eq dict.value}">selected</c:if>>${dict.name}</option>
                            </c:forEach>
                        </select>
                    </c:otherwise>
                </c:choose>
            </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">
                <input type="radio" name="roleType" value="common" ${empty officialActivity || officialActivity.roleType eq 'common' ?  'checked' : ''} title="全部" checked>
                <c:forEach items="${roleList}" var="r">
                    <input type="radio" name="roleType" ${officialActivity.roleType eq r.roleCode ? 'checked' : ''} value="${r.roleCode}" title="${r.roleName}" >
                </c:forEach>
            </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">
                <input name="groupBy" value="${officialActivity.groupBy}" 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:0px">
            <div class="layui-input-block">
                <input name="toImgUrl" value="${officialActivity.toImgUrl}" placeholder="请输入链接地址" class="layui-input"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">活动图片</label>
        <div class="layui-input-block" style="display: flex;justify-content: flex-start">
            <input name="fileKey" id="fileKey" hidden/>
            <input name="officImgUrl" id="url" value="${officialActivity.officImgUrl}" hidden/>
            <input name="chooseFile" id="chooseFile" placeholder="浏览..." class="layui-input layui-col-xs6"
                   style="cursor:hand" readonly/>
            <button type="button" class="layui-btn layui-btn-normal layui-col-xs3" id="uploadBtn">
                上传
            </button>
        </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">
                <textarea placeholder="请输入活动规则" name="officRule" id="officRule"
                          style="display: none">${officialActivity.officRule}</textarea>
                <%--                <input name="goodDesc" value="${officialGoods.goodDesc}" maxlength="100" placeholder="请输入商品描述" 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 form;
    //Demo
    layui.use(['form', 'upload', 'element', 'layer', 'laydate', 'layedit'], function () {
        form = layui.form;
        var upload = layui.upload;
        var layer = layui.layer;
        var element = layui.element;
        var laydate = layui.laydate;
        var layedit = layui.layedit;

        var editIndex = layedit.build('officRule', {
            height: 140 //设置编辑器高度
        }); //
        // laydate.render({
        //     elem: '#start' //指定元素
        //     ,zIndex: 99999999
        // });
        // laydate.render({
        //     elem: '#end' //指定元素
        //     ,zIndex: 99999999
        // });
        lay('.loan-date').each(function () {
            laydate.render({
                elem: this
                , trigger: 'click'
            });
        });
        element.init();
        $('#close').click(function () {
            parent.layer.closeAll();
        });

        //创建监听函数
        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 openIndex = "";
        upload.render({
            elem: '#chooseFile', // 文件选择
            accept: 'images',
            acceptMime: 'image/*',
            url: '${uploadParam.host}',
            exts: 'jpg|png|gif|bmp|jpeg',
            method: 'POST',
            auto: false, // 设置不自动提交
            bindAction: '#uploadBtn', // 提交按钮
            before: function () {
                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']});
                var data = {};
                obj.preview(function (index, file, result) {
                    var fileName = file.name;
                    $("#chooseFile").val(fileName);
                    var suffix = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length);
                    var key = '${uploadParam.key}' + "." + suffix;
                    $("#key").val(key);
                    $("#fileKey").val(key);
                    data.OSSAccessKeyId = $('#accessid').val();
                    data.policy = $('#policy').val();
                    data.signature = $('#signature').val();
                    data.expire = $('#expire').val();
                    data.success_action_status = $('#success_action_status').val();
                    data.callBack = $("#key2_callBack").val();
                    data.key = key;
                    layer.close(loading);
                });
                this.data = data;
            },
            done: function (res) {
                layer.close(openIndex);
                if (res.code == 200) {
                    layer.msg("上传成功！")
                    $("#url").val(res.url);
                } else {
                    layer.msg("上传失败！")
                }
            },
            error: function (res) {
                layer.msg(res.msg);
            }
        });

        //关闭进度条提示
        $(document).on('click', '.js_upload_progress_bar_sure', function () {
            layer.close(layer.index);
        });

        //监听提交
        form.on('submit(formDemo)', function (data) {
            var index = layer.load(1, {shade: [0.1, '#000']});
            data.field.officRule = layedit.getContent(editIndex);
            if ($('#id').val() == null) {
                var activityGroupNo = [];
                $("input[name='activityGroupNo']:checked").each(function (i) {
                    activityGroupNo[i] = $(this).val();
                })
                var noStr = activityGroupNo.join(",");
                data.field.activityGroupNo = noStr;
            }
            $.ajax({
                url: '${ctx}/officialActivity/saveOrUpdate',
                method: 'post',
                data: JSON.stringify(data.field),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    layer.close(index);
                    if (res.code == 200) {
                        layer.alert(res.data, function () {
                            parent.location.reload();
                        })
                    } else {
                        layer.msg(res.message, {
                            icon: 5
                        });
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>
