<!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" />
    <link rel="stylesheet" href="">
    <style>
        .layui-form {
            padding: 20px;
        }
        .main_title, .main_type, .money, .scope_application, .sendType,.phoneNumber,.discount {
            margin-bottom: 20px;
        }
        .discount {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }
        .discount input {
            width: 60px;
        }
        .sendType,.phoneNumber {
            display: none;
        }
        .labelName {
            display: inline-block;
            width: 100px;
            text-align: right;
        }
        input {
            border: 1px solid #ddd;
            border-radius: 4px;
            background: transparent;
            outline: none;
            padding-left: 5px;
            height: 30px;
        }
        .main_title input {
            width: 200px;
        }
        .prompt {
            margin-left: 100px !important;
        }

        .main_type input, .sendType input {
            margin-top: -2px;
            margin-right: 5px;
        }
        .main_type span, .sendType span {
            margin-right: 12px;
            line-height: 28px;
        }
        .layui-form .money input {
            width: 60px;
        }
        .changeData .dataOne {
            margin-bottom: 20px;
        }
        .changeData .dataOne p {
            margin: 12px 0 0 20px;
            color: #ccc;
        }
        .changeData .dataOne input {
            width: 60px;
        }
        .main_bottom .describe, .main_bottom .sendTime {
            margin-bottom: 20px;
        }
        .describe,.main_type {
            display: flex;
        }
        .describe textarea,.phoneNumber textarea {
            width: 400px;
        }
        .sendTime {
            display: flex;
        }
        .sendTime input {
            margin-top: -6px;
            margin-right: 5px;
        }
        .bottom_btn {
            width: 25%;
            text-align: center;
        }
        select {
            border: 1px solid #ddd;
            border-radius: 4px;
            background: transparent;
            outline: none;
            height: 30px;
            width: 200px;
        }
        .search_ipt ul,.search_ipt li{
            padding: 0;
            margin: 0;
            font-style: normal;
            list-style: none;
        }
        .search_ipt ul {
            display: none;
            position: absolute;
            left: 0;
            top: 26px;
            padding: 5px 0;
            z-index: 899;
            min-width: 200px;
            border: 1px solid #eee;
            max-height: 300px;
            overflow-y: auto;
            background-color: #fff;
            border-radius: 2px;
            box-sizing: border-box;
            box-shadow: 1px 1px 4px rgb(0 0 0 / 8%);
        }
        .search_ipt li {
            background-color: #fff;
            cursor: pointer;
            padding-left: 10px!important;
            color: #999;
            padding: 0 10px;
            line-height: 36px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .search_ipt li:hover {
            background: #F6F6F6;
        }
        .search_ipt .layui-input {
            width: 200px;
        }
        .required {
            font-style: normal;
            color: red;
        }
        #iptModal {
            display: none;
        }
    </style>
