coupon-edit.js 5.92 KB
var $ = require('jquery');
var edit=require('../common/edit');


    var e=new edit("#basicForm");
    var type=$("#basicForm").attr("role");

    if(type=="info"){
        e.on("render",function(){
            $("input:text","#basicForm")
            .add("select","#basicForm")
            .add(":radio","#basicForm")
            .add(":checkbox","#basicForm").prop("disabled",true);

            $(".panel-footer").hide();
        });
    }
    //渲染优惠详情
    e.on("render",function(){
        if($("#promotionInfo").val()){
            var __promotionInfo = JSON.parse($("#promotionInfo").val());

            $("input:radio[name=promotionInfo-type]").each(function(){
                if($(this).val()==__promotionInfo["type"]){
                    $(this).prop("checked","checked");
                    if($(this).val()=="3"){
                        $("#resultdiscount").hide();
                    }
                }
            });
            $("#promotionInfo-condition-amount_at_least").val(__promotionInfo["condition"]["amount_at_least"]);
            $("#promotionInfo-condition-count_at_least").val(__promotionInfo["condition"]["count_at_least"]);
            $("#promotionInfo-action-discount").val(__promotionInfo["action"]["discount"]);
            $("#promotionInfo-action-discount_at_most").val(__promotionInfo["action"]["discount_at_most"]);
        }
    });

    e.on("render",function(){
        if($("#reqDepartment").val()){
            var departmentArr = String.prototype.split.call($("#reqDepartment").val(), '/'),
            $department1 = $("#department1"),
            $department2 = $("#department2");

            $department1.val(departmentArr[0]);
            if (departmentArr[1]) {
                $department2.val(departmentArr[1]).show();
            } else {
                $department2.hide();
            }
        }
    });

    e.on("bind",function(){
        if($("#reqDepartment").val()){
            var departmentArr = String.prototype.split.call($("#reqDepartment").val(), '/'),
            $department1 = $("#department1"),
            $department2 = $("#department2");

            $("select").change(function() {
                if ($(this).is($department1)) {
                    departmentArr.length = 0;
                    departmentArr[0] = $(this).val();
                    if ($(this).val() === "零售部") {
                        $department2.show();
                        departmentArr[1] = $department2.val();
                    } else {
                        $department2.hide();
                    }
                } else {
                    departmentArr[1] = $(this).val();
                }
                $("#reqDepartment").val(departmentArr.join('/'));
            });
        }
    });

    e.on("bind",function(){
        $discount = $("#promotionInfo-action-discount");
        $("input:radio[name=promotionInfo-type]").change(function(){
            var $checked=$(this);
            if ($checked.val() == "3") {
                $discount.prop("disabled", true).val("0.00").parent("#resultdiscount").hide();
            } else {
                $discount.prop("disabled", false).parent("#resultdiscount").show();
            }
        });
    });

    //添加需要验证
    if(type=="add"){
        e.on("validate",function(){
            function validatecode(){
                var result=false;
                $.ajax({
                    type: 'POST',
                    url: '/market/coupon/checkPromotionCode',
                    async:false,
                    data: {
                        code:$("#code").val()
                    },
                    dataType: 'json'
                }).then(function(data) {
                    data = data.data;
                    if (data.code == "200") {
                        var badcode = [];

                        $.each(data.data, function(index, item) {
                            if (item.flag == "1") {
                                badcode.push(item.code);
                            }
                        });
                        if (badcode.length == 0) {
                            result=true;
                            return result;
                        }
                    }
                    result=""+badcode.join(",")+"重复";
                }, function() {
                    result="检查优惠码,网络发生异常";
                });
                return result;
            }
            return validatecode();
            
        });
    }

    e.init();
    //提交
    var submit=function(callback){
        e.submit($("#basicForm").attr("action"),function(option){
            option.success=function(res){
                res=res.data;
                if(res.code=="200"){
                    e.$tip('提交成功',function(){
                        location.href="/market/coupon/index";
                    },'growl-success');
                }else{
                    e.$tip(res.message);
                }
                return false;
            },
            option.error=function(res){
                e.$tip("提交失败");
            }
            console.log(option.data);
            callback&&callback(option.data);
        });
    };

    $("#save_brand").click(function(){
        var promotionInfo={
            type:$("input:checked[name=promotionInfo-type]").val(),
            condition:{
                amount_at_least:$("#promotionInfo-condition-amount_at_least").val(),
                count_at_least:$("#promotionInfo-condition-count_at_least").val()
            },
            action:{
                discount:$("#promotionInfo-action-discount").val(),
                discount_at_most:$("#promotionInfo-action-discount_at_most").val()
            }
        }
        $("#promotionInfo").val(JSON.stringify(promotionInfo));

        submit(function(data){
            //修改数据
            if(data.status=="2"){
                data.status=0;
            }
        });
        return false;
    });