couponList.js 9.44 KB
/**
 * Created by ty on 2016/5/24.
 */

'use strict';
var $ = require('jquery'),
    common = require('../common/common');
/**
 * 全局变量
 * departments 部门
 * couponTypes 优惠券类型
 * customTypes 用户类型
 * ENUM tab参数
 * @type {string[]}
 */
var departments = ["渠道二部", "渠道一部", "零售部", "品牌资源部", "店铺运营部", "类目运营部", "营销部", "市场部", "会员部 ", "客服部", "内容营销部"];
var couponTypes = ["A券", "B券", "公开券", "生日券", "免邮券"];
var useLimitTypes = ["无限制", "货物件数限制", "订单金额限制"];
var customTypes = ["新客户", "银卡会员", "金卡会员", "白金卡会员", "普通用户(不含新客)"];
var ENUM = {
    status: {0: '待审核', 1: '审核通过', 2: '驳回', '-1': '过期', 3: '作废'},//全部
    tips: {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "all": 0}
};
var columnname = "all";
/**
 * 初始化下拉框
 */
new common.dropDown({el: '#filter-dep'});
common.util.__ajax({
    url: "/coupon/queryList",//获取所有优惠券ID
    data: {size: 1000}
}, function (res) {
    $("#couponsIds").html(common.util.__template2($("#search-coupon").html(), res));
    new common.dropDown({el: "#choose-coupon"});
}, true);

/**
 * tab分页
 */
var t = new common.tab({
    el: "#basicTab",
    click: function () {
        columnname = $(this).find('a').attr('columnname');
        g.options.columns[6].hidden = true;
        g.options.columns[7].hidden = true;
        switch (columnname) {
            case "1":
            {
                g.options.columns[8].hidden = true;
            }
                break;
            case "2":
            {
                g.options.columns[6].hidden = false;
                g.options.columns[8].hidden = true;
            }
                break;
            case "3":
            {
                g.options.columns[7].hidden = false;
                g.options.columns[8].hidden = true;
            }
                break;
            case "all":
            {
                g.options.columns[8].hidden = false;
            }
                break;
        }
        g.init("/coupon/queryCouponList");
    },
    columns: [
        {name: "all", display: "全部"},
        {name: "1", display: "已通过"},
        {name: "0", display: "待审核"},
        {name: "2", display: "已驳回"},
        {name: "3", display: "已作废"},
        {name: "-1", display: "已过期"}
    ]
}).init(ENUM.tips);
/**
 * 列表
 * @type {common.grid}
 */
var g = new common.grid({
    el: '#basicTable',
    parms: function () {
        return {
            couponId: common.util.__input("choose-coupon"),
            couponName: common.util.__input("filter-couponName"),
            department: common.util.__input("filter-dep"),
            status: t.active
        };
    },
    columns: [
        {display: "优惠券ID", name: "id"},
        {
            display: "基本信息", render: function (item) {
            var arr = [];
            arr.push("<p>名称:" + item.couponName + "</p>");
            arr.push("<p>类型:" + couponTypes[item.couponType - 1] + "</p>");
            arr.push("<p>优惠结果:" + item.couponAmount + "</p>");
            arr.push("<p>数量:" + item.couponNum + "</p>");
            arr.push("<p>部门:" + departments[item.department - 1] + "</p>");
            return arr.join('');
        }
        },
        {
            display: "使用期限", render: function (item) {
            var startTime = Bll.getTime(item.startTime);
            var endTime = Bll.getTime(item.endTime);
            return startTime + "</br> 至 " + endTime;
        }
        },
        {
            display: "使用条件", render: function (item) {
            var str = "无限制";
            if (item.useLimitType == 1) {
                str = "件数满:" + item.useLimit;
            } else if (item.useLimitType == 2) {
                str = "金额满:" + item.useLimit;
            }
            return '<p style="max-width: 200px;word-wrap: break-word;">' + str + '</p>';
        }
        },
        {display: "优惠券说明", name: "explains"},
        {display: "申请人", name: "proposer"},
        {
            display: "驳回理由", hidden: true, name: "rejectReason", render: function (item) {
            return "<p style='color: red'>" + item.rejectReason + "</p>"
        }
        },
        {
            display: "作废理由", hidden: true, name: "invalidReason", render: function (item) {
            return "<p style='color: red'>" + item.invalidReason + "</p>"
        }
        },
        {
            display: '状态', render: function (item) {
            var html = ENUM.status[item.status] || '全部';
            if (item.reason) {
                if (item.status == 2 || item.status == 4) {
                    html += '<p style="color:red;word-wrap: break-word;">(' + item.reason + ')</p>';
                }
            }
            return html;
        }
        },
        {
            display: "操作", render: function (item) {
            var HtmArr = [];
            HtmArr = Bll.getButtons(item.__index, item.id, item.options);
            return HtmArr.join('');
        }
        }
    ]
});
g.init("/coupon/queryCouponList");
/**
 * 通用方法
 * @type {{getTime: Function, toast: Function, toastInfo: Function}}
 */
