Add.js 2.72 KB
/**
 * Created by jiangmin on 16/5/26.
 * 品牌发券管理
 */

var $ = require('jquery');
common = require('../../../common/common');
var activity = require('./../partials/activity');
var startTimeObj;
var endTimeObj;

var e1 = new common.edit2("#basicForm");
/**
 * 监听输入值的变化
 */
$(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 = {
    //渲染界面
    render: function () {
        $("#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.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/add",
            data: activity
        }, function () {
            location.href = "/couponActivity/index";
        })
    }
});