index.js 9.03 KB
/**
 * 优惠码列表页
 */
'use strict';
var $ = require('jquery'),
    Handlebars = require('yoho.handlebars'),
    grid=require('../common/grid'),
    dropDown = require('../common/dropDown');

require('yoho.bootstrap')

exports.init = function() {

    //下拉框
    new dropDown({el: '#filter-dep'});


    /**
     * 状态数据转换为中文显示
     * @param: $element(转换的节点), status(转换的状态字符串)
     */
    function statusConvert(status) {
        switch (status) {
            case 0: 
                return '待审核';
                break;
            case 1:
                return '审核通过';
                break;
            case 2:
                return '驳回';
                break;
            case 3:
                return '过期';
                break;
            case 4:
                return '作废';
                break;
            default:
                return '全部';
                break;
        }
    }

    /**
     * 优惠码tab卡文字替换及默认选中
     * @param: param(请求count接口参数), indexStatus(切换条件时选中的tab卡)
     */
    function tabShow(param, indexStatus) {
        var param = param || {};
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/getCodeCount',
            data: param,
            success: function(res) {
                var tpl = Handlebars.compile($('#tab-tpl').html());

                $('.list-tabs ul').html(tpl({
                    data: res.data.data
                }));

                $('.list-tabs').find('li').each(function() {
                    var status = $(this).data('status');

                    $(this).find('span').text(statusConvert(status));

                    if (status === 'all') {
                        $(this).addClass('active').siblings().removeClass('active');
                    }
                });

                if (indexStatus === undefined) {
                    $('.list-tabs').find('[data-status="all"]').addClass('active').siblings().removeClass('active');
                } else {
                    $('.list-tabs').find('[data-status="' + indexStatus + '"]').addClass('active').siblings().removeClass('active');
                }          
            }
        })
    }

    tabShow();

    //tab卡切换列表事件
    $('.list-tabs').on('click', 'li', function() {
        $(this).addClass('active').siblings().removeClass('active');
        g.reload();
    });

    var btnAuthority = JSON.parse($("#btnAuthority").val());

    var g=new grid({
        el: '#basicTable',
        size: 10,
        parms:function(){

            return {
                batchNo: $.trim($('#filter-id').val()) ||"",
                status: parseInt($('.list-tabs').find('.active').data('status')) ||"",
                name: $.trim($('#filter-name').val()) ||"",
                reqDepartment:$.trim($('#filter-dep').val()) ||""
            };
        },
        columns:[
            {display:"批次号", name:"batchNo", render: function(item) {
                return '<a href="/coupon/info/' + item.id + '">' + item.batchNo + '</a>';
            }},
            {display:"基本信息",name:"name", render: function(item) {
                return '<p>名称:' + item.name + '</p><p>次数' + item.limitTimes + '</p><p>部门:' + item.reqDepartment + '</p>';
            }},
            {display: '使用期限', name: "name", render: function(item) {
                return '<p>' + item.limitDateFrom + '</p><p>' + item.limitDateTo + '</p>'
            }},
            {display: '优惠码说明', name: "describe"},
            {display: '申请人', name: "staff"},
            {display: '状态', name: "status", render: function(item) {
                var html = statusConvert(item.status);
                if (item.reason) {
                    html += '<p style="color:red;">(' + item.reason + ')</p>';
                }
                return html;
            }},
            {display:"操作",name:"operate",render:function(items){
                console.log(this);
                var HtmArr=[];
                if(btnAuthority.look){
                    HtmArr.push('<a href="/coupon/info/'+ items.id+'" class="btn btn-info btn-xs">查看详情</a>');
                }
                if(items.status == 0){
                    HtmArr.push('<a class="btn btn-primary btn-xs apply-success" data-id="'+ items.id+'" href="javascript:;">通过</a>');
                    HtmArr.push('<a class="btn btn-warning btn-xs apply-back" data-id="'+ items.id+'" href="javascript:;">驳回</a>');
                    HtmArr.push('<a class="btn btn-danger btn-xs apply-cancel" data-id="'+ items.id+'" href="javascript:;">作废</a>');
                }
                if(items.status == 1){
                    HtmArr.push('<a class="btn btn-danger btn-xs apply-cancel" data-id="'+ items.id+'" href="javascript:;">作废</a>');
                }
                if(items.status == 2){
                    HtmArr.push('<a class="btn btn-info btn-xs apply-modify" data-id="'+ items.id+'" href="/coupon/update/'+ items.id+'">修改</a>');
                }
                return HtmArr.join('');
            }}
        ]
    })
    
    g.init($("#gridurl").val());

    $("#filter-btn").click(function(){
        tabShow({
            batchNo: $.trim($('#filter-id').val()),
            name: $.trim($('#filter-name').val()),
            reqDepartment:$.trim($('#filter-dep').val())
    }, parseInt($('.list-tabs').find('.active').data('status')))
        g.reload();
    });

    //作废事件
    $('#basicTable').on('click', '.apply-cancel', function() {
        var that = this,
            applyId = $(this).data('id');

        $('#cancel-modal').modal();
        $('#cancel-modal').on('click', '.btn-primary', function() {
            var reason = $('#cancel-reason').val();

            if (reason === '' || $.trim(reason) === '') {
                alert('请填写作废原因');
                return;
            }

            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '/auditPromotion',
                data: {
                    id: applyId,
                    status: 4,
                    reason: reason
                },
                success: function(data) {
                    $('#cancel-modal').modal('hide');
                    g.reload();
                }
            })
        });
    });

    
    //通过事件
    $('#basicTable').on('click', '.apply-success', function() {
        var that = this,
            $listBtn,
            applyId = $(this).data('id'),
            applyState = $(this).data('state');

        $('#success-modal').modal();
        $('#success-modal').on('click', '.btn-primary', function() {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '/auditPromotion',
                data: {
                    id: applyId,
                    status: 1    //审核通过
                },
                success: function(data) {
                    $('#success-modal').modal('hide');
                    g.reload();
                }
            })
        });

        $listBtn = $('#success-modal').find('.btn-info');   

        //发放列表点击事件
        $listBtn.off('click.sendPromotion').on('click.sendPromotion', function() {
            var auditTime = $(that).parents('td').siblings('.time').find('p').eq(0).text();

            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '/sendPromotion',
                data: {
                    auditTime: auditTime
                },
                success: function(res) {
                    if (res.data.data.length === 0) {
                        alert('暂无时间交叉的优惠码');
                    } else {
                        $('#send-modal').modal({
                            width: 1000
                        });

                        var tpl = Handlebars.compile($('#send-tpl').html());

                        $('#send-modal tbody').html(tpl({
                            data: res.data.data
                        }));
                    }
                    
                }
            })
        });
    });

    //驳回事件
    $('#basicTable').on('click', '.apply-back', function() {
        var that = this,
            applyId = $(this).data('id');

        $('#back-modal').modal();
        $('#back-modal').on('click', '.btn-primary', function() {
            var reason = $('#back-reason').val();

            if (reason === '' || $.trim(reason) === '') {
                alert('请填写驳回原因');
            }
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '/auditPromotion',
                data: {
                    id: applyId,
                    status: 2,
                    reason: reason
                },
                success: function(data) {
                    $('#back-modal').modal('hide');
                    g.reload();
                }
            })
        })
    });

    
}