<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('顶部banner')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <style>
        .home_page {
            display: flex;
            justify-content: center;
            padding: 20px;
        }
        .right_content {
            font-size: 16px;
            font-weight: 600;
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 50%;
        }
        .left_content {
            flex: 1;
        }
        .left_title, .right_title {
            margin-bottom: 12px;
        }
        .function_btn {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }
        .function_btn .hot_title {
            font-size: 16px;
            font-weight: 600;
        }
        .function_btn .tags {
            display: flex;
            align-items: center;
        }
        .function_btn .tags .tag {
            padding: 6px 12px 4px;
            border: 1px solid #ccc;
            border-radius: 5px;
            position: relative;
            margin-right: 12px;
        }
        .function_btn .tags .tag span {
            display: inline-block;
            position: absolute;
            top: -8px;
            right: -8px;
            width: 18px;
            height: 18px;
            line-height: 16px;
            border: 1px solid #aaa;
            border-radius: 50%;
            background: #fff;
            text-align: center;
            cursor: pointer;
        }
        .banner_list {
            display: flex;
            flex-direction: column;
        }
        .banner_item {
            border: 1px dashed #ccc;
            border-radius: 10px;
            padding: 12px;
            margin-bottom: 20px;
            position: relative;
        }
        .banner_item .del {
            position: absolute;
            right: -8px;
            top: -8px;
            width: 20px;
            height: 20px;
            line-height: 18px;
            border: 1px solid #ccc;
            border-radius: 50%;
            text-align: center;
            cursor: pointer;
        }
        .img_show, .set_method, .set_function {
            display: flex;
        }
        .title {
            font-size: 16px;
            font-weight: 600;
            width: 80px;
            text-align: right;
        }
        .kv-upload-progress {
            display: none !important;
        }
        .ibox-content .imgBox {
            width: 350px;
            max-height: 180px;
        }
        .jump, .ticket_name, .set_function_time {
            display: flex;
            flex-grow: 1;
            align-items: center;
        }
        .iptOrSelect {
            flex: 1;
        }
    </style>