</head>
<body>
<form id="form-user-add"  class="layui-form">
    <div class="main_title">
        <span class="labelName"><i class="required">*</i>名称：</span><input type="text" id="title" name="keyword" placeholder="券名称" required />
    </div>
    <div class="main_type">
        <span class="labelName">类别：</span>
        <div class="layui-input-block" style="display: flex">
            <input type="radio" value="2" class="typeOne" name="typeOne" checked><span>商品</span>
        </div>

    </div>
    <div class="main_type">
        <span class="labelName">类型：</span>
        <div class="layui-input-block" style="display: flex">
            <input type="radio" value="0" class="typeTwo" name="typeTwo" checked><span>用户输入兑换</span>
            <input type="radio" value="1" class="typeTwo" name="typeTwo"><span>发送至“我的券”</span>
            <input type="radio" value="2" class="typeTwo" name="typeTwo"><span>积分兑换</span>
        </div>
    </div>
    <div class="scope_application" id="scope_application">
        <span class="labelName">适用范围：</span>
        <div class="search_ipt" style="position: relative;display: inline;">
            <input type="text" id="searchIpt" autocomplete="off" class="layui-input" placeholder="全部">
            <ul id="dataList" style="position: absolute;"  onclick="selectGoods()"></ul>
        </div>
    </div>
    <div class="discount">
        <span class="labelName"><i class="required">*</i>折扣：</span>
        <div class="discount_item">
            <input type="text" id="discount" autocomplete="off" class="layui-input" placeholder="">  折
        </div>
    </div>
    <div class="changeData">
        <div id="modalOne">
            <div id="dataOne1" class="dataOne">
                <span class="labelName"><i class="required">*</i>数量：</span>
                <input type="text" value="1" id="num" />
                张
            </div>
            <div class="dataOne">
                <span class="labelName"><i class="required">*</i>券的有效期：</span>
                <input type="text" id="ticketTime" />
                天
                <p class="prompt">*可兑换的时间</p>
            </div>
            <div class="dataOne" id="exchange">
                <span class="labelName"><i class="required">*</i>兑换有效期：</span>
                <input type="text" id="exchangeTime" />
                天
                <p class="prompt">*兑换后的有效时间</p>
            </div>
        </div>
    </div>
    <div class="sendType">
        <div style="display: flex;align-items: center;">
            <span class="labelName">发送类型：</span>
            <div class="layui-input-block" style="display: flex">
<!--                <input type="radio" value="10" class="sendType" name="sendType" checked><span>全体用户</span>-->
                <input type="radio" value="1" class="sendType" name="sendType" checked><span>会员</span>
                <input type="radio" value="2" class="sendType" name="sendType"><span>手机号</span>
            </div>
        </div>
    </div>
    <div class="phoneNumber">
        <span style="display:inline-block;margin-bottom: 12px;"><i class="required">*</i>请输入手机号<span style="color: #ccc">（手机号可填多个，使用"，"隔开）</span>：</span>
        <textarea id="phoneNum" class="form-control" rows="4" style="margin-left: 100px"></textarea>
    </div>
    <div class="main_bottom">
        <div class="describe">
                <span class="labelName">
                    <i class="required">*</i>描述说明：
                </span>
            <textarea id="describeTxt" class="form-control" rows="4"></textarea>
        </div>
        <div class="bottom_btn">
            <!-- <button type="button" class="layui-btn btn btn-primary" lay-filter="formDemo" onclick="create()">创建代金券</button> -->
            <button type="button" class="btn btn-primary" onclick="create()">创建折扣券</button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-suggest-js"/>
