<script type="text/html" template lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-form" lay-filter="LAY-app-CoreCmsPromotionCondition-createForm" id="LAY-app-CoreCmsPromotionCondition-createForm">
        <input type="hidden" name="promotionId" id="promotionId" value="{{d.params.data.promotionModel.id}}" />
        <div class="layui-form-item">
            <label class="layui-form-label">促销条件：</label>
            <div class="layui-input-block">
                <select name="code" id="code" lay-filter="conditionCode">
                    <option value="">请选择促销条件</option>
                    {{# layui.each(d.params.data.conditionCodes, function(index, item){ }}
                    <option value="{{ item.sKey }}">{{ item.sDescription }}</option>
                    {{# }); }}
                </select>
            </div>
        </div>
        <div id="conditionBox"></div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <div class="layui-footer">
                    <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsPromotionCondition-createForm-submit" id="LAY-app-CoreCmsPromotionCondition-createForm-submit" value="确认添加">
                </div>
            </div>
        </div>
    </div>
</script>

<script id="goods_all_tpl" type="text/html">
    <input type="hidden" name="nums" value="1" />
    <div class="layui-form-item">
        <label class="layui-form-label">条件名称：</label>
        <div class="layui-input-inline">
            <div class="layui-form-mid">所有商品都满足条件</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">条件：</label>
        <div class="layui-form-mid">
            无需设置任何条件，直接点击“完成”吧。
        </div>
    </div>
</script>

<script id="goods_cats_tpl" type="text/html">
    <div class="layui-form-item">
        <label class="layui-form-label">条件名称：</label>
        <div class="layui-input-inline">
            <div class="layui-form-mid">指定商品分类</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品分类：</label>
        <div class="layui-input-block">
            <ul id="goodsSelectCategorytTree" class="dtree" data-id="0" data-name="parentId"></ul>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品数量：</label>
        <div class="layui-input-inline seller-inline-2">
            <input name="nums" lay-verType="tips" lay-verify="required|number" autocomplete="off" value="1" placeholder="商品数量" class="layui-input" type="text">
        </div>
        <div class="layui-form-mid layui-word-aux">大于等于此商品数量才满足条件</div>
    </div>
</script>

<script id="goodsIds_tpl" type="text/html">
    <div class="layui-form-item">
        <label class="layui-form-label">条件名称：</label>
        <div class="layui-input-inline">
            <div class="layui-form-mid">指定商品id</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品id：</label>
        <div class="layui-input-block">
            <div id="goods_box" class="select_seller_goods_box">
                <div>
                    <button class="layui-btn" lay-active="goods_show"><i class="layui-icon layui-icon-ok"></i>选择商品</button>
                </div>
                <input type="hidden" name="goods" id="goods" value="">
                <ul id="goods_list" class="sellect_seller_goods_list">
                </ul>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品数量：</label>
        <div class="layui-input-inline seller-inline-2">
            <input name="nums" lay-verType="tips" lay-verify="required|number" autocomplete="off" value="1" placeholder="商品数量" class="layui-input" type="text">
        </div>
        <div class="layui-form-mid layui-word-aux">大于等于此商品数量才满足条件</div>
    </div>
</script>

<script id="goods_brands_tpl" type="text/html">
    <div class="layui-form-item">
        <label class="layui-form-label">条件名称：</label>
        <div class="layui-input-inline">
            <div class="layui-form-mid">指定商品品牌</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品品牌：</label>
        <div class="layui-input-inline">
            <select name="brandId" id="brandId">
                <option value="">请选择品牌</option>
                {{# layui.each(d, function(index, item){ }}
                <option value="{{ item.id }}">{{ item.name }}</option>
                {{# }); }}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品数量：</label>
        <div class="layui-input-inline seller-inline-2">
            <input name="nums" lay-verType="tips" lay-verify="required|number" autocomplete="off" value="1" placeholder="商品数量" class="layui-input" type="text">
        </div>
        <div class="layui-form-mid layui-word-aux">大于等于此商品数量才满足条件</div>
    </div>
</script>

<script id="order_full_tpl" type="text/html">
    <div class="layui-form-item">
        <label class="layui-form-label">条件名称：</label>
        <div class="layui-input-inline">
            <div class="layui-form-mid">满减</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">满多少：</label>
        <div class="layui-input-inline seller-inline-2">
            <input name="money" lay-verType="tips" lay-verify="required|money" autocomplete="off" value="" placeholder="金额" class="layui-input" type="text">
        </div>
        <div class="layui-form-mid layui-word-aux">订单金额满多少的时候，优惠</div>
    </div>
</script>

<script id="user_grade_tpl" type="text/html">
    <div class="layui-form-item">
        <label class="layui-form-label">条件名称：</label>
        <div class="layui-input-inline">
            <div class="layui-form-mid">指定用户等级</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><i class="required-color">*</i>请选择：</label>
        <div class="layui-input-block">
            <div id="grades-select" class="xm-select-demo"></div>
        </div>
    </div>
</script>

<script>
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'form', 'coreHelper', 'laytpl', 'view', 'dtree', 'util', 'table', 'xmSelect'],
            function () {
                var $ = layui.$, form = layui.form, admin = layui.admin, view = layui.view, laytpl = layui.laytpl, dtree = layui.dtree, coreHelper = layui.coreHelper, util = layui.util, table = layui.table, xmSelect = layui.xmSelect;

                form.on('select(conditionCode)', function (data) {
                    $('#code').val(data.value);
                    var view = document.getElementById('conditionBox');
                    $("#conditionBox").empty();
                    if (data.value === 'GOODS_ALL') {//所有商品满足条件
                        var getTpl = goods_all_tpl.innerHTML;
                        laytpl(getTpl).render(d.params.data.promotionModel, function (html) { view.innerHTML = html; });
                    } else if (data.value === 'GOODS_IDS') { //指定某些商品满足条件
                        var getTpl = goodsIds_tpl.innerHTML;
                        laytpl(getTpl).render(d.params.data.promotionModel, function (html) { view.innerHTML = html; });
                    } else if (data.value === 'GOODS_CATS') {//指定商品分类满足条件
                        var getTpl = goods_cats_tpl.innerHTML;
                        laytpl(getTpl).render(d.params.data.promotionModel, function (html) { view.innerHTML = html; });
                        dtree.renderSelect({
                            elem: "#goodsSelectCategorytTree",
                            width: "100%",
                            data: d.params.data.categories.data,
                            done: function (res, $ul, first) {
                                if (first) {
                                    dtree.dataInit("goodsSelectCategorytTree", '0');
                                    dtree.selectVal("goodsSelectCategorytTree", '0');
                                }
                            }
                        });
                    } else if (data.value === 'GOODS_BRANDS') {//指定商品品牌满足条件
                        var getTpl = goods_brands_tpl.innerHTML;
                        laytpl(getTpl).render(d.params.data.brands, function (html) { view.innerHTML = html; });
                    } else if (data.value === 'ORDER_FULL') {//订单满XX金额满足条件
                        var getTpl = order_full_tpl.innerHTML;
                        laytpl(getTpl).render(d.params.data.promotionModel, function (html) { view.innerHTML = html; });
                    } else if (data.value === 'USER_GRADE') {//用户符合指定等级
                        var getTpl = user_grade_tpl.innerHTML;
                        laytpl(getTpl).render(d.params.data.promotionModel, function (html) { view.innerHTML = html; });
                        xmSelect.render({
                            el: '#grades-select',
                            tips: '请选择用户类别?',
                            name: 'grades',
                            prop: {
                                name: 'title',
                                value: 'id',
                            },
                            data: d.params.data.grades
                        })
                    }
                    form.render();
                });

                //处理属性 为 lay-active 的所有元素事件
                util.event('lay-active', {
                    goods_show: function () {
                        admin.popup({ shadeClose: false,
                            title: '选择商品',
                            area: ['750px', '90%'],
                            id: 'LAY-app-CoreCmsPinTuanRule-createForm-GetGoodsList',
                            success: function (layero, index) {
                                view(this.id).render('common/getGoodIds', null).done(function () {
                                    form.render();
                                    //监听商品列表页工具条

                                    //table.on('tool(LAY-app-CoreCmsGoods-GoodsTable-TableBox)',
                                    //    function (obj) {
                                    //        var data = obj.data; //获得当前行数据
                                    //        $("#goods_list").empty();
                                    //        $("#goods_list").append('<li><span id="' + data.id + '"  lay-active="goods_delete">×</span>' + data.name + '</li>');
                                    //        $("#goods").val(data.id);
                                    //        layer.close(index);
                                    //    });

                                    //监听商品列表页工具条
                                    form.on('submit(LAY-app-CoreCmsGoods-getData)',
                                        function (data) {
                                            if (Object.getOwnPropertyNames(ids).length === 0) {
                                                layer.msg("请先选择商品");
                                                return;
                                            }
                                            //判断个数是否满足
                                            if (Object.getOwnPropertyNames(ids).length > 10) {
                                                layer.msg("最多只能选择" + 10 + "个");
                                                return false;
                                            }
                                            $("#goods_list").empty();
                                            var the_val = "";
                                            for (var key in ids) {
                                                $("#goods_list").append('<li><span id="' + key + '"  lay-active="goodsDelete">×</span>' + ids[key].name + '</li>');
                                                the_val += "," + key;
                                            }
                                            $("#goods").val(the_val.slice(1));
                                            layer.close(index);
                                        });

                                });
                            }
                        });
                    },
                    goods_delete: function () {
                        var ids_array = $("#goods").val().split(",");
                        for (var i = 0; i < ids_array.length; i++) {
                            if (ids_array[i] == $(this).attr("id")) {
                                ids_array.splice(i, 1);
                            }
                        }
                        $("#goods").val(ids_array.join(","));
                        $(this).parent().remove();
                    }
                });

                form.verify({
                    money: [/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/, '请输入金额'],
                    verifycode: [/^[\S]{0,50}$/, '促销条件编码最大只允许输入50位字符，且不能出现空格'],
                });
                //重载form
                form.render(null, 'LAY-app-CoreCmsPromotionCondition-createForm');
            })
    };
</script>