var Bll = {
    getTime: function (time) {
        var t = new Date(time * 1000);
        return common.util.__dateFormat(t, "yyyy-MM-dd hh:mm:ss");
    },
    toast: function (content, url, data) {
        common.dialog.confirm("温馨提示", content, function () {
            common.util.__ajax({
                url: url,
                data: data()
            }, function () {
                g.reload();
            },true);
        });
    },
    toastPass: function (content, url, data) {
        common.dialog.confirm("温馨提示", content, function () {
            common.util.__ajax({
                url: url,
                data: data
            }, function () {
                g.reload();
            },true);
        });
    },
    toastInfo: function (item, hint) {
        common.dialog({
            title: hint,
            content: common.util.__template2($("#templateInfo").html(), item),
            width: "40%",
            button: [{
                value: '取消',
                css: "btn btn-primary"
            }

            ]
        })
    },
    getButtons: function (index, id, array) {
        var btns = [];
        var buttons = [
            '<a class="btn btn-warning btn-xs apply-back" data-index="' + index + '" >驳回</a>',
            '<a class="btn btn-primary btn-xs apply-success" data-index="' + index + '" >通过</a>',
            '<a class="btn btn-info btn-xs coupon-info" data-index="' + index + '">查看详情</a>',
            '<a class="btn btn-info btn-xs apply-modify" data-index="' + index + '" href="/market/couponList/update/' + id + '">修改</a>',
            '<a class="btn btn-danger btn-xs apply-cancel" data-index="' + index + '" >作废</a>'
        ];
        for (var i = 0; i < array.length; i++) {
            btns.push(buttons[array[i]])
        }
        return btns;
    }
};

/**
 * 筛选
 */
$("#filter-btn").click(function () {
    g.reload(1);
});
//审核通过
$(document).on('click', '.apply-success', function () {
    var item = g.rows[$(this).data("index")];
    var data = {
        couponId: item.id
    };
    Bll.toastPass("确定要通过该申请吗?", "/coupon/approve", data);
});
//驳回
$(document).on('click', '.apply-back', function () {
    var item = g.rows[$(this).data("index")];
    var data = function () {
        var reason = $('#reason').val();
        if (reason === '' || $.trim(reason) === '') {
            return "请填写驳回原因";
        }
        return {
            couponId: item.id,
            rejectReason: reason
        };
    };
    Bll.toast(common.util.__template2($("#template").html(), {name: "你确定要驳回该申请吗?"}), "/coupon/reject", data);
});
//作废
$(document).on('click', '.apply-cancel', function () {
    var item = g.rows[$(this).data("index")];
    var data = function () {
        var reason = $('#reason').val();
        if (reason == '' || $.trim(reason) == '') {
            return "请填写作废原因";
        }
        return {
            couponId: item.id,
            invalidReason: reason
        };
    };
    Bll.toast(common.util.__template2($("#template").html(), {name: "你确定要作废此限购码吗?"}), "/coupon/invalid", data);
});
/**
 * 查看详情
 */
$(document).on('click', '.coupon-info', function () {
    var item = g.rows[$(this).data("index")];
    var item1 = $.extend(true, {}, item);
    item1.createTime = Bll.getTime(item1.createTime);
    item1.startTime = Bll.getTime(item1.startTime);
    item1.endTime = Bll.getTime(item1.endTime);
    item1.couponType = couponTypes[item1.couponType - 1];
    item1.department = departments[item1.department - 1];
    item1.useLimitType = useLimitTypes[item1.useLimitType];
    if (item1.customType) {
        item1.customType = item1.customType.split(",");
        var typeLen = item1.customType.length;
        var types = [];
        if (typeLen == 5) {
            item1.customType = "全部"
        }
        else {
            for (var i = 0; i < typeLen; i++) {
                types.push(customTypes[item1.customType[i] - 1]);
            }
            item1.customType = types.join(",");
        }
    }else {
        item1.customType="无";
    }
    Bll.toastInfo(item1, '优惠券详情');
});