index.js 12.4 KB

'use strict';
var $ = require('jquery'),
    common=require('../common/common');

var ENUM={
    vipType:{1:'正常折扣',2:'统一折扣',3:'无折扣',4:'固定折扣',5:'自定义折扣'},
    gender: {1: '男', 2: '女', 3: '通用'}
}


new common.dropDown({
    el:'#shop',
    ajax:'shopsRest'
});
new common.dropDown({
    el:'#supplier',
    ajax:'supplier'
});
new common.dropDown({
    el:'#brand',
    ajax:'brand'
});
new common.dropDown({
    el:'#cate',
    ajax:'maxSort'
});
new common.dropDown({el:"#jit"});
new common.dropDown({el:"#status"});
new common.dropDown({el:"#stock"});
new common.dropDown({el:"#sex"});

var g = new common.grid({
    el:"#basicTable",
    parms:function(){

        return {
            product_skn: common.util.__input("skn"),
            shop_id:common.util.__input("shop"),
            brand_id:common.util.__input("brand"),
            supplier_id:common.util.__input("supplier"),
            is_jit:common.util.__input("jit"),
            is_auditing:common.util.__input("status"),
            stock:common.util.__input("stock"),
            gender:common.util.__input("sex"),
            max_sort_id:common.util.__input("cate")
        };
    },
    columns:[
        {display: 'SKN', name: 'product_skn'},
        {display: '商品信息', name:'info', render: function(item) {
            return '<p>名称:' + item.product_name + '</p><p>品牌:' + item.brand_name + '</p><p>类目:' + item.middle_sort_name + '/' + item.small_sort_name + '</p>';
        }},
        {display: '供应商/店铺', name: 'supplier_name'},
        {display: '性别', name: 'gender', render: function(item) {
            return ENUM.gender[item.gender];
        }},
        {display: '吊牌价', name: 'retail_price'},
        {display: '销售价', name: 'sales_price'},
        {display: 'VIP折扣类型', name: 'vip_discount_type', render: function(item) {
            return ENUM.vipType[item.vip_discount_type];
        }},
        {display: 'VIP价', name: 'vip_price'}, 
        {display: '白金', name: 'vip3_price'},
        {display: '金卡', name: 'vip2_price'},
        {display: '银卡', name: 'vip1_price'},
        {display: '返币金额', name: 'return_coin'},
        {display: '操作信息', name: 'operateInfo', render: function(item) {
            var html = '';
            if (item.founder_name) {
                html += '<p>' + item.founder_name + '</p>';
            }

            if (item.updateTime) {
                html += '<p>' + item.updateTime + '</p>';
            }
            return html;
        }},
        {display: '操作', render:function(item){
            return '<a href="javascript:;" class="btn btn-info btn-xs modify-btn" data-skn="' + item.product_skn + '">修改</a>'
        }}
    ]
});

g.init($("#gridurl").val());

$('#basicTable').on('click', '.modify-btn', function() {
    var skn = $(this).data('skn'),
        that = this;

    if ($(that).data('detail')) {
        $(that).parents('tr').next('.product-detail').toggle();
        return;
    }

    $.ajax({
        type: 'POST',
        url: '/goods/getPrice',
        dataType: 'json',
        data: {
            param: skn
        }
    }).then(function(res) {
        var data = res.data.data;


        $(that).data('detail', true).parents('tr')
            .after($(common.util.__template($("#template").html(), data)))
            .next('.product-detail').find('.skn-value').text(skn);

        var $wrap = $(that).parents('tr').next('.product-detail'),
            $price = $wrap.find('.sale-price'),
            $select = $wrap.find('.discount-select'),
            $vipPrice = $wrap.find('.vip-price'),
            $vip3Price = $wrap.find('.vip3-price'),
            $vip2Price = $wrap.find('.vip2-price'),
            $vip1Price = $wrap.find('.vip1-price'),
            $submitBtn = $wrap.find('.sure-modify');

        $select.val(data.vip_discount_type);

        function vipPrice() {
            var price = $price.val(),
                vipPrice = {
                    vip1: price * 0.95,
                    vip2: price * 0.9,
                    vip3: price * 0.88,
                },
                selectVal = $select.val();

            if (selectVal == 5) {
                $vip1Price.removeAttr('disabled');
                $vip2Price.removeAttr('disabled');
                $vip3Price.removeAttr('disabled');
            } else {
                $vip1Price.attr('disabled', true);
                $vip2Price.attr('disabled', true);
                $vip3Price.attr('disabled', true);
            }

            if (selectVal == 1) {
                $vip1Price.val(vipPrice.vip1.toFixed(2));
                $vip2Price.val(vipPrice.vip2.toFixed(2));
                $vip3Price.val(vipPrice.vip3.toFixed(2));
            } else if (selectVal == 3) {
                $vip1Price.val($price.val());
                $vip2Price.val($price.val());
                $vip3Price.val($price.val());
            } else if (selectVal == 4) {
                $vip1Price.val($vipPrice.val());
                $vip2Price.val($vipPrice.val());
                $vip3Price.val($vipPrice.val());
            }

        }
        
        vipPrice();
        $select.on('change', function() {
            vipPrice();
        });

        $vipPrice.on('blur', function() {
            vipPrice();
        });

        $price.on('blur', function() {
            vipPrice();
        });

        $submitBtn.on('click', function() {
            $.ajax({
                type: 'POST',
                url: '/goods/updatePrice',
                dataType: 'json',
                data: {
                    product_skn: skn,
                    sales_price: $price.val(),
                    vip_discount_type: $select.val(),
                    return_coin: $wrap.find('.return-coin').val(),
                    vip_price: $vipPrice.val(),
                    vip1_price: $vip1Price.val(),
                    vip2_price: $vip2Price.val(),
                    vip3_price: $vip3Price.val()
                }
            }).then(function(res) {
                if (res.data.code == 200) {
                    prompt('修改成功', 'success', function() {
                        g.reload();
                    });
                } else {
                    prompt(res.data.message, 'danger');
                }
            });
        });

    });
});