</head>
<body>
    <div class="home_page">
        <div class="left_content">
            <div class="left_title">
                
            </div>
            <div class="function_btn">
                <div class="hot_title">
                    展示形式：
                </div>
                <div class="tags" style="font-weight: 600;font-size: 16px;">
                    轮播
                </div>
            </div>
            <div class="function_btn">
                <div class="hot_title">
                    选择照片：
                </div>
                <div class="tags">
                    <button type="button" class="btn btn-warning" style="margin-right: 12px;" onclick="addItem(0)">添加banner</button>
                    <span id="tall">(最多允许添加10张图，建议宽度750像素)</span>
                </div>
            </div>
            <div id="bannerList" class="banner_list">
                
            </div>
            
            <div class="footer_btn">
                <button type="button" class="btn btn-success" onclick="save()">保存</button>
            </div>
        </div>
        <div class="right_content">
            <div class="right_title">
                
            </div>
            <div class="function_area">
                <iframe src="http://m.zhengzai.tv" width="375" height="896" frameborder="0"></iframe>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: bootstrap-suggest-js" />
    <script th:inline="javascript">
        var prefix2 = ctx + "local";
        var platformUrl = [[${platformUrl}]];
        let num = 0;
        let type = 0;
        $(function(){
            let name = 'id';
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            type = unescape(r[2]);
            if (type == 2) {
                $("#tall").html('至少添加8张图，建议尺寸1：1')
            }
            let data = {
                type: 1
            }
            promiseMethods('/frontBanner/list', 'post', data).then(res => {
                console.log(res, 121212121)
                let { data } = res;
                for (var i = 0; i < data.length; i++) {
                    let str = `<div class="banner_item" data-id="${data[i].bannerId}" data-index="${data[i].mid}">
                        <span class="del" onclick="delItem('${i+1}', '${index}')">x</span>
                        <div class="img_show">
                            <div class="title">
                                图片展示：
                            </div>
                            <div class="ibox-content" style="border: none;">
                                <img id="viewImg${num}" class="imgBox" src="" alt="">
                                <div class="form-group">
                                    <div class="file-loading">
                                        <input id="fileinput${num}" type="file" name="file" data-browse-on-zone-click="true" data-theme="fas">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="set_method" style="margin-bottom: 20px;">
                            <div class="jump">
                                <div class="title">
                                    跳转方式：
                                </div>
                                <div class="iptOrSelect" style="margin-right: 12px;">
                                    <select id="jumpSelect${num}" onchange="changeJump(this)" class="type form-control" data-first-title="请选择">
                                        <option value="1">票务</option>
                                        <option value="2">商品</option>
                                        <option value="3">H5</option>
                                    </select>
                                </div>
                            </div>
                            <div class="ticket_name">
                                <div class="title">
                                    <span class="change${num}">票务名称</span>：
                                </div>
                                <div class="input-group iptOrSelect shwoSelect${num}"">
                                    <input id="ticketName${num}" type="text" class="form-control storeList" onFocus="focusMethod(this)" placeholder="请输入查询信息">
                                    <div class="input-group-btn">
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                                <div class="iptOrSelect shwoH5${num}" style="display:none;">
                                    <input id="ticketName${num}" type="text" class="layui-input form-control" placeholder="请输入活动链接">
                                </div>
                            </div>
                        </div>
                        <div class="set_function">
                            <div class="set_function_time">
                                <div class="title">
                                    时间设置：
                                </div>
                                <div class="select_tiem">
                                    <input type="text" class="layui-input form-control timeStart" id="startTime${num}" autocomplete="off" placeholder="配置活动时间">
                                </div>
                            </div>
                            <div class="set_function_time" style="width: 40%;">
                                <div class="title">
                                    排序：
                                </div>
                                <div class="select_tiem">
                                    <input id="px${num}" type="text" class="layui-input form-control px" placeholder="请输入顺序数字">
                                </div>
                            </div>
                        </div>
                    </div>`;
                    $("#bannerList").append(str);
                    common(num)
                }
            })
            // 
        })
        function addItem (index) {
            if (num == 10) {
                return layer.msg('最多可添加10张图片')
            }
            num++
            let str = `<div class="banner_item" data-id="" data-index="">
                        <span class="del" onclick="delItem('${num}', '${index}')">x</span>
                        <div class="img_show">
                            <div class="title">
                                图片展示：
                            </div>
                            <div class="ibox-content" style="border: none;">
                                <img id="viewImg${num}" class="imgBox" src="" alt="">
                                <div class="form-group">
                                    <div class="file-loading">
                                        <input id="fileinput${num}" type="file" name="file" data-browse-on-zone-click="true" data-theme="fas">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="set_method" style="margin-bottom: 20px;">
                            <div class="jump">
                                <div class="title">
                                    跳转方式：
                                </div>
                                <div class="iptOrSelect" style="margin-right: 12px;">
                                    <select id="jumpSelect${num}" onchange="changeJump(this)" class="type form-control" data-first-title="请选择">
                                        <option value="1">票务</option>
                                        <option value="2">商品</option>
                                        <option value="3">H5</option>
                                    </select>
                                </div>
                            </div>
                            <div class="ticket_name">
                                <div class="title">
                                    <span class="change${num}">票务名称</span>：
                                </div>
                                <div class="input-group iptOrSelect shwoSelect${num}"">
                                    <input id="ticketName${num}" type="text" class="form-control storeList" onFocus="focusMethod(this)" placeholder="请输入查询信息">
                                    <div class="input-group-btn">
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                                <div class="iptOrSelect shwoH5${num}" style="display:none;">
                                    <input id="ticketName${num}" type="text" class="layui-input form-control" placeholder="请输入活动链接">
                                </div>
                            </div>
                        </div>
                        <div class="set_function">
                            <div class="set_function_time">
                                <div class="title">
                                    时间设置：
                                </div>
                                <div class="select_tiem">
                                    <input type="text" class="layui-input form-control timeStart" id="startTime${num}" autocomplete="off" placeholder="配置活动时间">
                                </div>
                            </div>
                            <div class="set_function_time" style="width: 40%;">
                                <div class="title">
                                    排序：
                                </div>
                                <div class="select_tiem">
                                    <input id="px${num}" type="text" class="layui-input form-control px" placeholder="请输入顺序数字">
                                </div>
                            </div>
                        </div>
                    </div>`;
            $("#bannerList").append(str);
            common(num)
        }
        function save () {
            let obj = {};
            let itmeLength = $(".banner_item");
            
            let data = []
            Array.prototype.forEach.call(itmeLength,function (item,index,arr) {
                obj.activityUrl = item.querySelector('#ticketName'+(index+1)+'').value;
                obj.bannerId = item.getAttribute('data-id');
                obj.bannerType = 1;
                obj.createTime = '';
                obj.delTag = '0';
                obj.endTime = '';
                obj.indexs = item.querySelector('.px').value;;
                obj.jumpType = item.querySelector('#jumpSelect'+(index+1)+'').value;
                obj.mid = item.getAttribute('data-index') || 0;
                obj.picUrl = item.querySelector('.imgBox').getAttribute('src');
                obj.startTime = item.querySelector('.timeStart').value;
                obj.updateTime = '';
                obj.ticketId = '';
                obj.ticketName = '';
                data.push({...obj})
            });//foreach js6章封装的方法
            console.log(data, 'data')
            promiseMethods('/frontBanner/updateOrCreate', 'post', JSON.stringify(data), 'application/json').then(res => {
                console.log(res)
            })
            // refreshItem()
        }
        function focusMethod(e) {
            let index = $(e).attr('id').replace(/[^\d]/g,''); // 选中的是第几组
            let url = '';
            $(".storeList").bsSuggest({
                allowNoKeyword: false, //是否允许无关键字时请求数据
                showBtn:false,
                multiWord: true, //以分隔符号分割的多关键字支持
                hideOnSelect: true,
                getDataMethod: "url", //获取数据的方式，总是从 URL 获取
                url: ctx+'kylin/base/performance/status?status=(3,6)&title=',
                /*如果从 url 获取数据，并且需要跨域，则该参数必须设置*/
                processData: function (json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
                    //字符串转化为 js 对象
                    return json;
                }
            }).on('onDataRequestSuccess', function (e, result) {
	            console.log('onDataRequestSuccess: ',e, result);
            }).on('onSetSelectValue', function (e, selectedData,selectedRawData ) { // 当前行的所有值都能拿到
                console.log('211: ',  e.target.value, selectedData, selectedRawData);
            }).on('onUnsetSelectValue', function (e) {
                console.log("onUnsetSelectValue", e);
            });;
        }
        function delItem (num) {
            console.log(num)
            
        }
        function changeJump(e) {
            let index = $(e).attr('id').replace(/[^\d]/g,''); // 选中的是第几组
            let jumpType = $(e).val(); // 选中的跳转类型
            switch(jumpType) {
                case '1':
                    $('.shwoH5'+index+'').hide();
                    $('.shwoSelect'+index+'').show();
                    $(".change"+index+"").text('票务名称');
                    break;
                case '2':
                    $('.shwoH5'+index+'').hide();
                    $('.shwoSelect'+index+'').show();
                    $(".change"+index+"").text('商品名称');
                    break;
                case '3':
                    $(".change"+index+"").text('活动链接');
                    $('.shwoH5'+index+'').show();
                    $('.shwoSelect'+index+'').hide();
                    break;
                default:
                    $('.shwoH5'+index+'').hide();
                    $('.shwoSelect'+index+'').show();
                    $(".change"+index+"").text('商品集合');
            }
        }
        function common (num) {
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: `#startTime${num}`, //指定元素
                    type: 'datetime'
                });
            });
            $("#fileinput"+num+"").fileinput({
                'theme': 'explorer-fas',
                'uploadUrl':  "https://devplatform.zhengzai.tv/platform/basicServices/alOss/upload",
                "uploadExtraData": {
                    "pathName" : "banner",
                    "buckType" : 1
                },
                autoReplace: true,
                showCaption: false,
                showPreview: false,
                showRemove: false,
                showUpload: false,
                showCancel: false,
                showClose: false,
                autoReplace: true,
                dropZoneTitle: "请上传文件",
                maxFileCount: 1
            }).on("filebatchselected", function (event, files) {         //默认上传
                $(this).fileinput("upload");
            })
            .on("fileuploaded", function (event, data) {       //上传回调事件
                let showPicture = 'https://img.zhengzai.tv/' + data.response.data.ossPath;
                $("#viewImg"+num+"").attr('src', showPicture)
            })
        }
        function promiseMethods(url,type,data,contentType) {
            return new Promise((resolve,reject)=>{
                $.ajax({
                    url,
                    type,
                    data,
                    contentType,
                    success:function(res) {
                        resolve(res);
                    }
                })
            })
        }
    </script>
</body>
</html>