Edit.js 6.67 KB
var $ = require('jquery');
var edit = require('../../../common/edit');
var common = require('../../../common/common');
var couponBean = require('../partials/promotionCode');

var e = new edit("#basicForm");
var type = $("#basicForm").attr("role");//类型
var userSourceLimit = [];
var userTypeLimit = [];
/**
 * 监听输入值的变化
 */
$(document).on("change", ".observe", function () {
    var $this = $(this);
    var name = $this.data("field");
    couponBean = common.util.__buildobj(name, '.', couponBean, function (obj, name) {
        obj[name] = $this.val();
    });
});
if (type == "info") {
    e.on("render", function () {
        $("input:text", "#basicForm")
            .add("select", "#basicForm")
            .add(":radio", "#basicForm")
            .add(":checkbox", "#basicForm").prop("disabled", true);
        $("textarea", "#basicForm").prop("disabled", true);

        $(".panel-footer").hide();
    });
}
/**
 * 全局对象
 */
var Bll = {
    validateCode: function () {
        var result = false;
        $.ajax({
            type: 'POST',
            url: '/market/coupon/checkPromotionCode',
            async: false,
            data: {
                code: $("#code").val()
            },
            dataType: 'json'
        }).then(function (data) {
            if (data.code == "200") {
                var badcode = [];
                if (data.data.length > 0) {
                    $.each(data.data, function (index, item) {
                        if (item.flag == "1") {
                            badcode.push(item.code);
                        }
                    });
                    if (badcode.length > 0) {
                        result = "" + badcode.join(",") + "重复";
                    } else {
                        result = true;
                        return result;
                    }

                } else {
                    result = true;
                    return result;
                }
            }
            // result = "" + badcode.join(",") + "重复";
        }, function () {
            result = "检查优惠码,网络发生异常";
        });
        return result;
    },
    __render: function (item, type) {//渲染界面
        $("#basicForm").html(common.util.__template2($("#promotionCode-template").html(), item));
        e.init();
        startTimeObj = $("#startTime").fdatepicker({
            format: 'yyyy-mm-dd hh:ii:ss',
            pickTime: true
        }).data("datepicker");
        endTimeObj = $("#endTime").fdatepicker({
            format: 'yyyy-mm-dd hh:ii:ss',
            pickTime: true
        }).data("datepicker");
        new common.dropDown({el: '#filter-couponType'});
        new common.dropDown({el: '#filter-department'});

    }
};
//详情页 不可改
if (type == "add") {
    Bll.__render(couponBean, "add");
} else {
    var id = location.href.substring(location.href.lastIndexOf("/") + 1);
    common.util.__ajax(
        {
            url: "/market/coupon/getPromotionCode",
            data: {id: id}
        }, function (res) {
            couponBean = res.data;
            var reqDepartment = couponBean.reqDepartment.split('/');
            couponBean.department1 = reqDepartment[0];
            couponBean.department2 = reqDepartment[1];
            var promotionInfo = JSON.parse(couponBean.promotionInfo);
            couponBean.promotionInfotype = promotionInfo.type;
            couponBean.amount_at_least = promotionInfo.condition.amount_at_least;
            couponBean.count_at_least = promotionInfo.condition.count_at_least;
            couponBean.discount = promotionInfo.action.discount;
            couponBean.discount_at_most = promotionInfo.action.discount_at_most;
            if (couponBean.userSourceLimit) {
                userSourceLimit = couponBean.userSourceLimit.split('|');
                for (var i = 0; i < userSourceLimit.length; i++) {
                    var custom = "custom-" + userSourceLimit[i];
                    $("input ." + custom).attr("checked", "checked");
                }
            }
            if (couponBean.userTypeLimit) {
                userTypeLimit = couponBean.userTypeLimit.split('|');
                for (var i = 0; i < userTypeLimit.length; i++) {
                    var custom = "userTypeLimit-" + userTypeLimit[i];
                    $("input ." + custom).attr("checked", "checked");
                }
            }
            Bll.__render(couponBean, type);
        }, true);
}
e.init();
/**
 * 用户身份
 */
$(document).on("click", "input[name='userTypeLimit']", function () {
    if ($(this).is(':checked')) {
        userTypeLimit.push($(this).val());
    } else {
        for (var i = 0; i < userTypeLimit.length; i++) {
            if ($(this).val() == userTypeLimit[i]) {
                userTypeLimit.splice(i, 1)
            }
        }
    }
    couponBean.userTypeLimit = userTypeLimit.join("|");
});
/**
 * 用户来源
 */
$(document).on("click", "input[name='userSourceLimit']", function () {
    if ($(this).is(':checked')) {
        userSourceLimit.push($(this).val());
    } else {
        for (var i = 0; i < userSourceLimit.length; i++) {
            if ($(this).val() == userSourceLimit [i]) {
                userSourceLimit.splice(i, 1)
            }
        }
    }
    couponBean.userSourceLimit = userSourceLimit.join("|");
});
/**
 * 优惠类型
 */
$(document).on('change', "input[name=promotionInfotype]", function () {
    var $checked = $(this);
    var $discount = $("#discount");
    if ($checked.val() == "3") {
        $discount.prop("disabled", true).val("0.0").parent("#resultdiscount").hide();
    } else {
        $discount.prop("disabled", false).parent("#resultdiscount").show();
    }
});
/**
 * 保存事件
 */
$(document).on("click", "#save_brand", function () {
    var promotionInfo = {
        type: couponBean.promotionInfotype,
        condition: {
            amount_at_least: couponBean.amount_at_least,
            count_at_least: couponBean.count_at_least
        },
        action: {
            discount: couponBean.discount,
            discount_at_most: couponBean.discount_at_most
        }
    };
    couponBean.promotionInfo = JSON.stringify(promotionInfo);
    couponBean.reqDepartment = [couponBean.department1, couponBean.department2].join('/');
    var url = $("#basicForm").data("action");
    var flag = true;
    if (type == "add") {
        flag = Bll.validateCode();
    }
    if (e.validate()) {
        if (flag == true) {
            common.util.__ajax({
                url: url,
                data: couponBean
            }, function () {
                window.location = "/market/coupon/index";
            });
        } else {
            common.util.__tip(Bll.validateCode(), 'warning')
        }
    }
    return false;
});