<!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('新品推荐')" />
    <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, .first_content {
            display: flex;
            align-items: center;
            margin-bottom: 12px;
        }
        .function_btn .hot_title {
            font-size: 16px;
            font-weight: 600;
        }
        .function_btn .tags {
            display: flex;
            align-items: center;
        }
        .function_btn .tags_content {
            display: flex;
            flex-direction: column;
        }
        label {
            margin: 0;
            display: flex;
            align-items: center;
        }
        .function_btn .tags label input {
            margin: 0 4px 0 12px;
        }
        .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;
            width: 100%;
        }
        .banner_item, .items {
            border: 1px dashed #ccc;
            border-radius: 10px;
            padding: 12px;
            margin-bottom: 20px;
        }
        .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;
        }
        .img_box {
            display: flex;
            flex-direction: column;
        }
    </style>
</head>
<body>
    <div class="home_page">
        <div class="left_content">
            <!-- <div class="function_btn">
                <div class="hot_title">
                    展示形式新页面：
                </div>
                <div class="tags">
                    <label>
                        <input type="radio" value="1" id="optionsRadios1" name="optionsRadios"><span>一行一个</span></label>
                    <label>
                        <input type="radio" value="2" id="optionsRadios2" name="optionsRadios"><span>一行两个</span></label>
                    <label>
                        <input type="radio" value="3" id="optionsRadios3" name="optionsRadios"><span>一行三个</span></label>
                    <label>
                        <input type="radio" value="4" id="optionsRadios4" name="optionsRadios"><span>上一下三</span></label>
                </div>
            </div> -->
            <div class="function_btn">
                <div class="hot_title">
                    位置顺序：
                </div>
                <div class="tags">
                    <input type="text" class="layui-input form-control titleIpt" id="sorting" value="" autocomplete="off" placeholder="请输入模块顺序">
                </div>
            </div>
            <div class="left_title">
                <button type="button" class="btn btn-info" onclick="addItem()">添加</button>
            </div>
            <div class="function_btn">
                
            </div>
            <div class="function_btn">
                <div id="bannerList" class="banner_list">
                    
                </div>
            </div>
            <div class="first_content" style="align-items: flex-start;">
                <div class="hot_title">
                    推荐文案：
                </div>
                <div class="tags">
                    <textarea id="information" class="form-control txtIpt" name="" style="width: 553px;height: 150px;"></textarea>
                </div>
            </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 id="iframe" src="http://172.16.3.82:8080/#/goods/home?place=admin" width="375" height="1190" 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 count = -1;
        let compilationsId = '';
        let createTime = '';
        let cubeId = '';
        let mid = '';
        let updateTime = '';
        $(function() {
            // addItem (1);
            let url = window.location.hostname;
            if (url.includes('test')) {
                $("#iframe").attr('src', 'https://ttestm.zhengzai.tv/#/goods/home?place=admin');
            } else if (!url.includes('test') && url.includes('admin')) {
                $("#iframe").attr('src', 'https://m.zhengzai.tv/#/goods/home?place=admin');
            } else {

            }
            promiseMethods('/front-index/list','post').then(res=>{
                let { data } = res;
                data.forEach(item => {
                    if (item.moudleName == "XINPINTUIJIAN") {
                        $("#sorting").val(item.indexs)
                    }
                })
            })
            promiseMethods('/goblin-front-cube/list', 'post', { type: 2 }).then(res => {
                let { data } = res;
                if (data && data.length > 0) {
                    let arr = data[0].spuId.split(',');
                    mid = data[0].mid;
                    cubeId = data[0].cubeId;
                    $("#information").val(data[0].informationA);
                    for (var i = 0; i < data.length; i++) {
                        addItem(data, i, arr)
                    }
                } else {
                    addItem()
                }
                // refreshItem()
            })
        })
        // $("input[name=optionsRadios]").change(function(e){
        //     $("#bannerList").empty();
        //     count = e.target.value;
        //     promiseMethods('/goblin-front-cube/list', 'post').then(res => {
        //         let { data } = res;
        //         if (data && data.length > 0) {
        //             let arr = data[0].spuId.split(',');
        //             if (count == data[0].rowType) {
        //                 for (var i = 0; i < data.length; i++) {
        //                     let str = '';
        //                     str += `<div class="items" data-id="${data[i].mid}">
        //                             <div class="first_content" style="align-items: flex-start;">
        //                                 <div class="hot_title">
        //                                     选择商品：
        //                                 </div>
        //                                 <div class="tags_content" style="width: 553px;">
        //                                     <div class="input-group">
        //                                         <input id="goodsName${i}" data-id="${arr[i]}" type="text" class="form-control storeList" placeholder="${data[i].goblinGoods.name}">
        //                                         <div class="input-group-btn">
        //                                             <ul class="dropdown-menu dropdown-menu-right" role="menu">
        //                                             </ul>
        //                                         </div>
        //                                     </div>
        //                                     <div class="img_box">
        //                                         <img id="goodsImg${i}" src="${data[i].goblinGoods.coverPic}" alt="" style="max-height: 180px;max-width:180px;margin: 12px 0;">
        //                                         <p id="goodsTitle${i}" style="width:180px;">${data[i].goblinGoods.name}</p>
        //                                         <p id="goodsPrice${i}">${data[i].goblinGoods.priceLe}</p>
        //                                     </div>
        //                                     <button id="del${i}" type="button" class="btn btn-danger" style="width: 60px;" onclick="del('${i}')">删除</button>
        //                                 </div>
        //                             </div>
        //                         </div>`
        //                     $("#bannerList").append(str);
        //                     common(i)
        //                 }
        //             } else {
        //                 addItem (count);
        //             }
        //         } else {
        //             addItem (count);
        //         }
        //         // refreshItem()
        //     })
        // })
        function addItem (data, i, arr) {
            let str = '';
            
            if (data) {
                count = i;
                str = `<div id="item${count}" class="items" data-id="${data[i].mid}">
                        <div class="first_content" style="align-items: flex-start;">
                            <div class="hot_title">
                                选择商品：
                            </div>
                            <div class="tags_content" style="width: 553px;">
                                <div class="input-group">
                                    <input id="goodsName${i}" data-id="${arr[i]}" type="text" class="form-control storeList" placeholder="${data[i].goblinGoods.name}">
                                    <div class="input-group-btn">
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                                <div class="img_box">
                                    <img id="goodsImg${i}" src="${data[i].goblinGoods.coverPic}" alt="" style="max-height: 180px;max-width:180px;margin: 12px 0;">
                                    <p id="goodsTitle${i}" style="width:180px;">${data[i].goblinGoods.name}</p>
                                    <p id="goodsPrice${i}">${data[i].goblinGoods.priceLe}</p>
                                </div>
                                <button id="del${i}" type="button" class="btn btn-danger" style="width: 60px;" onclick="del('${i}')">删除</button>
                            </div>
                        </div>
                    </div>`
            } else {
                count++;
                str = `<div id="item${count}" class="items">
                    <div class="first_content" style="align-items: flex-start;">
                        <div class="hot_title">
                            选择商品：
                        </div>
                        <div class="tags_content" style="width: 553px;">
                            <div class="input-group">
                                <input id="goodsName${count}" type="text" class="form-control storeList" placeholder="搜索商品名称">
                                <div class="input-group-btn">
                                    <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                    </ul>
                                </div>
                            </div>
                            <div class="img_box">
                                <img id="goodsImg${count}" src="" alt="" style="max-height: 180px;max-width:180px;margin: 12px 0;">
                                <p id="goodsTitle${count}" style="width:180px;"></p>
                                <p id="goodsPrice${count}"></p>
                            </div>
                            <button id="del${count}" type="button" class="btn btn-danger" style="width: 60px;" onclick="del('${count}')">删除</button>
                        </div>
                    </div>
                </div>`
            }
            $("#bannerList").append(str);
            common(count)
        }
        function save () {
            let data = [{
                compilationsId: compilationsId || '',
                createTime: createTime || '',
                cubeId: cubeId|| '',
                goblinGoods: {},
                informationA: $("#information").val(),
                informationB: '',
                informationC: '',
                informationD: '',
                mid: mid || '0',
                delTag: '0',
                pageType: '',
                rowType: '',
                type: 2,
                spuId: [],
                spuName: '',
                updateTime: updateTime || ''
            }];
            let domData = $('.items');
            
            Array.prototype.forEach.call(domData,function (item,index) {
                data[0].spuId.push(item.querySelector('.storeList').getAttribute('data-id') || '');
            });//foreach js6章封装
            data[0].spuId = data[0].spuId.toString();
            promiseMethods('/goblin-front-cube/updateOrCreate', 'post', JSON.stringify(data), 'application/json').then(() => {
                let data = {
                    "mid": 5,
                    "moudleIndexId": "70875412096233472977232",
                    "moudleName": "XINPINTUIJIAN",
                    "indexs": $('#sorting').val(),
                    "updateTime": "2022-01-06 13:55:31",
                    "createTime": "2022-01-06 13:55:34",
                    "delTag": 0,
                    "status": 0
                };
                return promiseMethods('/front-index/update', 'post', JSON.stringify(data), 'application/json')
            }).then((res) => {
                layer.msg("保存成功！")
                $.operate.successCallback(res);
            })
            
        }
        function del (num) {
            $("#item"+num+"").remove();
        }
        function common (num) {
            $(".storeList").bsSuggest({
                idField: 'spuId',                 // data.value 的第几个数据，作为input输入框的内容
	            keyField: 'name',                // data.value 的第几个数据，作为input输入框的内容
                allowNoKeyword: false, //是否允许无关键字时请求数据
                showBtn:false,
                multiWord: true, //以分隔符号分割的多关键字支持
                hideOnSelect: true,
                getDataMethod: "url", //获取数据的方式，总是从 URL 获取
                effectiveFields: ['name'],
                url: '/compilations/goodsList?name=',
                /*如果从 url 获取数据，并且需要跨域，则该参数必须设置*/
                processData: function (json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
                    //字符串转化为 js 对象
                    let data = {};
                    data.value = json.data
                    return data
                }
            }).on('onDataRequestSuccess', function (e, result) {
            }).on('onSetSelectValue', function (e, selectedData,selectedRawData ) { // 当前行的所有值都能拿到
                let index = e.target.id.replace(/[^\d]/g,''); // 选中的是第几组
                $("#goodsImg"+index+"").attr('src', selectedRawData.coverPic);
                $("#goodsTitle"+index+"").text(selectedRawData.name);
                $("#goodsPrice"+index+"").text('￥'+ selectedRawData.priceLe);
                $('#del'+index+'').show();
            }).on('onUnsetSelectValue', function (e) {
            });;
        }
        function promiseMethods(url,type,data,contentType) {
            return new Promise((resolve,reject)=>{
                $.ajax({
                    url,
                    type,
                    data,
                    contentType,
                    success:function(res) {
                        resolve(res);
                    }
                })
            })
        }
    </script>
</body>
</html>