+(function($){
	$.lie = $.lie || {version: "v1.0.0"};

	$.extend($.lie, {
		add_order:{
            lyorder: function() { // 联营订单
                this.bind(1, 1);      
            },

            zyorder: function() { // 自营线上订单
                this.bind(2, 1);
            },

			offline: function(internal_uid) { // 自营线下订单
                this.bind(2, 0, internal_uid);
                this.lists(internal_uid, 2, 0);
            },

            businessType: function() { // 自营其他业务订单
                this.bind(2, 2);
            },

            /**
             * goods_type: is_online:是否线上订单
             * @param  {[type]}  goods_type   [1-联营,2-自营]
             * @param  {Boolean} is_online    [0-自营线下,1-联营/自营线上,2-自营其他业务]
             * @param  {String}  internal_uid [自营内部采购账号ID]
             * @return {[type]}               [description]
             */
            bind: function(goods_type, is_online, internal_uid='') { 
                var loading = this;

                // 获取用户信息
                $('.get-user').click(function(){
                    var mobile = $(this).siblings('.search-user').val();

                    if (!mobile) return false; 

                    // 自营其他订单类型---仓库损耗
                    if (mobile == '15022222222') {
                        $('#business_type').val(2).attr('disabled', true);
                    } else {
                        $('#business_type').val(1).attr('disabled', false); 
                    }

                    $.ajax({
                        url: '/ajax/getUserInfo',
                        type: 'post',
                        data: {mobile : mobile.trim(), goods_type: goods_type},
                        dataType: 'json',
                        success: function (resp) {
                            if (resp.errcode == 0) {
                                var data = resp.data;
                                var reg = /@/;

                                $('.user-info').show();

                                // 账号信息
                                $('.user_id').val(data.user.user_id);

                                if (reg.test(mobile)) {
                                    $('.user-mobile').text(data.user.email);
                                } else {
                                    $('.user-mobile').text(data.user.mobile);
                                }       

                                var is_test = data.user.is_test ? '是' : '否';
                                $('.user-test').text(is_test);

                                // 快递信息
                                if (data.address != null) {
                                    var addr_len = data.address.length;
                                    var addr_str = '<option value="">请选择</option>';

                                    for (var i = 0; i < addr_len; i++) {
                                        addr_str += '<option value="'+data.address[i]['address_id']+'">'+data.address[i]['consignee']+'</option>';
                                    }

                                    $('#address').empty().append(addr_str);
                                }

                                // 发票信息
                                if (data.invoice != null) {
                                    var inv_len = data.invoice.length;
                                    var inv_str = '<option value="">请选择</option>';

                                    for (var i = 0; i < inv_len; i++) {
                                        inv_str += '<option value="'+data.invoice[i]['tax_id']+'">'+data.invoice[i]['tax_title']+'</option>';
                                    }

                                    $('#invoice').empty().append(inv_str);
                                }
          
                                loading.lists(data.user.user_id, goods_type, is_online); // 加载该用户购物车

                                return false;
                            }

                            layer.msg(resp.errmsg)
                        },
                    })
                })

                // 选择收货地址
                $('.user-info').delegate('#address', 'change', function() {
                    var user_id = $('.user_id').val();
                    var address_id = $(this).val();
                    var user_coupon_id = $('.user_coupon_id').val();
                    var cart_ids = [];

                    $('.shop-table tbody').find('tr').each(function() {
                        var cid = $(this).data('cid');
                        cart_ids.push(cid);
                    })
             
                    if (address_id) {
                        $.ajax({
                            url: '/ajax/selectAddr',
                            type: 'post',
                            data: {address_id : address_id},
                            dataType: 'json',
                            success: function (resp) {
                                if (resp.errcode == 0) { 
                                    var data = resp.data;

                                    $('.address-content').show();

                                    $('.address_id').val(data.address_id);
                                    $('.address-consignee').text(data.consignee);
                                    $('.address-mobile').text(data.mobile);
                                    $('.address-detail').text(data.province_val + data.city_val + data.district_val + data.detail_address);
                                    
                                    if (cart_ids.length > 0) {
                                        loading.confirm(user_id, address_id, cart_ids, user_coupon_id, goods_type, is_online); // 确认订单信息
                                    }

                                    return false;
                                }

                                layer.msg(resp.errmsg || '网络异常');
                            }
                        })
                    } else {
                        $('.address-content').hide();
                        $('.address_id').val('');

                        if (cart_ids.length > 0) {
                            loading.confirm(user_id, address_id, cart_ids, user_coupon_id, goods_type, is_online); // 确认订单信息
                        }
                    }
                })

                // 选择发票类型
                $('.user-info').delegate('.radio-inv-type', 'click', function() { 
                    var inv_type = $(this).val();
                    var user_id = $('.user_id').val();
                    var inv_str = '<option value="">请选择</option>';

                    $('.invoice-content').hide();
                    $('.tax_id').val('');

                    if (inv_type) {
                        $.ajax({
                            url: '/ajax/selectInvType',
                            type: 'post',
                            data: {user_id: user_id, inv_type: inv_type},
                            dataType: 'json',
                            success: function (resp) {
                                if (resp.errcode == 0) {
                                    var data = resp.data;

                                    if (data != null) {
                                        var inv_len = data.length;  

                                        for (var i = 0; i < inv_len; i++) {
                                            inv_str += '<option value="'+data[i]['tax_id']+'">'+data[i]['tax_title']+'</option>';
                                        }

                                        $('#invoice').empty().append(inv_str);
                                    } else {
                                        $('#invoice').empty().append(inv_str);
                                    }

                                    return false;
                                }

                                layer.msg(resp.errmsg || '网络异常');
                            }
                        })
                    } else {
                        $('#invoice').empty().append(inv_str);
                    }
                })

                // 选择发票信息
                $('.user-info').delegate('#invoice', 'change', function() {
                    var tax_id = $(this).val();
                    
                    if (tax_id) {
                        $.ajax({
                            url: '/ajax/selectInv',
                            type: 'post',
                            data: {tax_id : tax_id},
                            dataType: 'json',
                            success: function (resp) {
                                if (resp.errcode == 0) { 
                                    var data = resp.data;
                                    var inv_type_val = '';

                                    $('.invoice-content').show();

                                    $('.tax_id').val(data.tax_id);
                                    $('.tax_title').text(data.tax_title);
                                    $('.tax_no').text(data.tax_no);
                                    $('.bank_account').text(data.bank_account);
                                    $('.bank_name').text(data.bank_name);
                                    $('.company_phone').text(data.company_phone);
                                    $('.company_address').text(data.company_address);
                                    $('.consignee').text(data.consignee);
                                    $('.consignee_phone').text(data.consignee_phone);
                                    $('.consignee_address').text(data.province_val + data.city_val + data.district_val + data.consignee_address);

                                    switch (data.inv_type) {
                                        case 2: inv_type_val = '普通发票'; break;
                                        case 3: inv_type_val = '增值税专用发票'; break;
                                        case 4: inv_type_val = '增值税普通发票'; break;
                                        default: inv_type_val = '未知'; break;
                                    }

                                    $('.inv_type').text(inv_type_val);
                                }
                            }
                        })
                    } else {
                        $('.invoice-content').hide();
                        $('.tax_id').val('');
                    }
                })

                // 获取SKU信息
                $('.get-sku').click(function() {
                    var sku_id = $(this).siblings('.search-sku').val();

                    if (!sku_id) {
                        layer.msg('请输入SKU_ID');
                        return false;
                    }

                    if (!/\d/.test(sku_id)) {
                        layer.msg('参数错误,请输入正确的SKU_ID');
                        return false;
                    }

                    $.ajax({
                        url: '/ajax/getSku',
                        type: 'post',
                        data: {sku_id : sku_id.trim()},
                        dataType: 'json',
                        success: function (resp) { console.log(resp)
                            if (resp.errcode != 0) {
                                layer.msg(resp.errmsg);
                                return false;
                            }

                            $('.sku-info').show();

                            var data = resp.data;
                            var status_val = '';

                            $('.goods_name').text(data.goods_name);
                            $('.brand_name').text(data.brand_name);
                            $('.supplier_name').text(data.supplier_name);
                            $('.stock').text(data.goods_number);
                            $('.moq').text(data.min_buy);
                            $('.mpq').text(data.min_mpq);

                            // if (goods_type == 1) {
                            //     var status = data.goods_status;
                            // } else {
                            //     var status = data.status;
                            // }

                            // switch (status) {
                            //     case 0: status_val = '待入库'; break;
                            //     case 1: status_val = '审核通过(上架)'; break;
                            //     case 3: status_val = '下架'; break;
                            //     case 4: status_val = '删除'; break;
                            // }
                            
                            status_val = data.is_buy ? '是' : '否';
                            $('.status').text(status_val);
                            $('.goods_id').val(data.goods_id);
                            $('.goods_type').val(data.goods_type);

                            //阶梯价格
                            if (data.tiered != null) {
                                var len = data.tiered.length;

                                if (len > 0) {
                                    var html = '';

                                    html += '<tr><th>阶梯</th><th>RMB价格</th>';

                                    if (data.ac_type) {
                                        switch (data.ac_type) {
                                            case 1: html += '<th>限时限量</th>'; break;
                                            case 2: html += '<th>活动价</th>'; break;
                                            case 3: html += '<th>会员价</th>'; break;
                                            default: html += ''; break;
                                        }
                                    }
                                    
                                    if (goods_type == 1) {
                                        html += '<th>USD价格</th>';
                                    }

                                    html += '</tr>';

                                    for (var i = 0; i < len; i++) {
                                        html += '<tr>';

                                        if (i == 0) {
                                            if (data.ac_type) {
                                                html += '<td><span class="goods-min-num">'+data.tiered[i]['purchases']+'</span></td><td>¥<span class="goods-min-price price-line">'+data.tiered[i]['price_cn']+'</span><td>¥<span>'+data.tiered[i]['price_ac']+'</span></td>';
                                            } else {
                                                html += '<td><span class="goods-min-num">'+data.tiered[i]['purchases']+'</span></td><td>¥<span class="goods-min-price">'+data.tiered[i]['price_cn']+'</span></td>';
                                            }

                                            if (goods_type == 1) {
                                                html += '<td>$<span class="goods-min-price-us">'+data.tiered[i]['price_us']+'</span></td>';
                                            }
                                        } else {
                                            if (data.ac_type) {
                                                html += '<td><span>'+data.tiered[i]['purchases']+'</span></td><td>¥<span class="price-line">'+data.tiered[i]['price_cn']+'</span></td><td>¥<span>'+data.tiered[i]['price_ac']+'</span></td>';
                                            } else {
                                                html += '<td><span>'+data.tiered[i]['purchases']+'</span></td><td>¥<span>'+data.tiered[i]['price_cn']+'</span></td>';
                                            }
                                            
                                            if (goods_type == 1) {
                                                html += '<td>$<span>'+data.tiered[i]['price_us']+'</span></td>';
                                            }     
                                        } 

                                        html += '</tr>';     
                                    }

                                    // $('.ladder_price_table tr:gt(0)').remove();
                                    $('.ladder_price_table').empty().append(html);
                                }
                            }    
                        }
                    })
                })

                // 添加到商品列表
                $('.add-to-lists').click(function() {
                    var goods_min_num = $(this).parents('.sku-info').find('.moq').text();
                    var goods_id = $(this).siblings('.goods_id').val();
                    var type = $(this).siblings('.goods_type').val();
                    var delivery_place = 1; // 1.大陆,2.香港 自营默认是大陆
                    var user_id = 0;

                    if (internal_uid) {
                        user_id = internal_uid;
                    } else {
                        user_id = $('.user_id').val();
                    }

                    if (!user_id) {
                        layer.msg('请先选择用户');
                        return false;
                    }

                    if (goods_type == 1) { // 联营需要选择交货地
                        delivery_place = $('input[name=delivery_place]:checked').val();

                        if (delivery_place == null) {
                            layer.msg('请选择交货地');
                            return false;
                        }

                        if (type == 3 || type == 4) {
                            layer.msg('仅支持联营商品下单,请在自营订单中下单');
                            return false;
                        }

                        var goods_min_price = $(this).parents('.sku-info').find('.goods-min-price').text();
                        var goods_min_price_us = $(this).parents('.sku-info').find('.goods-min-price-us').text();

                        if (delivery_place == 1) {
                            if (goods_min_price == 0) {
                                layer.msg('缺少RMB价格,商品添加失败');
                                return false;
                            }
                        } else if (delivery_place == 2) {
                            if (goods_min_price_us == 0) {
                                layer.msg('缺少美金价格,商品添加失败');
                                return false;
                            }
                        }                         
                    } else {
                        if (type == 1 || type == 2) { // 1.联营 2.专卖
                            layer.msg('仅支持自营商品下单,请在联营订单中下单');
                            return false;
                        }
                    }

                    var datax = {
                        uid: user_id,
                        id: goods_id,
                        num: goods_min_num,
                        buy: -1,
                        delivery_place: delivery_place, 
                    }

                    $.ajax({
                        type: "POST",
                        url: '/ajax/addCart',
                        data: datax,
                        dataType: "json",
                        success: function(resp){
                            layer.msg(resp.errmsg);
                            loading.lists(user_id, goods_type, is_online, delivery_place); // 加载购物车列表
                            return false;
                        },
                    })                
                })

                // 切换交货地
                $('.delivery_place').click(function() {
                    var user_id = $('.user_id').val();
                    var delivery_place = $(this).val();

                    loading.lists(user_id, goods_type, is_online, delivery_place); // 加载购物车列表
                })

                // 调整商品数量
                $('.shop-table').delegate('.goods_num', 'blur', function() {
                    var self = $(this);
                    var num = self.val();
                    var min_buy = Number(self.parents('tr').attr('min_buy'));
                    var cart_id = self.parents('tr').data('cid');
                    var type = self.parents('tr').data('type');
                    var user_id = 0;
                    var delivery_place = 1; // 1.大陆,2.香港 自营默认是大陆

                    if (internal_uid) {
                        user_id = internal_uid;
                    } else {
                        user_id = $('.user_id').val();
                    }

                    if (num < min_buy) {
                        num = min_buy;
                        layer.msg('购买数量低于最小起订量,默认调整为最小起订量');
                    }

                    if (goods_type == 1) { // 联营需要选择交货地
                        delivery_place = $('input[name=delivery_place]:checked').val();
                    } else { // 自营数量需要结合倍数
                        var mpl = Number(self.parents('tr').find('.goods_mpl').text()); // 倍数
                        num = Math.ceil(num / mpl) * mpl;
                    }

                    $.ajax({
                        type: "POST",
                        url: '/ajax/changeNum',
                        data: {uid: user_id, num: num, cart_id: cart_id, type: type, goods_type: goods_type},
                        dataType: "json",
                        success: function(resp){
                            if (resp.errcode == 0) {
                                loading.lists(user_id, goods_type, is_online, delivery_place); // 加载购物车列表
                                return false;
                            }

                            layer.msg(resp.errmsg);
                        },
                    })
                })

                // 删除商品
                $('.shop-table').delegate('.remove-goods', 'click', function() {
                    var self = $(this);
                    var cart_id = self.parents('tr').data('cid');
                    var user_id = 0;
                    var delivery_place = 1; // 1.大陆,2.香港 自营默认是大陆

                    if (internal_uid) {
                        user_id = internal_uid;
                    } else {
                        user_id = $('.user_id').val();
                    }

                    if (goods_type == 1) { // 联营需要选择交货地
                        delivery_place = $('input[name=delivery_place]:checked').val();
                    }

                    layer.open({
                        title: '删除提示',
                        content: '确定删除该商品吗?',
                        btn: ['确定', '取消'],
                        btn1: function () {
                            $.ajax({
                                type: "POST",
                                url: '/ajax/deleteGoods',
                                data: {uid : user_id, cart_id : cart_id},
                                dataType: "json",
                                success: function(resp){
                                    layer.msg(resp.errmsg);
                                    // self.parents('tr').remove();
                                    loading.lists(user_id, goods_type, is_online, delivery_place); // 加载购物车列表
                                    return false;
                                },
                            })
                        }, 
                        btn2: function (index) {
                            layer.close(index);
                        }
                    })    
                })

                // 选择优惠券
                $('#user_coupon').change(function(){
                    var coupon_id = $(this).val();
                    var address_id = $('.address_id').val();
                    var user_id = $('.user_id').val();
                    var cart_ids = [];

                    $('.shop-table tbody').find('tr').each(function() {
                        var cid = $(this).data('cid');
                        cart_ids.push(cid);
                    })

                    if (coupon_id) {      
                        $('.user_coupon_id').val(coupon_id);
                    } else {
                        $('.user_coupon_id').val('');
                    }

                    loading.confirm(user_id, address_id, cart_ids, coupon_id, goods_type, is_online); // 确认订单信息
                })

                // 切换销售类型
                $('#sale_type').change(function() {
                    var coupon_id = $('.user_coupon_id').val();
                    var address_id = $('.address_id').val();
                    var user_id = $('.user_id').val();
                    var sale_type = $(this).val();
                    var cart_ids = [];

                    $('.shop-table tbody').find('tr').each(function() {
                        var cid = $(this).data('cid');
                        cart_ids.push(cid);
                    })

                    if (cart_ids.length > 0) {
                        $.ajax({
                            type: "POST",
                            url: '/ajax/switchSaleType',
                            data: {sale_type: sale_type, cart_ids: cart_ids},
                            dataType: "json",
                            success: function(resp){
                                if (resp.errcode == 0) {
                                    layer.msg(resp.errmsg);
                                    loading.lists(user_id, goods_type, is_online); // 加载购物车列表
                                   
                                    return false;
                                }   

                                layer.msg(resp.errmsg); 
                            },
                        })
                    }
                })

                // 批量导入物料
                $('.goods_import').click(function() {
                    var user_id = $('.user_id').val();
                    var delivery_place = 1; // 1.大陆,2.香港 自营默认是大陆

                    user_id = internal_uid ? internal_uid : user_id; // 若是内部下单则使用内部用户ID                                                         

                    if (!user_id) {
                        layer.msg('请先选择用户');
                        return false;
                    }

                    if (goods_type == 1) {
                        delivery_place = $('input[name=delivery_place]:checked').val();
                    }                      

                    // 上传文件
                    var form = $('<form id="formUpload" method="post" enctype="multipart/form-data">\
                                <input type="file" name="upload" id="fileInput" accept="application/vnd.ms-excel, application/x-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, .csv">\
                            </form>');
                    var type = $(this).data('type'); // 1-联营模板,2-自营模板

                    form.find('input').change(function(){
                        var formData = new FormData();
                        formData.append('file', this.files[0]);
                        formData.append('type', type); 
                        formData.append('user_id', user_id); 
                        formData.append('delivery_place', delivery_place); 

                        $.ajax({
                            type: "POST",
                            url: '/ajax/batchGoodsImport',
                            data: formData,
                            dataType: 'json',
                            cache: false,
                            processData: false,
                            contentType: false,
                            success: function(resp){
                                if (resp.errcode == 0) {
                                    layer.msg(resp.errmsg);
                                    loading.lists(user_id, goods_type, is_online, delivery_place); // 加载购物车列表

                                    return false;
                                }   

                                layer.msg(resp.errmsg); 
                            },
                        })
                    })

                    form.find('input[type="file"]').trigger('click');   
                })

                // 提交订单
                // $('.submit-order').on('click', function() {
                $('.submit-order').off().on('click', function() {
                    var self       = $(this);
                    var address_id = $('input[name=address_id]').val();
                    var type       = self.data('type'); // 1-联营线上,2-自营线上,3-自营线下,4-自营其他业务
                    var cart_ids   = [];
                    var user_id    = 0;
                    var remark     = $('#remark').val(); // 订单备注

                    if (internal_uid) {
                        user_id = internal_uid;
                    } else {
                        user_id = $('.user_id').val();
                    }

                    $('.shop-table tbody tr').each(function() {
                        var cid = $(this).data('cid');
                        cart_ids.push(cid);
                    })

                    if (cart_ids.length > 0) {
                        var submit = true;
                        var datax = {
                            uid: user_id,
                            sale_id: sale_id,
                            address_id: address_id,
                            type: type,
                            cart_ids: cart_ids,
                            remark: remark,
                        }

                        if (type == 3) { // 自营线下订单
                            datax.address_name = $('input[name=address_name]').val();
                            datax.address_mobile = $('input[name=address_mobile]').val();
                        } else { // 联营、自营线上订单
                            var tax_id = $('.tax_id').val();
                            var inv_type = $('input[name=inv_type]:checked').val();
                            var user_coupon_id = $('.user_coupon_id').val();

                            if (!address_id) {
                                layer.msg('请选择快递信息');
                                return false;
                            }
                            
                            if (type == 4) { // 自营其他业务
                                var business_type = $('#business_type').val();
                                datax.business_type = business_type;
                                datax.role = role; // 自营线上下单添加角色
                            } else {
                                if ((inv_type && !tax_id) || (!tax_id && goods_type == 2)) {
                                    layer.msg('请选择发票信息');
                                    return false;
                                }
                            }

                            datax.tax_id = tax_id;
                            datax.user_coupon_id = user_coupon_id; 
                        }

                        // self.attr('disabled', true).css('pointer-events', 'none'); // 成功生成订单后禁用按钮,防止重复提交

                        // 自营线上
                        if (type == 2 && is_online == 1) {
                            var sale_type = $('#sale_type').val();           
                            var cart_info = [];

                            $('.shop-table tbody tr').each(function() {
                                var gid = $(this).find('.goods_id').text();
                                var num = $(this).find('.goods_num').val();

                                cart_info.push({'gid':gid, 'num':num});
                            })

                            datax.role = role; // 自营线上下单添加角色

                            $.ajax({
                                type: "POST",
                                url: '/ajax/checkStock',
                                data: {cart_info: JSON.stringify(cart_info), sale_type: sale_type},
                                dataType: "json", 
                                async: false, 
                                success: function(resp){
                                    if (resp.errcode == -1) {
                                        var data = eval(resp.data);
                                        var content = '<p>'+resp.errmsg+'</p>';
                                        submit = false;

                                        for (var i = 0; i < data.length; i++) {
                                            content += '<p><span>'+(i+1)+'、</span>'+data[i]['goods_name']+',可用库存:'+data[i]['stock']+'</p>';
                                        }

                                        // 弹出提示
                                        layer.open({
                                            area: ['500px', '300px'],
                                            title: '提示信息',
                                            content: content,
                                            btn: [],
                                        });

                                        // self.removeAttr('disabled').css('pointer-events', 'auto');

                                        return false; 
                                    }                                         
                                },
                            })       
                        } 
                        
                        if (submit) {
                            $.ajax({
                                type: "POST",
                                url: '/ajax/create',
                                data: datax,
                                dataType: "json",
                                success: function(resp){
                                    if (resp.errcode == 0) {
                                        layer.msg(resp.errmsg);
                                        location.href = '/addorder/success?order_id='+resp.data.order_id+'&type='+resp.data.type;
                                       
                                        return false;
                                    }   

                                    layer.alert(resp.errmsg); 
                                },
                            })

                            layer.msg('订单生成中...', {icon: 16, time: 0, shade: 0.3});
                        }                         
                    } else {
                        layer.msg('请先添加商品');
                    }
                })
            },

            // 购物车商品列表
            lists: function(user_id, goods_type, is_online, delivery_place="1") { // type:1-联营,2-自营
                var self = this;
                var user_coupon_id = $('.user_coupon_id').val() ? $('.user_coupon_id').val() : 0;

                $.ajax({
                    type: 'post',
                    url: '/ajax/cartList',
                    data: {uid : user_id, type: goods_type, delivery_place: delivery_place},
                    dataType: "json",
                    success: function(resp){
                        if (resp.errcode == 0) {
                            var list = resp.data.list;
                            var len = list.length;
                            var html = '';
                            var order_amount = 0;
                            var currency = '';
                            var cart_ids = [];

                            if (len > 0) {
                                // 购物车列表
                                for (var i = 0; i < len; i++) {
                                    html += '<tr data-cid="'+list[i].cart_id+'" data-type="'+list[i].type+'" min_buy="'+list[i].min_buy+'">'+
                                        '<td>'+(i+1)+'</td>'+
                                        '<td class="goods_id">'+list[i].goods_id+'</td>'+
                                        '<td>'+list[i].goods_name+'</td>'+
                                        '<td>'+list[i].brand_name+'</td>';

                                    if (goods_type == 2) {
                                        html += '<td><span class="goods_mpl">'+list[i].mpl+'</span></td>';
                                    }
                                        
                                    html += '<td><input type="text" class="goods_num" name="goods_num" value="'+list[i].goods_number+'"></td>'+
                                            '<td class="goods_price">'+list[i].goods_price+'</td>'+
                                            '<td class="goods_amount">'+list[i].goods_amount_format+'</td>'+
                                            '<td>'+list[i].delivery_time+'</td>';
                                            
                                    if (role != 7) {
                                        html += '<td>'+list[i].supplier_name+'</td>';
                                    }
                                            
                                     html += '<td><a class="btn btn-danger btn-xs remove-goods">删除</a></td>'+
                                        '</tr>';

                                    cart_ids.push(list[i].cart_id); // 购物车ID集合
                                }

                                $('.shop-lists').show();
                                $('.shop-table tbody').empty().append(html);

                                $('#sale_type').val(list[0].sale_type); // 自营线上订单选择销售类型

                                var address_id = $('.address_id').val();
                                self.confirm(user_id, address_id, cart_ids, user_coupon_id, goods_type, is_online); // 确认订单信息
                            }

                            // 自营商品检查数量
                            if (goods_type == 2) {
                                var changeGoods = [];

                                $('.shop-table tbody tr').each(function() {
                                    var goods_id = $(this).find('.goods_id').text();
                                    var num = Number($(this).find('.goods_num').val());
                                    var mpl = Number($(this).find('.goods_mpl').text());
                                    var cart_id = $(this).data('cid');
                                    var type = $(this).data('type');

                                    if (num % mpl != 0) {
                                        num = Math.ceil(num / mpl) * mpl;

                                        $.ajax({
                                            type: "POST",
                                            url: '/ajax/changeNum',
                                            data: {uid: user_id, num: num, cart_id: cart_id, type: type, goods_type: goods_type},
                                            dataType: "json",
                                            async: false,
                                            success: function(resp){
                                                if (resp.errcode == 0) {
                                                    changeGoods.push(goods_id);                                                
                                                }
                                            },
                                        })
                                    }
                                })

                                if (changeGoods.length > 0) {
                                    layer.alert('已按倍数调整购买数量,商品ID:'+changeGoods.join(', '));
                                    self.lists(user_id, goods_type, is_online, delivery_place); // 加载购物车列表
                                }  
                            }
                        } else {
                            layer.msg(resp.errmsg || '网络异常');

                            $('.shop-lists').hide();

                            if (is_online) {
                                $('.order-info').hide();
                            } else {
                                $('.amount-section').hide();
                            }        
                        }
                    },
                });
            },

            // 可用优惠券
            getCoupon: function (user_id, goods_type, cart_ids)
            {
                $.ajax({
                    type: 'post',
                    url: '/ajax/getCoupon',
                    data: {uid : user_id, goods_type: goods_type, cart_ids : cart_ids},
                    dataType: "json",
                    success: function(resp){
                        if (resp.errcode == 0) {
                            var data = resp.data;
                            var str = '<option value="">请选择</option>';

                            if (data.user_coupon_id != null) {
                                layer.msg(resp.errmsg);

                                str += '<option value="'+data.user_coupon_id+'">'+data.coupon_name+'</option>';
                                
                                $('#user_coupon').attr('data-amount', data.sale_amount); // 优惠券金额
                                $('#user_coupon').empty().append(str);

                                return false;
                            } else {
                                $('#user_coupon').empty().append(str);
                            }

                            return false;
                        }

                        layer.msg(resp.errmsg || '网络异常');   
                    }
                })
            },

            // 确认订单信息
            confirm: function(user_id, address_id, cart_ids, user_coupon_id, goods_type, is_online=1) {
                var self = this;
                var business_type = $('#business_type'); // 自营其他业务

                if (business_type.length > 0) {
                    business_type = business_type.val();
                } else {
                    business_type = 0;
                }

                $.ajax({
                    type: 'post',
                    url: '/ajax/confirm',
                    data: {uid: user_id, address_id: address_id, cart_ids: cart_ids, user_coupon_id: user_coupon_id, goods_type: goods_type, is_online: is_online, business_type: business_type},
                    dataType: "json",
                    success: function(resp){console.log(resp);
                        if (resp.errcode == 0) { 
                            var data = resp.data;

                            if (is_online) {
                                $('.order-info').show();

                                $('.goods_amount_val').empty().text(data.goods_total_format); // 商品总额
                                $('.order_amount').empty().text(data.order_amount_format); // 订单总额

                                // 联营和自营线上
                                if (is_online != 2) {
                                    if (!user_coupon_id) { // 若优惠券ID不存在,则推荐
                                        $('.preferential_price').hide();
                                        $('.preferential_price_val').empty();
                                        
                                        self.getCoupon(user_id, goods_type, cart_ids); // 获取最优的优惠券
                                    } else {
                                        $('.preferential_price').show();
                                        $('.preferential_price_val').empty().text(data.preferential_price_format);
                                    }
                                }

                                if (data.extend_fee) {
                                    $('.extend_fee').show();
                                    $('.extend_fee_val').empty().text(data.extend_fee_format); // 附加费
                                }

                                if (goods_type == 2 && business_type != 2) { // 自营仓库损耗不需要运费
                                    $('.express_fee_val').empty().text(data.finally_shipping_price_format); // 运费
                                } 
                            } else {
                                $('.amount-section').show();
                                $('.order_amount').empty().text(data.order_amount_format); 
                            } 

                            return false;
                        }

                        layer.msg(resp.errmsg || '网络异常');
                    }
                })
            },
        }
    });
})(jQuery)