function prompt(msg, type, callback) {
    $.gritter.add({
        title: msg,
        class_name: 'growl-' + type,
        sticky: false,
        time: '1000',
        after_open: function() {
            callback && callback();
        }
    });
}

//筛选
$("#filter-btn").click(function(){
    g.reload(1);
});

//回车筛选
$('#skn').on('keyup', function(event) {
    if (event.keyCode == 13) {
        $("#filter-btn").trigger('click');
    }
});

//批量导入按钮点击事件
$('#import-btn').on('click', function() {
    if ($('#basicTable').css('display') == 'none') {
        g.reload();
    } 
    $('#basicTable, .bulk-import').toggle();
});

//upload-input
var successList = null,
    $error = $('#error-msg');
common.edit.ajaxfileupload("#upload-input",{
    params:{type:"price",__type:"batch-import"},
    onStart: function() {
        $('.load-modal').show();
    },
    onComplete:function(response){
        console.log(response);
        $('.load-modal').hide();
        if (response.code == 200) {
            var data = response.data,
                li = '';

            $('#priceTable').show();
            $('.success-wrap').hide();
            successList = data.successList;
            $.each(data.failFileReason, function(i, value) {
                li += '<li>' + value + '</li>';
            });
            $error.empty().html(li);
            p.init(successList);
        } else {
            alert(response.message);
        }
    }
});



var isFirst = false,
    tableHtml = '';
var p = new common.grid({
    el:"#priceTable",
    columns:[
        {display: 'SKN', render: function(item) {
            return item.newProductPriceBo.product_skn;
        }},
        {display: '吊牌价', render: function(item) {
            return item.newProductPriceBo.retail_price + '<p style="color: #ccc;">' + item.oldProductPriceBo.retail_price + '</p>';
        }},
        {display: '销售价', render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.sales_price;

            if (newPrice.sales_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.sales_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.sales_price + '</p>';
        }},
        {display: 'VIP折扣类型', render: function(item) {
            return ENUM.vipType[item.newProductPriceBo.vip_discount_type] + 
                '<p style="color: #ccc;">' + ENUM.vipType[item.oldProductPriceBo.vip_discount_type] + '</p>';
        }},
        {display: 'VIP价', render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = '';

            if (newPrice.vip_price) {
                newPriceHtml = newPrice.vip_price;

                if (newPrice.vip_price < (newPrice.retail_price / 2)) {
                    newPriceHtml = '<p style="color: red">' + newPrice.vip_price + '</p>';
                }
            } else {
                newPriceHtml = '-'
            }
            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip_price + '</p>';
        }},
        {display: '白金价', render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.vip3_price;

            if (newPrice.vip3_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.vip3_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip3_price + '</p>';
        }},
        {display: '金卡价', render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.vip2_price;

            if (newPrice.vip2_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.vip2_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip2_price + '</p>';
        }},
        {display: '银卡价', render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.vip1_price;

            if (newPrice.vip1_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.vip1_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip2_price + '</p>';
        }},
        {display: '返币金额', render: function(item) {
            return item.newProductPriceBo.return_coin + '<p style="color: #ccc;">' + item.oldProductPriceBo.return_coin + '</p>';
        }},
        {display: '操作', render: function(item) {
            return '<a class="btn btn-info delete-btn" data-index="' + item.__index + '" href="javascript:;">删除</a>';
        }}
    ]
});

$('#delete-all').on('click', function() {
    $('#priceTable').empty();
    $error.empty();
})


//批量变价列表单个删除
$('#priceTable').on('click', '.delete-btn', function() {
    //var index = p.rows[$(this).data('index')];
    successList.splice($(this).data('index'),1);
    p.reload();
    // console.log(p.rows);

});


var path = '';
$('#sure-change').on('click', function() {
    var arr = [],
        data = '';

    if (!successList || successList.length == 0) {
        alert('请上传批量变价文件');
        return;
    }

    $.each(successList, function(i, value) {
        arr.push(value.newProductPriceBo);
    });
    
    data = JSON.stringify(arr);

    common.util.__ajax({
        url:'/goods/batchUpdatePrice',
        data:{
            batchList: data
        }
    },function(res){
        console.log(res);
        $('#priceTable').hide();
        $('.success-wrap').show();
        path = res.data;
        //$('#download-btn').attr('href', )
    });
});

$('#download-btn').on('click', function() {
    console.log(path);
    /*common.util.__ajax({
        url:'/goods/batch/download',
        data:{
            path: path
        }
    },function(res){
        console.log(res);
        //$('#download-btn').attr('href', )
    });*/
    location.href = $(this).data('domain') + '/batch/download?path=' + path;
});