$(document).ready(function(){
    $('#btnCancel').click(function(){
        if(confirm('确定要取消这个订单吗?')){
        return true;
        }
        return false;
    });
    
    $('#btnCombine').click(function(){
        var count = 0;
        var resp = '';
        $.ajax({
            url:'ajax.aspx?NoCopyRight=1&type=GetCombineOrders',
            dataType:'json',
            cache:false,
            async:false,
            success:function(msg){
                var curOrderID = $('#OrderID').val();
                $('#com_pre_sub').nextAll().remove();
                var data = msg.Orders;
                $.each(data,function(i,n){
                   count = i;
                   var row;
                   if(n.OrderID == curOrderID){
                       row = $('#mainorder');
                   }else{
                       row = $('#mainorder').clone();
                   };
                   
                   row.find('.com_chkbox').html('主订单');
                   row.find('.com_orderno').html(n.OrderNo);
                   row.find('.com_receiver').html(n.ReceiverName);
                   row.find('.com_paytype').html(GetPayType(n.PayType));
                   row.find('.com_ordertime').html(n.OrderTime);
//                   row.find('.com_status').html(n.Status);
                   row.find('.com_pdtprice').html(n.ProductPrice);
                   row.find('.com_totalprice').html(n.TotalPrice);

                   if(n.OrderID != curOrderID){
                       row.find('.com_chkbox').html('<input type="checkbox" name="chk_orders" value="' + n.OrderID + '" />');
                       row.removeAttr('id');
                       $('#datalist').append(row);
                   };
                });
            },
            error:function(){alert('请先登陆!');resp = 'NOLOGIN'}
        });
        if(resp == 'NOLOGIN'){return false;}
        if(count < 1){
            alert('没有搜索到可以合并的订单');
            return false;
        };
        
        //提交选中的订单
        $('#btnSubmitCom').unbind().click(function(){
            if($('[@name=chk_orders][@checked]').length == 0){
                alert('请选择要合并的订单');
                return false;
            }else if(!confirm('确定要合并吗?此操作不能撤回')){
                return false;
            };
        });
        $('#ShowCombine').popup({
            width:600,
            height:300,
            isFixed:true,
            btnClose:'closepop'
        });
    });
    
    //确认已签收
    $('#btnReceived').click(function(){
        if(confirm('如果您已经收到货物，请点击"确认"，否则点击"取消"')){
            $.ajax({
                url:'ajax.aspx?NocopyRight=1&type=ConfirmReceived',
                data:'orderID='+ orderID,
                type:'post',
                cache:false,
                success:function(msg){
                    switch(msg){
                        case 'NOLOGIN':
                            alert('请先登陆！');window.location.href = 'OrderDetail.aspx?OrderID='+orderID;
                            break;
                        case '1':
                            alert('操作失败');
                            break;
                        case '0':
                            alert('操作成功');
//                            $('#btnReceived').remove();
                                window.location.href = 'OrderDetail.aspx?OrderID='+orderID;
                            $('.orderstatustext').text('[已签收]');
                            break;                            
                    }
                },
                error:function(){alert('error');}
            });//end ajax
        };//end if
    });
});

//支付类型
function GetPayType(paytype){
    result = '';
    switch(paytype){
        case '1':
            result = '在线支付';
            break;
        case '2':
            result = '银行卡转账';
            break;
        case '3':
            result = '邮局汇款';
            break;
        case '4':
            result = '公司转账';
            break;
        case '5':
            result = '货到付款';
            break;
        case '6':
            result = '门店自提';
            break;
    };
    return result;
};