<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-CoreCmsPromotion-editForm" id="LAY-app-CoreCmsPromotion-editForm">
        <input type="hidden" name="id" id="id" value="{{d.params.data.model.id}}" />
        <input type="hidden" name="type" id="type" value="{{d.params.data.model.type }}" />
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">促销名称</label>
            <div class="layui-input-inline layui-inline-8">
                <input name="name" id="name" lay-verType="tips" lay-verify="required|verifyname" class="layui-input" placeholder="请输入促销名称" lay-reqText="请输入促销名称" value="{{d.params.data.model.name || '' }}" />
            </div>
            <div class="layui-form-mid layui-word-aux">请输入不超过40个字符内容</div>
        </div>
        <div class="layui-form-item" pane>
            <label for="isEnable" class="layui-form-label">是否开启</label>
            <div class="layui-input-block">
                <input type="checkbox" lay-filter="switch" name="isEnable" id="isEnable" {{ d.params.data.model.isEnable ? 'checked' : '' }} lay-skin="switch" lay-text="开启|关闭">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="sort" class="layui-form-label">权重</label>
            <div class="layui-input-inline  layui-inline-1">
                <input type="number" min="0" max="999999" name="sort" id="sort" lay-verType="tips" lay-verify="required|number" class="layui-input" value="{{d.params.data.model.sort || '' }}" placeholder="请输入排序" lay-reqText="请输入排序并为数字" />
            </div>
            <div class="layui-form-mid layui-word-aux">数字越小，权重越大</div>
        </div>
        <div class="layui-form-item">
            <label for="startTime" class="layui-form-label">起止时间</label>
            <div class="layui-input-inline layui-inline-6">
                <input name="startTime" id="createTime-CoreCmsPromotion-startTime" type="text" lay-verType="tips" lay-verify="required|datetime" class="layui-input" placeholder="请输入起止时间" lay-reqText="请输入起止时间" value="{{d.params.data.model.startTime}} 到 {{d.params.data.model.endTime }}" />
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label for="isExclusive" class="layui-form-label">是否排他</label>
            <div class="layui-input-block">
                <input type="checkbox" lay-filter="switch" name="isExclusive" id="isExclusive" {{ d.params.data.model.isExclusive ? 'checked' : '' }} lay-skin="switch" lay-text="开启|关闭">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                促销条件：
            </label>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-xs" style="margin-left: 10px;" lay-active="conditionAdd">添加条件</button>
                <table id="condition" style="margin-left: 10px;" lay-filter="condition"></table>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                促销结果：
            </label>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-xs" style="margin-left: 10px;" lay-active="resultAdd">添加结果</button>
                <table id="result" lay-filter="result"></table>
            </div>
        </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-CoreCmsPromotion-editForm-submit" id="LAY-app-CoreCmsPromotion-editForm-submit" value="确认编辑">
                </div>
            </div>
        </div>
    </div>
