updateActivity.js 3.6 KB
/**
 * Created by jiangmin on 16/5/26.
 * 品牌发券管理
 */
var $ = require('jquery');
common = require('../common/common');
var e1 = new common.edit2("#basicForm");
var activity = {};
var activityId = location.href.substring(location.href.lastIndexOf("/") + 1);

/**
 * 监听输入值的变化
 */
$(document).on("change", ".observe", function () {
    var $this = $(this);
    var name = $this.data("field");
    activity = common.util.__buildobj(name, '.', activity, function (obj, name) {
        obj[name] = $this.val();
    });
});
//通用对象
var Bll = {
    //获取数据,并转换相应格式
    getData: function () {
        common.util.__ajax({
            url: "/couponActivity/queryById",
            data: {id: activityId},
            async: false
        }, function (res) {
            activity = res.data;
        }, true);
        var ids=[];
        activity.couponIds=[];
        if(activity.couponId){
            ids = activity.couponId.split(",");
        }
        if(ids.length==0){
            activity.couponIds.push({id: ""});
        }else{
            for (var i = 0; i < ids.length; i++) {
                activity.couponIds.push({id: ids[i]});
            }
        }
        if(activity.startTime){
            activity.startTime=Bll.getTime(activity.startTime);
        }
        if(activity.endTime){
            activity.endTime=Bll.getTime(activity.endTime);
        }
    },
    //渲染界面
    render: function () {
        console.log("activity",activity);
        $("#basicForm").html(common.util.__template2($("#couponListAdd-template").html(), activity));
        Bll.__editRender();
    },
    //转换时间格式
    getTime: function (time) {
        var t = new Date(time * 1000);
        return common.util.__dateFormat(t, "yyyy-MM-dd hh:mm:ss");
    },
    //验证
    __editRender: function () {
        e1.init();
        new common.dropDown({
            el: "#filter-activityType"
        });
        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");
        e1.on("file_onComplete", function (obj) {
            var names = obj.field;
            activity = common.util.__buildobj(names, '.', activity, function (o, name) {
                o[name] = obj.data;
            });
        });
    }
};
Bll.getData();
Bll.render();

/*添加一行*/
$(document).on("click", ".addBtn", function () {
    activity.couponIds.push({
        "id": ""
    });
    Bll.render();
});
/**
 * 删除行
 */
$(document).on("click", ".delBtn", function () {
    var index = $(this).data("index");
    if (activity.couponIds.length <= 1) {
        common.util.__tip("至少有一张优惠券", "warning")
    }
    else {
        activity.couponIds.splice(index, 1);
        Bll.render();
    }
});
/**
 * 保存
 */
$(document).on('click', "#save_brand", function () {
    var ids = [];
    if (activity.couponIds.length > 0) {
        for (var index in activity.couponIds) {
            ids.push(activity.couponIds[index].id)
        }
    }
    if (ids.length > 0) {
        activity.couponId = ids.join(",");
    }
    activity.startTime=((new Date(activity.startTime).getTime()))/1000||"";
    activity.endTime=((new Date(activity.endTime).getTime()))/1000||"";
    if(e1.validate()){
        common.util.__ajax({
            url:"/couponActivity/update",
            data:activity
        }, function (){
            location.href = "/couponActivity/index";
        } )
    }
});