</body>
<script th:inline="javascript">
    var phpMallUrl = [[${phpMallUrl}]];
    var roadShowId = '[[${roadShowId}]]'.replaceAll("\"", "");
    let prefix = ctx;
    let typeOne = '2'; // 类别
    let typeTwo = '0'; // 类型
    let sendType = '1'; // 发送类型
    let searchVal = ''; // 输入框内数据
    let searchData = []; // 搜索总数据
    let showId = ''; // 选中演出id
    let sendTimeType = '';
    function getMallList() {
        let data = {
            name: $('#searchIpt').val()
        }
        let str = '';
        promiseMethods('/compilations/goodsList','get',data).then(res=>{
            if (res.data.length>0) {
                searchData = [...res.data];
                searchData.forEach((item,index)=>{
                    str+= `<li class="selectData" onclick="selectOne('${item.spuId}','${item.name}')">${item.name}</li>`
                })
                $('#dataList').html(str);
            } else {
                $('#dataList').html('<li style="width:100%;text-align:center;color:#ccc;">--- 暂无数据 ---</li>');
            }
        })
    };
    function selectOne(id,name) {
        searchData.some(item => {
            if (item.spuId == id) {
                return $("#searchIpt").val(item.name);
            }
        })
        showId = id;
    };
    $("input[name=typeTwo]").change(function(e){
        typeTwo = e.target.value;
        if (typeTwo == 1) {
            $('#unit').text('张/账户')
            $('.sendType').show();
            $('#exchangeTime').val('')
            $('#exchange').hide();
            $('#dataOne1').show();
            if (sendType == 2) {
                $('.phoneNumber').show();
            }
        } else if (typeTwo == 2) {
            $('#unit').text('张')
            $('#exchange').hide();
            $('.sendType').hide();
            $('.phoneNumber').hide();
            $('.phoneNum').val('');
            $('#dataOne1').hide();
        } else {
            $('#unit').text('张')
            $('#exchange').show();
            $('.sendType').hide();
            $('.phoneNumber').hide();
            $('.phoneNum').val('');
            $('#dataOne1').show();
        }
    });
    $("#searchIpt").blur(function(){
        getMallList()
        $('#dataList').show()
    });
    // $("#searchIpt").blur(function(){
    //     setTimeout(() => {
    //         $('#dataList').hide()
    //     }, 500);
    // });
    function selectGoods(){
        $('#dataList').hide()
    }
    $("#searchIpt").keyup(function(){
        searchVal = $("#searchIpt").val()
        getMallList()
    });
    $("input[name=sendType]").change(function(e){
        sendType = e.target.value;
        if (e.target.value == 2) {
            $('.phoneNumber').show();
        } else {
            $('.phoneNumber').hide();
        }
    });
    function create() {
        if (!$('#title').val() || !$('#num').val() || !$('#ticketTime').val() || !$('#discount').val() || !$('#describeTxt').val()) {
            return layer.msg('请将必填项输入完整～！');
            if (typeTwo == 0) {
                if (!$('#exchangeTime').val()) {
                    return layer.msg('请将必填项输入完整～！');
                }
            }
            if (sendType == 2) {
                if (!$('.phoneNumber').val()) {
                    return layer.msg('请将必填项输入完整～！');
                };
            }
        }
        if ($("#describeTxt").val().match(/^\s+$/)) {
            return layer.msg('描述说明不可全为空格！');
        }
        let useScope = 100;
        let busiName = '';
        let busiId = '';
        if (!$('#searchIpt').val()) {
            showId = '';
        }
        if (showId) {
            useScope = 80;
            busiName = $('#searchIpt').val();
            busiId = showId;
        }
        let data = {
            "bindType": Number(typeTwo), // 兑换券/发放
            "busiType": 2, // 业务类别
            "couType": 4, // 券类型
            "couponRuleList": [ // 适用范围[100-全场｜90-演出｜91-场次｜92-票｜80-商品｜81-款式]
                {
                "useScope": useScope,
                "busiName": busiName || '全部商品',
                "busiId": busiId
                }
            ],
            "discount": $('#discount').val() / 10, // 折扣券需传
            "eventAmt": Number($('#num').val()), // 数量
            "eventLimit": $('#phoneNum').val(), // 手机号
            "eventType": Number(sendType),
            "label": "标注", // 不需管
            "notice": $('#describeTxt').val(),
            "redeemValidity": Number($('#exchangeTime').val()), // 兑换有效期
            "eventAt": $.common.dateFormat(new Date(), 'yyyy-MM-dd HH:mm:ss'),
            "title": $('#title').val(),
            "validity": Number($('#ticketTime').val()),
        }
        if (typeTwo == 1) {
            delete data.redeemValidity
        } else {
            delete data.eventType
        }
        if (sendType != 2) {
            delete data.eventLimit;
        }
        promiseMethods('/candy/coupon/mgt/add','post',JSON.stringify(data),'application/json').then(res=>{
            layer.msg('' + res);
            if (res.code != undefined && res.code != web_status.SUCCESS) {
                $.modal.alertWarning(res.msg);
                return [];
            } else if (res.code == web_status.SUCCESS) {
                $.operate.successTabCallback(res);
                closeItem();
            }
        })
    }
    function promiseMethods(url,type,data,contentType) {
        return new Promise((resolve,reject)=>{
            $.ajax({
                url,
                type,
                data,
                contentType,
                success:function(res) {
                    resolve(res)
                }
            })
        })
    }
</script>
</html>
