<!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 :: datetimepicker-css"/>
    <style>
        body .layui-layer-btn {
            display: none !important;
        }
        #layui-layer1 {
            z-index: 1001 !important;
        }
        #layui-layer-shade1 {
            z-index: 1000 !important;
        }
        #addClassify, #detailPro {
            display: none;
            width: 650px;
            padding-top: 20px;
        }
        #addClassify2, #detailPro {
            display: none;
            width: 450px;
            height: 260px;
            padding-top: 20px;
        }
        #addClassify .selectClassify {
            display: flex;
            align-items: center;
            padding: 0 60px;
        }
        .selectClassify .addAvatarItem .selectClassifyOne {
            display: flex;
        }
        .selectClassify .addAvatarItem .selectClassifyOne .span-label {
            width: 85px;
        }
        .picker {
            display: flex;
            justify-content: flex-start;
        }
        #addClassify .selectClassify label {
            display: flex;
            margin-right: 20px;
            align-items: center;
        }
        #addClassify .selectClassify label input {
            margin: 0;
            margin-right: 2px;
        }
        #detailPro .detail_inner {
            margin-bottom: 20px;
        }
        #detailPro .detail_title {
            margin-top: 50px;
            font-weight: bold;
            font-size: 16px;
        }
        .bottomBtn {
            text-align: left;
            padding: 30px 15px 12px;
        }
        .layui-layer-shade{
            opacity: 30%;
        }
        .item div {
            display: flex;
        }
        .item div a{
            margin-left: 12px;
        }
        /* 星星 */
        .required-group-name {
            color: red;
            font-size: 20px;
            position: relative;
            top: 6px;
        }
        .groupName {
            border: 1px solid #dfdfdf;
        }
        .required-province {
            color: red;
            display: inline-block;
            margin-right: 3px;
            position: relative;
            top: 6px;
            font-size: 20px;
        }
        /* 必填红色文字 */
        .required-group-text {
            height: 20px;
            color: red;
        }
        .required-province-text {
            color: red;
            height: 20px;
        }
        .search-wrapper {
            width: 100%;
        }
        .date-range {
            width: 100%;
        }
        .banner_list {
            display: flex;
            flex-direction: column;
            width: 100%;
        }
        .banner_item, .items {
            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 class="gray-bg">
<div class="container-div">
    <div class="row">
        <!-- <div class="col-sm-12 search-collapse">
            <form id="coupon-form">
                <div class="select-list">
                    <ul>
                        <input type="hidden" name="couType" th:value="${couType}"/>
                        <li>
                            <input type="text" name="mobile" placeholder="请输入手机号码"/>
                        </li>
                        <li>
                            状态：
                            <select name="status" id="">
                                <option value="0">全部</option>
                                <option value="1">正常</option>
                                <option value="2">冻结</option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div> -->

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-primary" onclick="openAddActivityModal()" shiro:hasPermission="monitor:job:export">
                增加活动
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>

    </div>
    <!-- modal -->
    <div id="addClassify">
        <div class="selectClassify">
            <div class="addAvatarItem">
                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>活动名称：</span>
                    <input type="text" class="form-control" id="activeName" style="margin-bottom: 15px;">
                </div>

                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>关联类型：</span>
                    <select name="activeType" id="activeType" class="form-control m-b">
                        <option value="">所有</option>
                        <option value="1">NFT</option>
                        <option value="2">组合购</option>
                        <option value="3">演出</option>
                        <option value="4">商品</option>
                    </select>
                </div>

                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>关联ID：</span>
                    <div class="search-wrapper" style="margin-bottom: 15px;">
                        <!-- 关联 模糊搜索 -->
                        <div id="search"  placeholder="请搜索" name="seaarchSelect"></div>
                    </div>
                </div>

                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>状态：</span>
                    <select name="status" id="status" class="form-control m-b">
                        <option value="">所有</option>
                        <option value="0">开启</option>
                        <option value="1">未开启</option>
                    </select>
                </div>

                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>时间范围：</span>
                    <div class="date-range"  style="margin-bottom: 15px;">
                        <input type="text" style="width: 200px;float: left" class="form-control" id="startTime" placeholder="开始时间选择" name="startTime"/>
                        <span class="control-label" style="float: left;margin-left: 10px;margin-right: 10px"> - </span>
                        <input type="text" style="width: 200px;float: left" class="form-control" id="endTime" placeholder="结束时间选择" name="endTime"/>
                    </div>
                </div>

                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>推荐：</span>
                    <div class="search-wrapper">
                        <a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="clickAffiliatedList('NFT', '1')">NFT</a>
                        <a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="clickAffiliatedList('组合购', '2')">组合购</a>
                        <a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="clickAffiliatedList('演出', '3')">演出</a>
                        <a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="clickAffiliatedList('商品', '4')">商品</a>
                    </div>
                </div>
            </div>
        </div>
        <input type="hidden" id="typeValue" value="1">
        <div class="bottomBtn">
            <a src="javascript:;" class="layui-layer-btn0 paddings" onclick="saveActivity()">确定</a>
            <a class="layui-layer-btn1 layui-layer-btn1" onclick="closeModal()">取消</a>
        </div>
    </div>

    <!-- modal -->
    <div id="addClassify2">
        <div class="selectClassify">
            <div class="addAvatarItem" style="padding: 20px;">
                <div class="selectClassifyOne" style="margin-bottom: 20px;">
                    <span class="span-label"><i style="color: red;">*</i>推荐商品：</span>
                    <div class="search-wrapper">
                        <div class="search-wrapper" style="margin-bottom: 15px;">
                            <!-- 关联 模糊搜索 -->
                            <div id="listRemoteSearch1"  placeholder="请搜索" name="seaarchSelect"></div>
                        </div>
                    </div>
                </div>
                <div class="selectClassifyOne">
                    <span class="span-label"><i style="color: red;">*</i>推荐类型：</span>
                    <select name="activeType" id="listActiveType1" class="form-control m-b" style="width: 170px;">
                        <option value="">所有</option>
                        <option value="1">NFT</option>
                        <option value="2">组合购</option>
                        <option value="3">演出</option>
                        <option value="4">商品</option>
                    </select>
                </div>
            </div>

        </div>
        <div class="bottomBtn">
            <a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="saveListAffiliated()">确定</a>
            <a class="btn btn-default btn-xs layui-layer-btn2" href="javascript:void(0)" onclick="closeModal2()">取消</a>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<th:block th:include="include :: bootstrap-fileinput-js" />
<script th:src="@{/js/searchSelect.js}"></script>

<script th:inline="javascript">
    let prefix = ctx + "stone/logs";
    let prefix2 = ctx + "local";
    let couType = [[${couType}]];
    let formData = new FormData()
    let listData = [] // 临时暂存 嵌套活动推荐
    let activeTypeChangeVal = 0 // 关联id select
    let activeTypeChangeValChild = 0 // list 中 关联id select
    let affiliatedUrl = ['', 'goblin/common/sku', 'goblin/common/mix', 'kylin/performances/roadShow/performance/status', 'compilations/goodsList']
    let currentUrl = ''
    let currentChildUrl = ''

    // 下线 modal
    let addActivityModalDom = ``

    let affiliatedChildModal = ``

    function clickAffiliatedList(val, index) {
        openAffiliatedModal(val, index)
    }

    // document 加载
    $(function () {
        var options = {
            url: '/kylin-recommend-active/pageActive',
            pagination: true,
            modalName: "关联",
            columns: [
                {
                    field: 'name',
                    title: '活动名称'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        let textArr = ['开启', '未开始'];
                        return textArr[value]
                    }
                },
                {
                    field: 'activeType',
                    title: '活动类型',
                    formatter: function (value, row, index) {
                        let textArr = ['NFT', '组合购', '演出', '商品'];
                        return textArr[value]
                    }
                },
                {
                    field: 'name',
                    title: '活动名称'
                },
                {
                    field: 'createdAt',
                    title: '创建时间',
                    formatter: function (value, row, index) {
                        return $.common.dateFormat(new Date(value), 'yyyy-MM-dd HH:mm:ss')
                    }
                },
                {
                    title: '操作',
                    // align: 'center',
                    formatter: function (value, row, index) {
                        let dataArr = [];
                        dataArr.push(`<span class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="toEdit('${row.mid}')"><i class="fa fa-edit"></i>编辑</span>`)
                        return dataArr.join('  ')
                    }
                }]
        };
        $.table.init(options);

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            autoclose: true
        });

        // 选择select
        $('#activeType').change(function () {
            activeTypeChangeVal = Number($(this).val()) // 1 2 3 4
            currentUrl = affiliatedUrl[activeTypeChangeVal]

        })

        $('#listActiveType1').change(function () {
            activeTypeChangeValChild = Number($(this).val()) // 1 2 3 4
            currentChildUrl = affiliatedUrl[activeTypeChangeValChild]
        })

        // 监听 模糊搜索
        let search = new searchSelect('#search', [], function(val) {
            let _params = {}
            if (activeTypeChangeVal === 1) {
                _params['type'] = 1
                _params['title'] = val
            } else if (activeTypeChangeVal === 2) { // 组合购
                _params['title'] = val
            } else if (activeTypeChangeVal === 3) { // 演出
                _params['status'] = '(3,6,7,9,10)'
                _params['title'] = val
            } else { // 商品
                _params['name'] = 1
                _params['title'] = val
            }
            $.ajax({
                type: 'get',
                url: ctx + currentUrl,
                contentType: "application/json;charset=UTF-8",
                data: _params,
                success: function(res) {
                    let forData = activeTypeChangeVal === 3 ? res.value : res.data
                    let _data = forData.map(item => {
                        if (activeTypeChangeVal === 1) { // NFT
                            return { id: item.spuId, value: item.spuName }
                        } else if (activeTypeChangeVal === 2) {
                            return { id: item.mixId, value: item.name }
                        } else if (activeTypeChangeVal === 3) {
                            return { id: item.performancesId, value: item.title }
                        } else { // 商品
                            return { id: item.spuId, value: item.name }
                        }
                    })
                    search.update(_data)

                }
            })
        })
        search.search(true)

        // 监听 模糊搜索 list
        let listRemoteSearch1 = new searchSelect('#listRemoteSearch1', [], function(val) {
            let _params = {}
            if (activeTypeChangeValChild === 1) {
                _params['type'] = 1
                _params['title'] = val
            } else if (activeTypeChangeValChild === 2) { // 组合购
                _params['title'] = val
            } else if (activeTypeChangeValChild === 3) { // 演出
                _params['status'] = '(3,6,7,9,10)'
                _params['title'] = val
            } else { // 商品
                _params['name'] = 1
                _params['title'] = val
            }
            $.ajax({
                type: 'get',
                url: ctx + currentChildUrl,
                contentType: 'application/json;charset=UTF-8',
                data: _params,
                success: function(res) {
                    let forData = activeTypeChangeValChild === 3 ? res.value : res.data
                    let _data = forData.map(item => {
                        if (activeTypeChangeValChild === 1) { // NFT
                            return { id: item.spuId, value: item.spuName }
                        } else if (activeTypeChangeValChild === 2) {
                            return { id: item.mixId, value: item.name }
                        } else if (activeTypeChangeValChild === 3) {
                            return { id: item.performancesId, value: item.title }
                        } else { // 商品
                            return { id: item.spuId, value: item.name }
                        }
                    })
                    listRemoteSearch1.update(_data)

                }
            })
        })
        listRemoteSearch1.search(true)


        // list 部分


    });

    // 保存
    function saveActivity () {
        let data = {
            activeName: $('#activeName').val(),
            activeBindId: $('#activeBindId').val(),
            startTime: $('#startTime').val(),
            endTime: $('#endTime').val(),
            activeType: Number($("#activeType").find("option:selected").val()),
            status: Number($("#status").find("option:selected").val()),
            list: listData
        }
        let isRequired = data.startTime &&data.endTime &&data.activeType
        console.log('saveActivity-params', data)
        if (isRequired) { // 必填ok
            $.ajax({
                url: `/kylin-recommend-active/addKylinRecommendActive`,
                type: 'post',
                headers: {"Content-Type": "application/json;charset=UTF-8"},
                data: JSON.stringify(data),
                success: function(res) {
                    if (res.code == 200) {
                        alert('新增成功')
                        layer.close(layer.index)
                    } else {
                        layer.msg(res.msg)
                    }
                }
            })
        } else {
            $.modal.msgWarning("拒绝原因为必填项！")
        }
    }

    // 保存关联活动内容
    function saveListAffiliated() {
        let _recommendBindId = document.getElementsByName("recommendBindId")[0].value
        let _type = $("#listActiveType1").find("option:selected").val() // 已经获取
        listData.push({
            recommendBindId: _recommendBindId,
            type: Number(_type)
        })
        layer.close(layer.index)
    }

    // 打开增加活动 modal
    function openAddActivityModal () {

        layer.open({
            type: 1,
            shade: .3,
            title: '增加活动', // 不显示标题
            content: $('#addClassify'), // 捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function(res){
                $("#addClassify").empty()
                // layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', {time: 5000, icon:6});
            }
        });
    }

    // 打开 list 选择 modal
    function openAffiliatedModal (val, index) {

        layer.open({
            type: 1,
            shade: .3,
            title: '增加关联推荐', // 不显示标题
            content: $('#addClassify2'), // 捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function(res){
                $("#addClassify2").empty()
                // layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', {time: 5000, icon:6});
            }
        });

        // 设置 代入的 点击
        $('#listActiveType1').find(`option[value=${index}]`).attr('selected', true)


    }

    // 跳转 编辑
    function toEdit (id) {
        let url = ctx + 'local/affiliatedActivityEdit?id=' + id;
        $.modal.openTab("关联活动编辑", url);
    }

    // 关闭模态框
    function closeModal () {
        $("#addClassify").empty()
        $(".layui-layer-close1").click();
    }

    // 关闭模态框2
    function closeModal2 () {
        $("#addClassify2").empty()
        layer.close(layer.index)
    }


    // ------------------------------------------------------------------------------------------------------
    function opendTicketList (id) {
        console.log(id, '121212121212activityTicketList')
        var url = prefix2+'/activityTicketList?id=' + id;
        $.modal.openTab("活动券列表", url);
    }

    // 导出数据
    function exportSelected() {
        // console.log(userIds.toString(), 'dataParam')
        $.modal.open('创建活动', prefix2 + "/createActivityModal", 500, 260, cancel)
    }

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