</script>
<script>
    var debug = layui.setter.debug;
    layui.data.sendParams = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg', 'util', 'view'],
            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , cropperImg = layui.cropperImg
                    , util = layui.util
                    , view = layui.view
                    , coreHelper = layui.coreHelper;

                //促销条件表格渲染
                var conditionTables = layui.table.render({
                    elem: '#condition'
                    , size: 'sm'
                    , url: "Api/CoreCmsPromotion/GetConditionList"
                    , where: { promotionId: d.params.data.model.id }
                    , method: 'POST'
                    , cols: [[ //标题栏
                        { field: 'code', width: 150, title: '条件代码' },
                        {
                            field: 'code', title: '条件名称', width: 150, templet: function (data) {
                                if (data.code) {
                                    for (var i = 0; i < d.params.data.promotionConditionTypes.length; i++) {
                                        if (data.code == d.params.data.promotionConditionTypes[i].sKey) {
                                            return d.params.data.promotionConditionTypes[i].sDescription;
                                        }
                                    }
                                }
                            }
                        }
                        , { field: 'parameters', title: '参数' }
                        , {
                            field: 'operating', title: '操作', width: 100, align: 'center', templet: function (data) {
                                var html = '';
                                html += '<a  class="layui-btn layui-btn-xs option-edit" data-id="' + data.id + '" lay-active="conditionEdit">编辑</a>';
                                html += '<a  class="layui-btn layui-btn-xs option-del" data-id="' + data.id + '" lay-active="conditionDetele">删除</a>';
                                return html;
                            }
                        }
                    ]]
                    , even: true
                });

                //促销结果表格渲染
                var resultTables = layui.table.render({
                    elem: '#result'
                    , size: 'sm'
                    , url: "Api/CoreCmsPromotion/GetResultList"
                    , where: { promotionId: d.params.data.model.id }
                    , method: 'POST'
                    , cols: [[ //标题栏
                        { field: 'code', width: 150, title: '结果代码' },
                        {
                            field: 'code', title: '条件名称', width: 150, templet: function (data) {
                                if (data.code) {
                                    for (var i = 0; i < d.params.data.promotionResultTypes.length; i++) {
                                        if (data.code == d.params.data.promotionResultTypes[i].sKey) {
                                            return d.params.data.promotionResultTypes[i].sDescription;
                                        }
                                    }
                                }
                            }
                        }, { field: 'parameters', title: '参数' }
                        , {
                            field: 'operating', title: '操作', width: 100, align: 'center', templet: function (data) {
                                var html = '';
                                html += '<a  class="layui-btn layui-btn-xs option-result-edit"  lay-active="resultEdit" data-id="' + data.id + '">编辑</a>';
                                html += '<a  class="layui-btn layui-btn-xs option-result-del" lay-active="resultDelete" data-id="' + data.id + '">删除</a>';
                                return html;
                            }
                        }
                    ]]
                    //,skin: 'line' //表格风格
                    , even: true
                    //,page: true //是否显示分页
                    //,limits: [5, 7, 10]
                    //,limit: 5 //每页默认显示的数量
                });

                //处理属性 为 lay-active 的所有元素事件
                util.event('lay-active', {
                    //添加促销条件
                    conditionAdd: function () {
                        coreHelper.Post("Api/CoreCmsPromotion/GetConditionCreate", { id: d.params.data.model.id }, function (e) {
                            if (e.code === 0) {
                                admin.popup({
                                    shadeClose: false,
                                    title: '创建数据',
                                    area: ['600px', '400px'],
                                    id: 'LAY-popup-CoreCmsPromotionCondition-GetConditionCreate',
                                    success: function (layero, index) {
                                        view(this.id).render('promotion/promotion/conditioncreate', { data: e.data }).done(function () {
                                            form.render();
                                            //监听提交
                                            form.on('submit(LAY-app-CoreCmsPromotionCondition-createForm-submit)',
                                                function (data) {
                                                    var field = data.field; //获取提交的字段

                                                    if (!!!field.code) {
                                                        layer.msg("请先选择促销条件");
                                                        return false;
                                                    }
                                                    var postData = {};
                                                    var parameters = {};
                                                    postData.promotionId = field.promotionId;
                                                    postData.code = field.code;

                                                    if (field.code === 'GOODS_ALL') {//所有商品满足条件
                                                        parameters.nums = field.nums;
                                                    } else if (field.code === 'GOODS_IDS') { //指定某些商品满足条件
                                                        if (!!!field.goods) {
                                                            layer.msg("请选择商品");
                                                            return false;
                                                        }
                                                        parameters.goodsId = field.goods;
                                                        parameters.nums = field.nums;
                                                    } else if (field.code === 'GOODS_CATS') {//指定商品分类满足条件
                                                        var catId = field.goodsSelectCategorytTree_select_nodeId
                                                        if (!!!catId) {
                                                            layer.msg("请选择商品分类");
                                                            return false;
                                                        }
                                                        parameters.catId = catId;
                                                        parameters.nums = field.nums;

                                                    } else if (field.code === 'GOODS_BRANDS') {//指定商品品牌满足条件
                                                        if (!!!field.brandId) {
                                                            layer.msg("请选择商品品牌");
                                                            return false;
                                                        }
                                                        parameters.brandId = field.brandId;
                                                        parameters.nums = field.nums;
                                                    } else if (field.code === 'ORDER_FULL') {//订单满XX金额满足条件
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'USER_GRADE') {//用户符合指定等级
                                                        if (!!!field.grades) {
                                                            layer.msg("请选择会员级别");
                                                            return false;
                                                        }
                                                        parameters.grades = field.grades;
                                                    }
                                                    postData.parameters = JSON.stringify(parameters);
                                                    if (debug) { console.log(field); console.log(postData); } //开启调试返回数据
                                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                    coreHelper.Post("Api/CoreCmsPromotion/DoConditionCreate", postData, function (e) {
                                                        console.log(e)
                                                        if (e.code === 0) {
                                                            //table刷新即可
                                                            conditionTables.reload();
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                                });

                                        });
                                    }
                                });
                            } else {
                                layer.msg(e.msg);
                            }
                        });
                    }
                    //编辑促销条件
                    , conditionEdit: function () {
                        var id = $(this).attr('data-id');
                        coreHelper.Post("Api/CoreCmsPromotion/GetConditionEdit", { id: id }, function (e) {
                            if (e.code === 0) {
                                admin.popup({
                                    shadeClose: false,
                                    title: '编辑数据',
                                    area: ['600px', '400px'],
                                    id: 'LAY-popup-CoreCmsPromotionCondition-GetConditionEdit',
                                    success: function (layero, index) {
                                        view(this.id).render('promotion/promotion/conditionedit', { data: e.data }).done(function () {
                                            form.render();
                                            //监听提交
                                            form.on('submit(LAY-app-CoreCmsPromotionCondition-editForm-submit)',
                                                function (data) {
                                                    var field = data.field; //获取提交的字段
                                                    if (!!!field.code) {
                                                        layer.msg("请先选择促销条件");
                                                        return false;
                                                    }
                                                    var postData = {};
                                                    var parameters = {};
                                                    postData.promotionId = field.promotionId;
                                                    postData.code = field.code;
                                                    postData.id = field.id;

                                                    if (field.code === 'GOODS_ALL') {//所有商品满足条件
                                                        parameters.nums = field.nums;
                                                    } else if (field.code === 'GOODS_IDS') { //指定某些商品满足条件
                                                        if (!!!field.goods) {
                                                            layer.msg("请选择商品");
                                                            return false;
                                                        }
                                                        parameters.goodsId = field.goods;
                                                        parameters.nums = field.nums;
                                                    } else if (field.code === 'GOODS_CATS') {//指定商品分类满足条件
                                                        var catId = field.goodsSelectCategorytTree_select_nodeId
                                                        if (!!!catId) {
                                                            layer.msg("请选择商品分类");
                                                            return false;
                                                        }
                                                        parameters.catId = catId;
                                                        parameters.nums = field.nums;

                                                    } else if (field.code === 'GOODS_BRANDS') {//指定商品品牌满足条件
                                                        if (!!!field.brandId) {
                                                            layer.msg("请选择商品品牌");
                                                            return false;
                                                        }
                                                        parameters.brandId = field.brandId;
                                                        parameters.nums = field.nums;
                                                    } else if (field.code === 'ORDER_FULL') {//订单满XX金额满足条件
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'USER_GRADE') {//用户符合指定等级
                                                        if (!!!field.grades) {
                                                            layer.msg("请选择会员级别");
                                                            return false;
                                                        }
                                                        parameters.grades = field.grades;
                                                    }
                                                    postData.parameters = JSON.stringify(parameters);
                                                    if (debug) { console.log(field); console.log(postData); } //开启调试返回数据
                                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                    coreHelper.Post("Api/CoreCmsPromotion/DoConditionEdit", postData, function (e) {
                                                        console.log(e)
                                                        if (e.code === 0) {
                                                            conditionTables.reload();
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                                });
                                        })
                                    }
                                });
                            } else {
                                layer.msg(e.msg);
                            }
                        });

                    }
                    //删除促销条件
                    , conditionDetele: function () {
                        var id = $(this).attr('data-id');
                        layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                            coreHelper.Post("Api/CoreCmsPromotion/DoConditionDelete", { id: id }, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                conditionTables.reload();
                                layer.msg(e.msg);
                            });
                        });
                    }
                    //添加促销结果
                    , resultAdd: function () {
                        coreHelper.Post("Api/CoreCmsPromotion/GetResultCreate", { id: d.params.data.model.id }, function (e) {
                            if (e.code === 0) {
                                admin.popup({
                                    shadeClose: false,
                                    title: '创建数据',
                                    area: ['600px', '400px'],
                                    id: 'LAY-popup-CoreCmsPromotionResult-create',
                                    success: function (layero, index) {
                                        view(this.id).render('promotion/promotion/resultcreate', { data: e.data }).done(function () {
                                            //监听提交
                                            form.on('submit(LAY-app-CoreCmsPromotionResult-createForm-submit)',
                                                function (data) {
                                                    var field = data.field; //获取提交的字段

                                                    if (!!!field.code) {
                                                        layer.msg("请先选择促销条件");
                                                        return false;
                                                    }
                                                    var postData = {};
                                                    var parameters = {};
                                                    postData.promotionId = field.promotionId;
                                                    postData.code = field.code;

                                                    if (field.code === 'GOODS_REDUCE') {//指定商品减固定金额
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'GOODS_DISCOUNT') { //指定商品打X折
                                                        if (!!!field.discount) {
                                                            layer.msg("请输入折扣");
                                                            return false;
                                                        }
                                                        parameters.discount = field.discount;
                                                    } else if (field.code === 'GOODS_ONE_PRICE') {//指定商品一口价
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'ORDER_REDUCE') {//订单减指定金额
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'ORDER_DISCOUNT') {//订单打X折
                                                        if (!!!field.discount) {
                                                            layer.msg("请输入折扣");
                                                            return false;
                                                        }
                                                        parameters.discount = field.discount;
                                                    } else if (field.code === 'GOODS_HALF_PRICE') {//指定商品每第几件减指定金额
                                                        if (!!!field.num) {
                                                            layer.msg("请输入第几件");
                                                            return false;
                                                        }
                                                        parameters.num = field.num;
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    }
                                                    postData.parameters = JSON.stringify(parameters);
                                                    if (debug) { console.log(field); console.log(postData); } //开启调试返回数据
                                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                    coreHelper.Post("Api/CoreCmsPromotion/DoResultCreate", postData, function (e) {
                                                        if (e.code === 0) {
                                                            resultTables.reload();
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                                });
                                        });
                                    }
                                });
                            } else {
                                layer.msg(e.msg);
                            }
                        });
                    }
                    //促销结果编辑
                    , resultEdit: function () {
                        var id = $(this).attr('data-id');
                        coreHelper.Post("Api/CoreCmsPromotion/GetResultEdit", { id: id }, function (e) {
                            if (e.code === 0) {
                                admin.popup({
                                    shadeClose: false,
                                    title: '编辑促销结果',
                                    area: ['600px', '400px'],
                                    id: 'LAY-popup-CoreCmsPromotionResult-edit',
                                    success: function (layero, index) {
                                        view(this.id).render('promotion/promotion/resultedit', { data: e.data }).done(function () {
                                            //监听提交
                                            form.on('submit(LAY-app-CoreCmsPromotionResult-editForm-submit)',
                                                function (data) {
                                                    var field = data.field; //获取提交的字段
                                                    if (!!!field.code) {
                                                        layer.msg("请先选择促销条件");
                                                        return false;
                                                    }
                                                    var postData = {};
                                                    var parameters = {};
                                                    postData.promotionId = field.promotionId;
                                                    postData.code = field.code;
                                                    postData.id = field.id;

                                                    if (field.code === 'GOODS_REDUCE') {//指定商品减固定金额
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'GOODS_DISCOUNT') { //指定商品打X折
                                                        if (!!!field.discount) {
                                                            layer.msg("请输入折扣");
                                                            return false;
                                                        }
                                                        parameters.discount = field.discount;
                                                    } else if (field.code === 'GOODS_ONE_PRICE') {//指定商品一口价
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'ORDER_REDUCE') {//订单减指定金额
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    } else if (field.code === 'ORDER_DISCOUNT') {//订单打X折
                                                        if (!!!field.discount) {
                                                            layer.msg("请输入折扣");
                                                            return false;
                                                        }
                                                        parameters.discount = field.discount;
                                                    } else if (field.code === 'GOODS_HALF_PRICE') {//指定商品每第几件减指定金额
                                                        if (!!!field.num) {
                                                            layer.msg("请输入第几件");
                                                            return false;
                                                        }
                                                        parameters.num = field.num;
                                                        if (!!!field.money) {
                                                            layer.msg("请输入金额");
                                                            return false;
                                                        }
                                                        parameters.money = field.money;
                                                    }
                                                    postData.parameters = JSON.stringify(parameters);
                                                    if (debug) { console.log(field); console.log(postData); } //开启调试返回数据
                                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                    coreHelper.Post("Api/CoreCmsPromotion/DoResultEdit", postData, function (e) {
                                                        if (e.code === 0) {
                                                            resultTables.reload();
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                                });
                                        })
                                    }
                                });
                            } else {
                                layer.msg(e.msg);
                            }
                        });
                    }
                    //删除促销结果
                    , resultDelete: function () {
                        var id = $(this).attr('data-id');
                        layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                            coreHelper.Post("Api/CoreCmsPromotion/DoResultDelete", { id: id }, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                resultTables.reload();
                                layer.msg(e.msg);
                            });
                        });
                    }

                });

                laydate.render({
                    elem: '#createTime-CoreCmsPromotion-startTime',
                    type: 'datetime',
                    range: '到',
                });
                form.verify({
                    verifyname: [/^[\S]{0,40}$/, '促销名称最大只允许输入40位字符，且不能出现空格'],
                });

                //重载form
                form.render(null, 'LAY-app-CoreCmsPromotion-editForm');
            })
    };
</script>