<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('添加推送')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-post-add">
        <!--<div class="form-group">
            <label class="col-sm-2 control-label">消息类型</label>
            <div class="col-sm-10">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="2" name="messageType" checked=""> 系统消息
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="messageType"> 会员消息
                    </label>
                </div>
            </div>
        </div>-->
        <div class="form-group">
            <label class="col-sm-2 control-label">设备类型</label>
            <div class="col-sm-10">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="pushRange" checked=""> 全部
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="2" name="pushRange"> IOS
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="3" name="pushRange"> Android
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">请选择跳转类型</label>
            <div class="col-sm-5">
                <select class="form-control" name="jumpType" id="jumpType">
                    <option value="">--请选择跳转类型--</option>
                    <option th:each="type : ${jumpType}" th:text="${type.name}" th:value="${type.id}"></option>
                </select>
            </div>
            <div class="col-sm-5">
                <input class="form-control" type="text" placeholder="请填写url" id="targetObjInput">
                <div hidden class="row" id="targetObjSelectShow">
                    <div class="col-lg-6">
                        <div class="input-group">
                            <input type="text" class="form-control" id="targetObjSelect">
                            <div class="input-group-btn">
                                <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--<div class="form-group">
            <label class="col-sm-2 control-label">推送范围</label>
            <div class="col-sm-10">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="2" name="specifyType" checked=""> 全部用户
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="specifyType"> 指定手机号
                    </label>
                </div>
            </div>
        </div>-->
        <div class="form-group">
            <label class="col-sm-2 control-label">排版方式</label>
            <div class="col-sm-10">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="2" name="messageShowType" checked=""> 文本消息
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="messageShowType"> 图文消息
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">推送标题：</label>
            <div class="col-sm-10">
                <input class="form-control" type="text" name="pushTitle" id="pushTitle" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-xs-2 control-label">推送内容：</label>
            <div class="col-xs-10">
                <textarea name="pushContent" maxlength="500" class="form-control" rows="3"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="font-noraml">推送图片</label>
            <div class="file-loading">
                <input id="fileinput-demo-1" type="file" name="file" data-browse-on-zone-click="true"
                       data-msg-placeholder="Select {files} for upload...">
            </div>
        </div>
        <input hidden id="img" name="img">

        <div class="form-group">
            <label class="col-sm-2 control-label">是否发送通知</label>
            <div class="col-sm-10">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="2" name="isPush" checked=""> 不发送
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="isPush"> 发送
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">推送方式</label>
            <div class="col-sm-6">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="2" name="sendType" checked=""> 立即推送
                    </label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="sendType"> 定时推送
                    </label>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="input-daterange input-group">
                    <input name="pushTime" type="text" class="input-sm form-control" id="laydate-startTime" placeholder="yyyy-MM-dd HH:mm:ss"/>
                </div>
            </div>
        </div>

    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<script type="text/javascript">
    var prefix = ctx + "admin/upush";
    var platformUrl = "[[${platformUrl}]]";
    var url5 = "[[${url5}]]";
    var url6 = "[[${url6}]]";
    var url7 = "[[${url7}]]";
    var url14 = "[[${url14}]]";
    var url20 = "[[${url20}]]";
    var url21 = "[[${url21}]]";
    var url22 = "[[${url22}]]";
    var url24 = "[[${url24}]]";
    var url26 = "[[${url26}]]";

    function submitHandler() {
        if ($.validate.form()) {
            var data = $('#form-post-add').serializeArray();

            var jumpType = $('#jumpType').select2('val');
            if (!jumpType) {
                layer.msg("请选择跳转类型");
                return false;
            }

            var targetObj = '';
            var targetObjName = '';
            if (12 == jumpType || 100 == jumpType) { // 请填写url
                targetObj = $("#targetObjInput").val();
                if (!targetObj) {
                    layer.msg("请填写跳转的url");
                    return false;
                }
            } else if (0 == jumpType || 201 == jumpType || 16 == jumpType || 17 == jumpType || 28 == jumpType || 32 == jumpType || 33 == jumpType) { // 无需跳转
                targetObj = '';
            } else {
                targetObj = $("#targetObjSelect").attr("data-id");
                targetObjName = $("#targetObjSelect").val();
                if (!targetObj) {
                    layer.msg("请选择跳转的目标");
                    return false;
                }
            }
            /*var img = $('#img').val();
            if (!img) {
                layer.msg("请上传图片,请勿忘记点击上传按钮先上传图片哦～");
                return false;
            }*/
            data.push({"name": "jumpValue", "value": targetObj});
            data.push({"name": "jumpContent", "value": targetObjName});

            $.operate.save(prefix + "/create", data);
        }
    }

    $(function () {
        layui.use(['laydate'], function () {
            var laydate = layui.laydate;

            var startDate = laydate.render({
                elem: '#laydate-startTime',
                type: 'datetime',
                theme: 'molv',
                trigger: 'click',
            });
        });

        // 跳转类型选择事件
        function targetObjBsSuggest(searchUrl, targetType) {
            var bsSuggest = $("#targetObjSelect").bsSuggest({
                indexId: 0,                 // data.value 每组数据的第几个数据，作为input输入框的 data-id，设为 -1 且 idField 为空则不设置此值
                indexKey: 1,                // data.value 的第几个数据，作为input输入框的内容
                keyField: "ID",
                showBtn: false, //是否显示下拉按钮
                hideOnSelect:true, //鼠标从列表单击选择了值时，是否隐藏选择列表
                searchingTip: "获取中...",
                clearable: true, //是否可清除已输入的内容
                allowNoKeyword: false,      // 是否允许无关键字时请求数据
                multiWord: false,            // 以分隔符号分割的多关键字支持
                separator: ",",             // 多关键字支持时的分隔符，默认为空格
                getDataMethod: "url",       // 获取数据的方式，总是从 URL 获取
                effectiveFieldsAlias: {
                    Id: "ID",
                    Keyword: "名称"
                },
                showHeader: true,
                url: searchUrl + '&title=',
                /*优先从url ajax 请求 json 帮助数据，注意最后一个参数为关键字请求参数*/
                // jsonp: 'callback',
                /*如果从 url 获取数据，并且需要跨域，则该参数必须设置*/
                processData: function (json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
                    var i, len, data = {
                        value: []
                    };

                    if (!json) {
                        return false;
                    }

                    if (6 == targetType) {
                        dataList = json.value;
                    } else if(8 == targetType) {
                        dataList = json.rows;
                    } else if(24 == targetType) {
                        dataList = json.data;
                    } else if(26 == targetType) {
                        dataList = json.data;
                    } else {
                        dataList = json.data
                    }

                    console.log(dataList);
                    len = dataList.length;

                    for (i = 0; i < len; i++) {
                        var Title = '';
                        var Id = '';
                        if (6 == targetType) {
                            Id = dataList[i].performancesId;
                            Title = dataList[i].title;
                        } else if(8 == targetType) {
                            Id = dataList[i].roadShowId;
                            Title = dataList[i].title;
                        } else if(24 == targetType) {
                            Id = dataList[i].id;
                            Title = dataList[i].collect_name;
                        } else if(26 == targetType) {
                            Id = dataList[i].id;
                            Title = dataList[i].name;
                        } else {
                            Id = dataList[i].id;
                            Title = dataList[i].title;
                        }
                        data.value.push({
                            "Id": Id,
                            "Title": Title,
                        });
                    }
                    console.log(data);
                    return data;
                }
            });
        }

        $('#jumpType').on('select2:select', function (e) {
            $("#targetObjSelect").bsSuggest("destroy");
            // var cardTypeW = $("#cardType option:checked").text(); // 获取选中的名
            // var targetType = $("#targetType option:checked").val();
            var targetType = $('#jumpType').select2('val');
            if (12 == targetType || 100 == targetType) { // 请填写url
                $("#targetObjSelectShow").hide();
                $("#targetObjInput").show();
            } else if (0 == targetType || 201 == targetType || 16 == targetType || 17 == targetType || 28 == targetType || 32 == targetType || 33 == targetType) { // 无需跳转
                $("#targetObjSelectShow").hide();
                $("#targetObjInput").hide();
            } else {
                var urlVarName = 'url' + targetType;
                var urleval = eval(urlVarName);
                var arrEntities = {'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"'};
                searchUrl = urleval.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
                    return arrEntities[t];
                });

                $("#targetObjSelectShow").show();
                $("#targetObjInput").hide();

                targetObjBsSuggest(searchUrl, targetType);
            }
        });
        $("#fileinput-demo-1").fileinput({
            'theme': 'explorer-fas',
            // 'uploadUrl': "/kylin/banners/upload3",
            // 'uploadUrl': "http://127.0.0.1:9003" + "/platform/basicServices/alOss/upload",
            'uploadUrl': platformUrl + "/platform/basicServices/alOss/upload",
            "uploadExtraData": {
                "pathName" : "push",
                "buckType" : 1
            },
            // overwriteInitial: false,
            // initialPreviewAsData: true,
            autoReplace: true,
            dropZoneTitle: "请上传文件",
            maxFileCount: 1
        });
        $("#fileinput-demo-1").on("fileuploaded", function (event, data, previewId, index) {
            $("#img").val(data.response.ossPath)
        });
    })
</script>
</body>
</html>
