using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.BLL.Common;
using TopWinCMS.Common;
using TopWinCMS.Model;
using TopWinCMS.Model.Enum;
namespace TopWinCMS.WebUI.Admin.Shop
{
    public partial class OrderList : System.Web.UI.Page
    {
        int ModelID = 4;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewShowOrderItemDataBind(int.Parse(Request.QueryString["OrderID"]));

            }
        }



        #region  ViewShowOrderItem处理单个订单
        /// <summary>
        /// 订单详细信息视图数据绑定
        /// </summary>
        /// <param name="orderNum"></param>
        protected void ViewShowOrderItemDataBind(int orderId)
        {

            OrdersManage order = new OrdersManage();
            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(orderId);
            if (model == null)
            {
                SwitchOrderListDataBindType();
                return;
            }

            lblOrderId.Text = model.ID.ToString();
            lblOrderNum.Text = model.OrderNum;
            lblInputTime.Text = model.CheckInDate.ToString();
            lblUserName2.Text = model.UserName;
            lblContacterName.Text = model.ContacterName;
            lblContacterName2.Text = model.ContacterName;
            lblAddress.Text = model.Address;
            lblZipCode.Text = model.ZipCode;
            lblPhone.Text = model.ZipCode;
            lblEmail.Text = model.Email;
            lblMobile.Text = model.Mobile;
            if (model.Invoiced == 1)
                lblInvoiced.Text = "√";
            else
                lblInvoiced.Text = "×";

            lblCheckOutStatus.Text = order.GetMoneyReceivedStatusDescription(model.ID);

            lblDeliverStatus.Text = order.GetDeliverStatusDescription(model.ID);

            lblStatus.Text = order.GetOrderStatusDescription(model.ID);


            PaymentMethodManage paymentType = new PaymentMethodManage();
            PaymentMethodInfo paymentTypeModel = new PaymentMethodInfo();
            paymentTypeModel = paymentType.EditLoad(model.PaymentType);
            if (paymentTypeModel != null)
                lblPaymentType.Text = paymentTypeModel.MethodName;

            DeliveryMethodManage deliverType = new DeliveryMethodManage();
            DeliveryMethodInfo deliverTypeModel = new DeliveryMethodInfo();
            deliverTypeModel = deliverType.EditLoad(model.DeliverType);
            if (deliverTypeModel != null)
                lblDeliverType.Text = deliverTypeModel.MethodName;


            lblPresentExp.Text = model.PresentExp.ToString();
            lblMoneyGoods.Text = string.Format("{0:c}", model.MoneyGoods);
            lblChargeDeliver.Text = string.Format("{0:c}", model.ChargeDeliver);

            if (model.NeedInvoice == 1)
                lblNeedInvoice.Text = "√";
            else
                lblNeedInvoice.Text = "×";
            lblInvoiceContent.Text = HttpUtility.HtmlDecode(model.InvoiceContent);
            lblRemark.Text = HttpUtility.HtmlDecode(model.Remark);


            lblMoneyGoods2.Text = string.Format("{0:c}", model.MoneyGoods);
            lblChargeDeliver2.Text = string.Format("{0:c}", model.ChargeDeliver);
            lblChargeDeliver3.Text = string.Format("{0:c}", model.ChargeDeliver);
            lblTotal.Text = string.Format("{0:c}", model.MoneyTotal);
            lblTotal2.Text = string.Format("{0:c}", model.MoneyTotal);
            lblMoneyReceived.Text = string.Format("{0:c}", model.MoneyReceived);
            lblArrearage.Text = string.Format("{0:c}", model.MoneyTotal - model.MoneyReceived);

            OrderListManage orderItem = new OrderListManage();

            gridShowOrderItem.DataSource = orderItem.GetList(model.ID);
            gridShowOrderItem.DataBind();

            //修改订单操作按钮的显示

            btnModifyOrderInfo.Visible = true;
            btnPreConfirmOrder.Visible = true;
            btnCancelConfirmOrder.Visible = true;
            btnDeleteOrder.Visible = true;
            btnRemitAndPay.Visible = true;
            btnAddPaymentorder.Visible = true;
            btnRefund.Visible = true;
            btnSendGoods.Visible = true;
            btnReceived.Visible = true;
            btnReturnGoods.Visible = true;
            btnEndOrder.Visible = true;
            btnInvoice.Visible = true;
            btnPrintOrder.Visible = true;


            //不需要发票
            if (model.NeedInvoice == 0)
            {
                btnInvoice.Visible = false;
            }
            //订单状态
            //假如已经结清
            if (model.Status == (int)TopWinCMS.BLL.Shop.Status.Finished)
            {
                btnModifyOrderInfo.Visible = false;
                btnPreConfirmOrder.Visible = false;
                btnCancelConfirmOrder.Visible = false;
                btnDeleteOrder.Visible = false;
                btnRemitAndPay.Visible = false;
                btnAddPaymentorder.Visible = false;
                btnSendGoods.Visible = false;
                btnReceived.Visible = false;
                btnEndOrder.Visible = false;
            }
            //假如等待确认 
            else if (model.Status == (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm)
            {
                btnCancelConfirmOrder.Visible = false;
                btnRemitAndPay.Visible = false;
                btnAddPaymentorder.Visible = false;
                btnSendGoods.Visible = false;
                btnRefund.Visible = false;
                btnReceived.Visible = false;
                btnReturnGoods.Visible = false;
                btnEndOrder.Visible = false;
                btnInvoice.Visible = false;
            }
            //假如已经确认
            else
            {
                btnPreConfirmOrder.Visible = false;

                TopWinCMS.BLL.Shop.MoneyReceivedStatus moneyReceivedStatus = new TopWinCMS.BLL.Shop.MoneyReceivedStatus();
                moneyReceivedStatus = order.GetMoneyReceivedStatus(model.ID);

                if (model.DeliverStatus != (int)TopWinCMS.BLL.Shop.DeliverStatus.GoodsSending || model.DeliverStatus != (int)TopWinCMS.BLL.Shop.DeliverStatus.SiginIn || moneyReceivedStatus != TopWinCMS.BLL.Shop.MoneyReceivedStatus.WAIT_BUYER_PAY)
                    btnCancelConfirmOrder.Visible = false;

                //订单收款状态
                //假如已付清
                if ((model.Status == (int)TopWinCMS.BLL.Shop.Status.Payoff) || moneyReceivedStatus == TopWinCMS.BLL.Shop.MoneyReceivedStatus.PAYOFF)
                {
                    btnRemitAndPay.Visible = false;
                    btnAddPaymentorder.Visible = false;
                }

                //假如在等待汇款
                if (moneyReceivedStatus == TopWinCMS.BLL.Shop.MoneyReceivedStatus.WAIT_BUYER_PAY)
                {
                    btnRefund.Visible = false;
                }

                //订单送货状态
                //假如已签收
                if (model.DeliverStatus == (int)TopWinCMS.BLL.Shop.DeliverStatus.SiginIn)
                {
                    btnSendGoods.Visible = false;
                    btnReceived.Visible = false;
                    btnRefund.Visible = false;
                }
                //假如已发货
                else if (model.DeliverStatus == (int)TopWinCMS.BLL.Shop.DeliverStatus.GoodsSending)
                {
                    btnSendGoods.Visible = false;
                    btnReturnGoods.Visible = false;
                }
                //假如在配送中 || 物流失败
                else
                {
                    btnReceived.Visible = false;
                    btnReturnGoods.Visible = false;
                }

                //If(  已付清 & 已签收) 才能结清
                if ((model.Status != (int)TopWinCMS.BLL.Shop.Status.Payoff) || (model.DeliverStatus != (int)TopWinCMS.BLL.Shop.DeliverStatus.SiginIn))
                    btnEndOrder.Visible = false;

                if (model.MoneyReceived > 0)
                {
                    btnDeleteOrder.Visible = false;
                }


                //if已开发票
                if (model.Invoiced == 1)
                    btnInvoice.OnClientClick = "return confirm('该订单已开好发票，确定还要开发票吗？')";


            }
            multiViewOrder.SetActiveView(viewShowOrderItem);
        }


        /// <summary>
        /// 订单详细记录绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gridShowOrderItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblProductId = (Label)e.Row.FindControl("lblProductId");
                ShopManage product = new ShopManage(ModelID);
                ShopInfo productModel = new ShopInfo();
                productModel = product.EditLoad(MyConvert.ToInt32(lblProductId.Text));

                if (productModel != null)
                {
                    HyperLink lnkProductName = (HyperLink)e.Row.FindControl("lnkProductName");
                    lnkProductName.Text = productModel.GoodsName;
                    lnkProductName.NavigateUrl = productModel.StaticURL;

                    Label lblUnit = (Label)e.Row.FindControl("lblUnit");
                    lblUnit.Text = productModel.Unit;
                    Label lblRemark = (Label)e.Row.FindControl("lblRemark");
                    lblRemark.Text = HttpUtility.HtmlDecode(lblRemark.Text);
                }

            }
        }


        //转向修改订单视图
        protected void btnModifyOrderInfo_Click(object sender, EventArgs e)
        {
            ViewModifyOrderDataBind(MyConvert.ToInt32(lblOrderId.Text));
            multiViewOrder.SetActiveView(viewModifyOrderInfo);
        }

        //转向确认订单视图
        protected void btnConfirmOrder_Click(object sender, EventArgs e)
        {
            int orderId = MyConvert.ToInt32(lblOrderId.Text);
            if (new OrdersManage().Exists(orderId))
            {
                new OrdersManage().UpdateOrderStatus(orderId, TopWinCMS.BLL.Shop.Status.Confimed);
            }
            ViewShowOrderItemDataBind(orderId);
        }

        //转向取消确定视图
        protected void btnCancelConfirmOrder_Click(object sender, EventArgs e)
        {
            int orderId = MyConvert.ToInt32(lblOrderId.Text);
            if (new OrdersManage().Exists(orderId))
            {
                new OrdersManage().UpdateOrderStatus(orderId, TopWinCMS.BLL.Shop.Status.WaitForConfirm);
            }
            ViewShowOrderItemDataBind(orderId);
        }

        //转向删除订单视图
        protected void btnDeleteOrder_Click(object sender, EventArgs e)
        {
            new OrdersManage().Delete(lblOrderId.Text.Trim());
            SwitchOrderListDataBindType();
        }

        //转向银行汇款支付视图
        protected void btnRemitAndPay_Click(object sender, EventArgs e)
        {
            ViewRemitAndPayDataBind(MyConvert.ToInt32(lblOrderId.Text, -1));
        }

        //转向虚拟货币支付视图
        protected void btnAddPaymentorder_Click(object sender, EventArgs e)
        {
            ViewAddPaymentDataBind(MyConvert.ToInt32(lblOrderId.Text, -1));
        }

        //转向退款视图
        protected void btnRefund_Click(object sender, EventArgs e)
        {
            ViewRefundDataBind(MyConvert.ToInt32(lblOrderId.Text, -1));
        }

        //转向录入发货信息视图
        protected void btnSendGoods_Click(object sender, EventArgs e)
        {
            ViewSaveDeliverInfoDataBind(MyConvert.ToInt32(lblOrderId.Text));
        }

        //客户已签收
        protected void btnReceived_Click(object sender, EventArgs e)
        {
            //更改订单送货状态为客户已签收

            int orderId = MyConvert.ToInt32(lblOrderId.Text);
            OrdersManage order = new OrdersManage();
            if (order.Exists(orderId))
            {
                order.UpdateOrderDeliverStatus(orderId, TopWinCMS.BLL.Shop.DeliverStatus.SiginIn);
            }
            ViewShowOrderItemDataBind(orderId);
        }

        //转向退货信息视图
        protected void btnReturnGoods_Click(object sender, EventArgs e)
        {
            ViewReturnGoodsDataBind(MyConvert.ToInt32(lblOrderId.Text));
            multiViewOrder.SetActiveView(viewReturnGoods);
        }

        //开发票
        protected void btnInvoice_Click(object sender, EventArgs e)
        {
            ViewInvoiceDataBind(MyConvert.ToInt32(lblOrderId.Text));

        }

        //结清订单
        protected void btnEndOrder_Click(object sender, EventArgs e)
        {
            //更改订单状态为已结清,并赠送会员积分
            OrdersManage order = new OrdersManage();
            int orderId = MyConvert.ToInt32(lblOrderId.Text);

            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(MyConvert.ToInt32(lblOrderId.Text));
            if (order.Exists(orderId))
            {
                order.UpdateOrderStatus(orderId, TopWinCMS.BLL.Shop.Status.Finished);

                ViewShowOrderItemDataBind(model.ID);
            }
        }

        //转向打印视图
        protected void btnPrintOrder_Click(object sender, EventArgs e)
        {
            //panelNav.Visible = false;
            //panelSearch.Visible = false;
            ViewPrintOrderDataBind(MyConvert.ToInt32(lblOrderId.Text));
            multiViewOrder.SetActiveView(viewPrintOrder);
        }


        #endregion

        #region viewModifyOrderInfo

        //修改订单视图数据绑定
        protected void ViewModifyOrderDataBind(int orderId)
        {
            OrdersManage order = new OrdersManage();
            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(orderId);
            if (model == null)
                return;
            lblOrderId8.Text = model.ID.ToString();
            lblOrderNum8.Text = model.OrderNum;


            lblInputTime8.Text = model.CheckInDate.ToString();
            lblUserName8.Text = model.UserName;
            lblContacterName8.Text = model.ContacterName;
            lblContacterName8.Text = model.ContacterName;

            if (model.Invoiced == 1)
                lblInvoiced8.Text = "√";
            else
                lblInvoiced8.Text = "×";

 
            lblCheckOutStatus8.Text = order.GetMoneyReceivedStatusDescription(model.ID);

            lblDeliverStatus8.Text = order.GetDeliverStatusDescription(model.ID);

            lblStatus8.Text = order.GetOrderStatusDescription(model.ID);



            if (model.NeedInvoice == 1)
                lblNeedInvoice8.Text = "√";
            else
                lblNeedInvoice8.Text = "×";

            txtContacterName8.Text = model.ContacterName;
            txtAddress8.Text = model.Address;
            txtZipCode8.Text = model.ZipCode;
            txtPhone8.Text = model.ZipCode;
            txtEmail8.Text = model.Email;
            txtMobile8.Text = model.Mobile;



            PaymentMethodManage paymentType = new PaymentMethodManage();
            List<PaymentMethodInfo> paymentTypeDataset = paymentType.GetList();
            drpPaymentType8.Items.Clear();

            if (paymentTypeDataset.Count > 0)
            {
                foreach (PaymentMethodInfo pm in paymentTypeDataset)
                {
                    if (pm.Pass == 1)
                    {
                        ListItem li = new ListItem();
                        li.Text = pm.MethodName;
                        li.Value = pm.ID.ToString();


                        drpPaymentType8.Items.Add(li);
                    }
                }
                drpPaymentType8.SelectedValue = model.PaymentType.ToString();
            }


            DeliveryMethodManage deliverType = new DeliveryMethodManage();
            IList<DeliveryMethodInfo> deliverTypeDataset = new List<DeliveryMethodInfo>();
            deliverTypeDataset = deliverType.GetList();
            drpDeliverType8.Items.Clear();

            if (deliverTypeDataset.Count > 0)
            {
                foreach (DeliveryMethodInfo pm in deliverTypeDataset)
                {
                    if (pm.Pass == 1)
                    {
                        ListItem li = new ListItem();
                        li.Text = pm.MethodName;
                        li.Value = pm.ID.ToString();
                        li.Attributes["title"] = Server.HtmlEncode(pm.Intro);

                        drpDeliverType8.Items.Add(li);
                    }
                }
                drpDeliverType8.SelectedValue = model.DeliverType.ToString();
            }


            if (model.NeedInvoice == 1)
                chkNeedInvoice8.Checked = true;
            else
                chkNeedInvoice8.Checked = false;
            txtChargeDeliver8.Text = model.ChargeDeliver.ToString("N2");
            txtInvoiceContent8.Text = HttpUtility.HtmlDecode(model.InvoiceContent).Replace("<br>", "\r\n");
            txtRemark8.Text = HttpUtility.HtmlDecode(model.Remark).Replace("<br>", "\r\n"); ;

            //if订单已经确认,将订单商品数据锁定

            drpDeliverType8.Enabled = model.Status == (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm ? true : false;
            drpPaymentType8.Enabled = model.Status == (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm ? true : false;
            txtChargeDeliver8.ReadOnly = model.Status == (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm ? false : true;
            chkNeedInvoice8.Enabled = model.Status == (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm ? true : false;


            OrderListManage orderItem = new OrderListManage();

            gridModifyOrderItems.DataSource = orderItem.GetList(model.ID);
            gridModifyOrderItems.DataBind();

        }




        // 商品详细记录数据绑定
        protected void gridModifyOrderItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                OrdersInfo orderModel = new OrdersInfo();
                OrdersManage order = new OrdersManage();
                orderModel = order.EditLoad(MyConvert.ToInt32(lblOrderId8.Text));
                if (orderModel == null)
                    return;

                TextBox txtQuantity8 = (TextBox)e.Row.FindControl("txtQuantity8");
                TextBox txtPrice8 = (TextBox)e.Row.FindControl("txtPrice8");

                txtQuantity8.ReadOnly = orderModel.Status != (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm ? true : false;
                txtPrice8.ReadOnly = orderModel.Status != (int)TopWinCMS.BLL.Shop.Status.WaitForConfirm ? true : false;

                Label lblProductId = (Label)e.Row.FindControl("lblProductId8");
                ShopManage product = new ShopManage(ModelID);
                ShopInfo productModel = new ShopInfo();
                productModel = product.EditLoad(MyConvert.ToInt32(lblProductId.Text));

                if (productModel != null)
                {
                    Label txtProductName = (Label)e.Row.FindControl("txtProductName8");
                    txtProductName.Text = productModel.GoodsName;

                    Label txtUnit = (Label)e.Row.FindControl("txtUnit8");
                    txtUnit.Text = productModel.Unit;
                }

            }

        }

        //根据选择送货方式自动填充运费项目
        protected void listDeliverType8_SelectedIndexChanged(object sender, EventArgs e)
        {
            DeliveryMethodManage deliver = new DeliveryMethodManage();
            DeliveryMethodInfo deliverTypeModel = new DeliveryMethodInfo();
            int deliverType = MyConvert.ToInt32(drpDeliverType8.SelectedValue);
            deliverTypeModel = deliver.EditLoad(deliverType);
            txtChargeDeliver8.Text = deliverTypeModel.Charge.ToString();
        }

        //取消修改订单
        protected void btnCancelModifyOrder_Click(object sender, EventArgs e)
        {
            ViewShowOrderItemDataBind(MyConvert.ToInt32(lblOrderId8.Text));
            multiViewOrder.SetActiveView(viewShowOrderItem);
        }

        // 修改订单(orders表)
        protected void btnConfirmModifyOrder_Click(object sender, EventArgs e)
        {
            int needInvoice = 0;

            if (chkNeedInvoice8.Checked)
                needInvoice = 1;
            string remark = (txtRemark8.Text.Trim());
            string contacterName = (txtContacterName8.Text.Trim());
            string address = (txtAddress8.Text.Trim());
            string zipCode = (txtZipCode8.Text.Trim());
            string mobile = (txtMobile8.Text.Trim());
            string phone = (txtPhone8.Text.Trim());
            string email = (txtEmail8.Text.Trim());


            int paymentType = MyConvert.ToInt32(drpPaymentType8.Text);
            int deliverType = MyConvert.ToInt32(drpDeliverType8.Text);
            decimal chargeDeliver = decimal.Parse(txtChargeDeliver8.Text.Trim());

            string invoiceContent = (txtInvoiceContent8.Text.Trim());

            //将订单写入数据库
            OrdersInfo orderModel = new OrdersInfo();
            OrdersManage order = new OrdersManage();
            orderModel = order.EditLoad(MyConvert.ToInt32(lblOrderId8.Text));
            if (orderModel == null)
                return;
            orderModel.ContacterName = HttpUtility.HtmlEncode(contacterName);
            orderModel.Address = HttpUtility.HtmlEncode(address);
            orderModel.Email = HttpUtility.HtmlEncode(email);
            orderModel.PaymentType = paymentType;
            orderModel.NeedInvoice = needInvoice;
            orderModel.InvoiceContent = HttpUtility.HtmlEncode(invoiceContent);
            orderModel.Phone = HttpUtility.HtmlEncode(phone);
            orderModel.ZipCode = HttpUtility.HtmlEncode(zipCode);
            orderModel.Mobile = HttpUtility.HtmlEncode(mobile);
            orderModel.DeliverType = deliverType;
            orderModel.ChargeDeliver = chargeDeliver;
            orderModel.Remark = HttpUtility.HtmlEncode(remark);
            orderModel.MoneyTotal = orderModel.MoneyGoods + chargeDeliver;

            string Err = null;
            order.Add(orderModel, null, ref Err);//修改订单

            if (string.IsNullOrEmpty(Err))
            {
                ViewModifyOrderDataBind(MyConvert.ToInt32(lblOrderId.Text));
                MsgBox.Alert("修改完成!", UpdatePanel1);
            }
            else
            {
                MsgBox.Alert(Err, UpdatePanel1);
            }
        }


        //修改订单详细记录（orderItems表)
        protected void gridModifyOrderItems_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //修改orderItems表中对应的记录的信息
            int index = e.NewSelectedIndex;
            Decimal truePrice = 0;
            int Quantity = 0;
            TextBox txtQuantity8 = (TextBox)gridModifyOrderItems.Rows[index].FindControl("txtQuantity8");
            TextBox txtPrice = (TextBox)gridModifyOrderItems.Rows[index].FindControl("txtPrice8");
            TextBox txtRemark = (TextBox)gridModifyOrderItems.Rows[index].FindControl("txtRemark8");
            Label lblItemId = (Label)gridModifyOrderItems.Rows[index].FindControl("lblItemId8");

            //提示输入错误信息
            if (!Decimal.TryParse(txtPrice.Text, out truePrice))
            {
                string errMsg = "指定价输入错误，请重新输入！";
                MsgBox.Alert(errMsg, UpdatePanel1); ;
                return;
            }

            if (!int.TryParse(txtQuantity8.Text, out Quantity))
            {
                string errMsg = "商品数量输入错误，请重新输入！";
                MsgBox.Alert(errMsg, UpdatePanel1); ;
                return;
            }

            //修改后的差价，用来更正orders表的MoneyGoods的值
            //Decimal priceDifference = 0;
            //int QuantityDifference = 0;

            OrderListManage orderItem = new OrderListManage();
            OrderListInfo orderItemModel = new OrderListInfo();
            orderItemModel = orderItem.EditLoad(MyConvert.ToInt32(lblItemId.Text.Trim()));
            if (orderItemModel != null)
            {
                //QuantityDifference = Math.Abs(orderItemModel.Quantity - Quantity);

                ////计算差价
                //priceDifference = (orderItemModel.OriginalPrice - truePrice) * QuantityDifference;

                //orderItemModel.OriginalPrice = truePrice;
                orderItemModel.Price= truePrice;
                orderItemModel.SubTotal = truePrice * Quantity;
                orderItemModel.Remark = (txtRemark.Text.Trim());

                string Err = null;
                orderItem.Edit(orderItemModel, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert(Err, UpdatePanel1);
                    return;
                }
                else
                {
                    MsgBox.Alert("已修改.", UpdatePanel1);
                }
            }

            ////修改orders表中对应的记录的信息
            //OrdersInfo orderModel = new OrdersInfo();
            //OrdersManage order = new OrdersManage();
            //orderModel = order.EditLoad(MyConvert.StrToInt(lblOrderId8.Text));
            //if (orderModel != null)
            //{
            //    orderModel.MoneyGoods = orderModel.MoneyGoods - priceDifference;
            //    orderModel.MoneyTotal = orderModel.MoneyGoods + orderModel.ChargeDeliver;
            //    orderModel.Remark = HttpUtility.HtmlEncode((txtRemark8.Text));

            //    string Err = null;
            //    order.Add(orderModel, null, ref Err);
            //    if (string.IsNullOrEmpty(Err) == false)
            //    {
            //        MsgBox.Alert(Err, UpdatePanel1);
            //        return;
            //    }
            //}

            //重新绑定数据
            ViewModifyOrderDataBind(MyConvert.ToInt32(lblOrderId8.Text));
        }
        #endregion

        #region viewRemitAndPay

        //填充视图数据
        protected void ViewRemitAndPayDataBind(int orderId)
        {
            OrdersManage order = new OrdersManage();
            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(orderId);
            string msg;
            if (model != null)
            {
                if (order.IsPaid(model.ID))
                {

                    msg = " 该订单已付清！";
                    MsgBox.Alert(msg, UpdatePanel1);

                    ViewShowOrderItemDataBind(orderId);
                    multiViewOrder.SetActiveView(viewShowOrderItem);
                    return;
                }
                lblOrderId9.Text = orderId.ToString();
                lblOrderNum9.Text = model.OrderNum;
                lblMoneyTotal9.Text = string.Format("{0:0}", model.MoneyTotal);
                lblUserName9.Text = model.UserName;
                lblContacterName9.Text = model.ContacterName;
                lblMoneyReceived9.Text = string.Format("{0:0}", model.MoneyReceived);
                txtMoney9.Text = Convert.ToString((model.MoneyTotal - model.MoneyReceived));
                lblMoneyMsg9.Text = string.Empty;
                txtDateAndTime9.Text = DateTime.Now.ToString("yyyy-MM-dd");
                drpBank9_DataBind();
                multiViewOrder.SetActiveView(viewRemitAndPay);

            }
            else
            {
                msg = "该订单不存在！";
                MsgBox.Alert(msg, UpdatePanel1);

                ViewShowOrderItemDataBind(orderId);
                multiViewOrder.SetActiveView(viewShowOrderItem);
                return;
            }

        }
        private void drpBank9_DataBind()
        {
            BankManage bank = new BankManage();
            drpBank9.DataSource = bank.GetList();
            drpBank9.DataTextField = "BankName";
            drpBank9.DataValueField = "ID";
            drpBank9.DataBind();
        }



        //保存汇款信息
        protected void btnSaveRemitAndPayInfo_Click(object sender, EventArgs e)
        {
            //判断输入信息有没有出错

            Decimal money = 0;
            bool isErr = false;
            OrdersManage order = new OrdersManage();
            OrdersInfo orderModel = new OrdersInfo();
            orderModel = order.EditLoad(MyConvert.ToInt32(lblOrderId9.Text));
            if (orderModel != null)
            {
                if (order.IsPaid(orderModel.ID))
                {
                    ViewShowOrderItemDataBind(orderModel.ID);
                    multiViewOrder.SetActiveView(viewShowOrderItem);
                    return;
                }
            }
            DateTime dateAndTime = DateTime.Now;
            if (!DateTime.TryParse(txtDateAndTime9.Text.Trim(), out dateAndTime))
            {
                isErr = true;
            }
            if (!decimal.TryParse(txtMoney9.Text.Trim(), out money))
            {
                isErr = true;
                lblMoneyMsg9.Text = "汇款金额输入错误！";
            }
            if (money == 0)
            {
                isErr = true;
                lblMoneyMsg9.Text = "汇款金额不能为０！";
            }
            if (money > (orderModel.MoneyTotal - orderModel.MoneyReceived))
            {
                isErr = true;
                lblMoneyMsg9.Text = "汇款金额不能大于所欠货款！";
            }
            if (isErr)
            {
                return;
            }
            if (orderModel != null)
            {
                //保存信息到bankrollItem表（先汇入）
                BankrollListManage bankroolItem = new BankrollListManage();
                BankrollListInfo model = new BankrollListInfo();
                model.OrderID = orderModel.ID;
                model.UserName = lblUserName9.Text;
                //model.UserName = orderModel.UserName;
                model.CheckInDate = DateTime.Now;
                model.Money = money;
                model.MoneyType = (int)BLL.Shop.HowPay.Remit;
                model.CurrencyType = (int)BLL.Shop.CurrencyType.RMB;
                model.eBankID = int.Parse(drpBank9.SelectedValue);
                model.Bank = drpBank9.Text;
                model.Income_PayOut = (int)BLL.Shop.Income_Payout.Income;
                model.ID = 0;
                model.PaymentID = 0;
                model.Remark = "用户汇款";
                model.LogTime = DateTime.Now;
                model.Admin = AdminLoginInfo.UserName;
                model.IP = HttpClient.GetIP();

                string Err = null;
                bankroolItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert(Err, UpdatePanel1);
                    return;
                }
                //保存信息到bankrollItem表（再扣除）
                model.Money = -money;
                model.MoneyType = (int)BLL.Shop.HowPay.PayByBalance;
                model.eBankID = 0;
                model.Bank = string.Empty;
                model.Income_PayOut = (int)BLL.Shop.Income_Payout.Payout;
                model.ID = MyConvert.ToInt32(lblOrderId9.Text);
                model.Remark = "支付订单费用，订单号：" + orderModel.OrderNum + "  " + txtRemark9.Text;


                string msg = string.Empty;

                bankroolItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert("系统错误发生错误，请重新汇款！ ", UpdatePanel1);
                    return;
                }

                //更新订单信息

                orderModel.MoneyReceived = money + orderModel.MoneyReceived;
                if (orderModel.MoneyReceived > 0)
                {
                    orderModel.Status = (int)TopWinCMS.BLL.Shop.Status.ReceivedEarnest;
                    if (orderModel.MoneyReceived == orderModel.MoneyTotal)
                        orderModel.Status = (int)TopWinCMS.BLL.Shop.Status.Payoff;
                }
                order.Add(orderModel, null, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert(Err, UpdatePanel1);
                    return;
                }
                //提示成功信息

                msg = "操作成功！";
                MsgBox.Alert(msg, UpdatePanel1);
                ViewShowOrderItemDataBind(orderModel.ID);
                multiViewOrder.SetActiveView(viewShowOrderItem);

            }

        }

        #endregion

        #region viewRefund
        protected void ViewRefundDataBind(int orderId)
        {
            OrdersManage order = new OrdersManage();
            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(orderId);
            string msg;
            if (model != null)
            {
                if (model.MoneyReceived <= 0)
                {
                    msg = "该订单所付货款为0！";

                    MsgBox.Alert(msg, UpdatePanel1);

                    ViewShowOrderItemDataBind(orderId);
                    multiViewOrder.SetActiveView(viewShowOrderItem);
                    return;

                }


                lblOrderId7.Text = orderId.ToString();
                lblOrderNum7.Text = model.OrderNum;
                lblMoneyTotal7.Text = string.Format("{0:0}", model.MoneyTotal);
                lblUserName7.Text = model.UserName;
                lblContacterName7.Text = model.ContacterName;
                lblMoneyReceived7.Text = string.Format("{0:0}", model.MoneyReceived);
                txtMoney7.Text = Convert.ToString(model.MoneyReceived);
                lblMoneyMsg7.Text = "<span style=\"color: #0000ff\">退款金额将从已付款中扣除。</span>";
                txtDateAndTime7.Text = DateTime.Now.ToString();
                txtRemark7.Text = " 订单退款金额，订单号：" + model.OrderNum;

                multiViewOrder.SetActiveView(viewRefund);

            }
            else
            {
                msg = "该订单不存在！";
                MsgBox.Alert(msg, UpdatePanel1);
                ViewShowOrderItemDataBind(orderId);
                multiViewOrder.SetActiveView(viewShowOrderItem);
            }
        }

        //保存退款信息
        protected void btnRefund7_Click(object sender, EventArgs e)
        {
            OrdersManage order = new OrdersManage();
            OrdersInfo orderModel = new OrdersInfo();
            orderModel = order.EditLoad(MyConvert.ToInt32(lblOrderId7.Text));

            string msg = string.Empty;

            if (orderModel != null)
            {
                if (orderModel.MoneyReceived <= 0)
                {
                    msg = "该订单所付货款为0！";
                    MsgBox.Alert(msg, UpdatePanel1);

                    ViewShowOrderItemDataBind(orderModel.ID);
                    multiViewOrder.SetActiveView(viewShowOrderItem);
                    return;
                }
            }

            else
            {
                msg = "该订单不存在！";
                MsgBox.Alert(msg, UpdatePanel1);

                ViewShowOrderItemDataBind(orderModel.ID);
                multiViewOrder.SetActiveView(viewShowOrderItem);
                return;
            }


            //判断输入信息有没有出错
            DateTime dateAndTime = DateTime.Now;
            Decimal money = 0;
            bool isErr = false;
            if (!DateTime.TryParse(txtDateAndTime7.Text.Trim(), out dateAndTime))
            {
                isErr = true;
            }
            if (!decimal.TryParse(txtMoney7.Text.Trim(), out money))
            {
                isErr = true;
                lblMoneyMsg7.Text = "退款金额输入错误！";
            }
            if (money > orderModel.MoneyReceived)
            {
                isErr = true;
                lblMoneyMsg7.Text = "退款金额不能大于订单已付款！";
            }

            if (money <= 0)
            {
                isErr = true;
                lblMoneyMsg7.Text = "退款金额须为正数！";
            }
            if (isErr)
            {
                return;
            }
            if (orderModel != null)
            {
                //保存信息到bankrollItem表
                BankrollListManage bankroolItem = new BankrollListManage();
                BankrollListInfo model = new BankrollListInfo();
                model.UserName = lblUserName7.Text;
                //model.ClientId = orderModel.ClientId;
                model.CheckInDate = dateAndTime;
                if (radRefundType.SelectedValue == "0")
                    model.Money = money;
                else
                    model.Money = 0;
                model.MoneyType = (int)TopWinCMS.BLL.Shop.HowPay.PayByBalance;
                model.CurrencyType = (int)TopWinCMS.BLL.Shop.CurrencyType.RMB;
                model.eBankID = 0;
                model.Bank = string.Empty;
                model.Income_PayOut = (int)TopWinCMS.BLL.Shop.Income_Payout.Income;
                model.ID = MyConvert.ToInt32(lblOrderId7.Text);
                model.PaymentID = 0;
                model.Remark = txtRemark7.Text;
                model.LogTime = DateTime.Now;
                model.Admin = AdminLoginInfo.UserName;
                model.IP = TopWinCMS.Common.HttpClient.GetIP();

                string Err = null;
                bankroolItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert("系统错误发生错误，请重新汇款！ ", UpdatePanel1);
                    return;
                }
                //更新订单信息
                //将订单状态结清改为已经确认

                orderModel.MoneyReceived = orderModel.MoneyReceived - money;

                if (orderModel.MoneyReceived <= 0)
                    orderModel.Status = (int)TopWinCMS.BLL.Shop.Status.Confimed;
                else
                    orderModel.Status = (int)TopWinCMS.BLL.Shop.Status.ReceivedEarnest;

                order.Add(orderModel, null, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert(Err, UpdatePanel1);
                    return;
                }

                //if选择扣除的金额添加到会员资金余额中
                if (radRefundType.SelectedValue == "0")
                {
                    //TopWinCMS.BLL.MemberManage user = new User();
                    //MemberInfo userModel = new MemberInfo();
                    //userModel = user.EditLoad(model.UserName);
                    //if (userModel != null)
                    //{
                    //    userModel.Balance = userModel.Balance + money;
                    //    user.Update(userModel);
                    //}
                }

                //提示成功信息

                MsgBox.Alert("操作成功！", UpdatePanel1);

                ViewShowOrderItemDataBind(orderModel.ID);
                multiViewOrder.SetActiveView(viewShowOrderItem);
                return;

            }
        }
        #endregion

        #region viewSaveDeliverInfo

        /// <summary>
        /// 绑定录入信息视图数据
        /// </summary>
        /// <param name="orderId"></param>
        protected void ViewSaveDeliverInfoDataBind(int orderId)
        {
            OrdersInfo orderModel = new OrdersInfo();
            OrdersManage order = new OrdersManage();
            orderModel = order.EditLoad(orderId);
            if (orderModel != null)
            {
                TopWinCMS.BLL.MemberManage user = new TopWinCMS.BLL.MemberManage();

                if (user.GetInfo(orderModel.UserName) != null)
                {
                    lblRealName5.Text = user.GetInfo(orderModel.UserName).TrueName;
                }
                lblOrderId5.Text = orderId.ToString();
                lblUserName5.Text = orderModel.UserName;
                lblContacterName5.Text = orderModel.ContacterName;
                lblOrderNum5.Text = orderModel.OrderNum;
                lblMoneyTotal5.Text = string.Format("{0:c}", orderModel.MoneyTotal);
                lblMoneyReceived5.Text = string.Format("{0:c}", orderModel.MoneyReceived);
                lblHandlerNameMsg5.Text = string.Empty;
                lblDeliverDateMsg5.Text = string.Empty;

                DeliveryMethodManage deliverType = new DeliveryMethodManage();
                DeliveryMethodInfo deliverTypeModel = new DeliveryMethodInfo();
                deliverTypeModel = deliverType.EditLoad(orderModel.DeliverType);
                if (deliverTypeModel != null)
                {
                    lblDeliverTypeName5.Text = deliverTypeModel.MethodName;
                }

                txtDeliverDate5.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");

                multiViewOrder.SetActiveView(viewSaveDeliverInfo);

            }
        }

        //录入发货信息到deliverItem表
        protected void btnDeliverConfirm_Click(object sender, EventArgs e)
        {
            //检查输入信息

            DateTime deliverDate = DateTime.Now;
            string expressCompany = (txtExpressCompany5.Text.Trim());
            string expressNum = (txtExpressNum5.Text.Trim());
            string handlerName = (txtHandlerName5.Text.Trim());
            string remark = (txtRemark5.Text.Trim());

            bool isErr = false;
            //if (!DateTime.TryParse(txtDeliverDate5.Text.Trim(), out deliverDate))
            //{
            //    isErr = true;
            //    lblDeliverDateMsg5.Text = "发货时间格式错误！";
            //}
            if (string.IsNullOrEmpty(handlerName))
            {
                isErr = true;
                lblHandlerNameMsg5.Text = "请输入经手人！";
            }
            if (isErr)
                return;

            //录入发货信息到deliverItem表

            DeliveryInfo model = new DeliveryInfo();
            DeliveryManage deliverItem = new DeliveryManage();

            model.OrderID = MyConvert.ToInt32(lblOrderId5.Text.ToString());
            model.DeliverDate = deliverDate;
            model.DeliverDirection = (int)BLL.Shop.DeliverDirection.SendGoods;
            model.HandlerName = handlerName;
            model.ExpressCompany = expressCompany;
            model.ExpressNumber = expressNum;
            model.Admin = AdminLoginInfo.UserName;
            model.Remark = remark;
            model.Served = 0;



            string msg = string.Empty;
            string Err = null;
            deliverItem.Add(model, ref Err);

            if (string.IsNullOrEmpty(Err))
            {
                //更新订单信息
                OrdersManage order = new OrdersManage();
                OrdersInfo orderModel = new OrdersInfo();
                orderModel = order.EditLoad(model.OrderID);
                orderModel.DeliverStatus = (int)TopWinCMS.BLL.Shop.DeliverStatus.GoodsSending;
                order.Add(orderModel, null, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert(Err, UpdatePanel1);
                    return;
                }


                //更新product表库存量
                OrderListManage orderItem = new OrderListManage();
                OrderListInfo orderItemModel = new OrderListInfo();
                List<OrderListInfo> orderItemDataset = new List<OrderListInfo>();
                orderItemDataset = orderItem.GetList(orderModel.ID);
                ShopManage product = new ShopManage(ModelID);
                ShopInfo productModel = new ShopInfo();

                foreach (OrderListInfo dr in orderItemDataset)
                {
                    orderItemModel = orderItem.EditLoad(dr.ID);
                    productModel = product.EditLoad(orderItemModel.GoodsID);
                    //productModel.OrderNum = productModel.OrderNum - orderItemModel.Quantity;
                    productModel.Stocks = productModel.Stocks - orderItemModel.Quantity;
                    productModel.BuyTimes = productModel.BuyTimes + orderItemModel.Quantity;

                    product.Add(productModel, "", new List<string>(), new List<object>(), ref Err);

                    if (string.IsNullOrEmpty(Err) == false)
                    {
                        MsgBox.Alert(Err, UpdatePanel1);
                        return;
                    }
                }


                //提示成功信息

                msg = "操作成功！";
                MsgBox.Alert(msg, UpdatePanel1);

                ViewShowOrderItemDataBind(model.OrderID);
                multiViewOrder.SetActiveView(viewShowOrderItem);
            }
            else
            {
                msg = "系统错误发生错误，请重新发货！";
                MsgBox.Alert(msg, UpdatePanel1);
            }
        }

        #endregion

        #region viewReturnGoods
        protected void ViewReturnGoodsDataBind(int orderId)
        {
            OrdersInfo orderModel = new OrdersInfo();
            OrdersManage order = new OrdersManage();
            orderModel = order.EditLoad(orderId);
            if (orderModel != null)
            {
                TopWinCMS.BLL.MemberManage user = new TopWinCMS.BLL.MemberManage();
                if (user.GetInfo(orderModel.UserName) != null)
                {
                    lblRealName4.Text = user.GetInfo(orderModel.UserName).TrueName;
                }
                lblOrderId4.Text = orderId.ToString();
                lblUserName4.Text = orderModel.UserName;
                lblContacterName4.Text = orderModel.ContacterName;
                lblOrderNum4.Text = orderModel.OrderNum;
                lblMoneyTotal4.Text = string.Format("{0:c}", orderModel.MoneyTotal);
                lblMoneyReceived4.Text = string.Format("{0:c}", orderModel.MoneyReceived);
                lblHandlerNameMsg4.Text = string.Empty;
                DeliveryMethodManage deliverType = new DeliveryMethodManage();
                DeliveryMethodInfo deliverTypeModel = new DeliveryMethodInfo();
                deliverTypeModel = deliverType.EditLoad(orderModel.DeliverType );
                if (deliverTypeModel != null)
                {
                    lblDeliverTypeName4.Text = deliverTypeModel.MethodName;
                }

                txtDeliverDate4.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
        }

        protected void btnReturnGoodsConfirm4_Click(object sender, EventArgs e)
        {

            //检查输入信息

            DateTime returnGoodsDate;
            string expressCompany = string.Empty;
            string expressNum = string.Empty;
            string handlerName = (txtHandlerName4.Text.Trim());
            string remark = (txtRemark4.Text.Trim());

            bool isErr = false;
            if (!DateTime.TryParse(txtDeliverDate4.Text.Trim(), out returnGoodsDate))
            {
                isErr = true;
                lblDeliverDateMsg4.Text = "发货时间格式错误！";
            }
            if (string.IsNullOrEmpty(handlerName))
            {
                isErr = true;
                lblHandlerNameMsg4.Text = "请输入经手人！";
            }
            if (isErr)
                return;

            //录入退货信息到deliverItem表

            DeliveryInfo model = new DeliveryInfo();
            DeliveryManage deliverItem = new DeliveryManage();

            model.OrderID = MyConvert.ToInt32(lblOrderId4.Text.ToString());
            model.DeliverDate = returnGoodsDate;
            model.DeliverDirection = (int)BLL.Shop.DeliverDirection.ReturnGoods;
            model.HandlerName = handlerName;
            model.ExpressCompany = expressCompany;
            model.ExpressNumber = expressNum;
            model.Admin = AdminLoginInfo.UserName;
            model.Remark = remark;


            OrdersManage order = new OrdersManage();
            OrdersInfo orderModel = new OrdersInfo();
            orderModel = order.EditLoad(model.OrderID);

            if (orderModel != null)
                model.Served = orderModel.DeliverStatus;


            string Err = null;
            deliverItem.Add(model, ref Err);

            if (string.IsNullOrEmpty(Err))
            {
                //更新订单信息
                orderModel.DeliverStatus = (int)TopWinCMS.BLL.Shop.DeliverStatus.Preparative;
                order.Add(orderModel, null, ref Err);
                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Alert(Err, UpdatePanel1);
                    return;
                }


                //更新product表库存量和订单数量
                OrderListManage orderItem = new OrderListManage();
                OrderListInfo orderItemModel = new OrderListInfo();
                List<OrderListInfo> orderItemDataset = new List<OrderListInfo>();
                orderItemDataset = orderItem.GetList(orderModel.ID);
                ShopManage product = new ShopManage(ModelID);
                ShopInfo productModel = new ShopInfo();


                foreach (OrderListInfo dr in orderItemDataset)
                {
                    orderItemModel = orderItem.EditLoad(dr.ID);
                    productModel = product.EditLoad(orderItemModel.GoodsID);
                    //productModel.GoodsNum = productModel.GoodsNum + orderItemModel.Quantity;
                    productModel.Stocks = productModel.Stocks + orderItemModel.Quantity;
                    productModel.BuyTimes = productModel.BuyTimes - orderItemModel.Quantity;

                    product.Add(productModel, "", new List<string>(), new List<object>(), ref Err);
                }


                //提示成功信息

                MsgBox.Alert("操作成功！", UpdatePanel1);

                ViewShowOrderItemDataBind(model.OrderID);
                multiViewOrder.SetActiveView(viewShowOrderItem);
            }
            else
            {
                MsgBox.Alert("系统错误发生错误，请重新退货！", UpdatePanel1);
            }
        }

        #endregion

        #region viewPrintOrder
        /// <summary>
        /// 打印订单视图数据绑定
        /// </summary>
        /// <param name="orderId"></param>
        protected void ViewPrintOrderDataBind(int orderId)
        {

            OrdersManage order = new OrdersManage();
            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(orderId);
            lblOrderId6.Text = model.ID.ToString();
            lblOrderNum6.Text = model.OrderNum;
            lblInputTime6.Text = model.CheckInDate.ToString();
            lblUserName6.Text = model.UserName;
            lblContacterName6.Text = model.ContacterName;
            lblContacterName62.Text = model.ContacterName;
            lblAddress6.Text = model.Address;
            lblZipCode6.Text = model.ZipCode;
            lblPhone6.Text = model.ZipCode;
            lblEmail6.Text = model.Email;
            lblMobile6.Text = model.Mobile;
            if (model.Invoiced == 1)
                lblInvoiced6.Text = "√";
            else
                lblInvoiced6.Text = "×";


            lblCheckOutStatus6.Text = order.GetMoneyReceivedStatusDescription(model.ID);

            lblDeliverStatus6.Text = order.GetDeliverStatusDescription(model.ID);

            lblStatus6.Text = order.GetOrderStatusDescription(model.ID);



            PaymentMethodManage paymentType = new PaymentMethodManage();
            PaymentMethodInfo paymentTypeModel = new PaymentMethodInfo();
            paymentTypeModel = paymentType.EditLoad(model.PaymentType);
            if (paymentTypeModel != null) lblPaymentType6.Text = paymentTypeModel.MethodName;

            DeliveryMethodManage deliverType = new DeliveryMethodManage();
            DeliveryMethodInfo deliverTypeModel = new DeliveryMethodInfo();
            deliverTypeModel = deliverType.EditLoad(model.DeliverType);
            if (deliverTypeModel != null) lblDeliverType6.Text = deliverTypeModel.MethodName;


            lblPresentExp6.Text = model.PresentExp.ToString();
            lblMoneyGoods6.Text = string.Format("{0:c}", model.MoneyGoods);
            lblChargeDeliver6.Text = string.Format("{0:c}", model.ChargeDeliver);

            if (model.NeedInvoice == 1)
                lblNeedInvoice6.Text = "√";
            else
                lblNeedInvoice6.Text = "×";
            lblInvoiceContent6.Text = HttpUtility.HtmlDecode(model.InvoiceContent).Replace("\r\n", "<br>");
            lblRemark6.Text = HttpUtility.HtmlDecode(model.Remark).Replace("\r\n", "<br>");


            lblMoneyGoods62.Text = string.Format("{0:c}", model.MoneyGoods);
            lblChargeDeliver62.Text = string.Format("{0:c}", model.ChargeDeliver);
            lblChargeDeliver63.Text = string.Format("{0:c}", model.ChargeDeliver);
            lblTotal6.Text = string.Format("{0:c}", model.MoneyTotal);
            lblTotal62.Text = string.Format("{0:c}", model.MoneyTotal);
            lblMoneyReceived6.Text = string.Format("{0:c}", model.MoneyReceived);
            lblArrearage6.Text = string.Format("{0:c}", model.MoneyTotal - model.MoneyReceived);


            OrderListManage orderItem = new OrderListManage();

            gridPrintOrder6.DataSource = orderItem.GetList(model.ID);
            gridPrintOrder6.DataBind();
        }
        protected void gridPrintOrder6_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblProductId = (Label)e.Row.FindControl("lblProductId");
                ShopManage product = new ShopManage(ModelID);
                ShopInfo productModel = new ShopInfo();
                productModel = product.EditLoad(MyConvert.ToInt32(lblProductId.Text));

                if (productModel != null)
                {
                    Label lblProductName = (Label)e.Row.FindControl("lblProductName");
                    lblProductName.Text = productModel.GoodsName;
                    Label lblUnit = (Label)e.Row.FindControl("lblUnit");
                    lblUnit.Text = productModel.Unit;
                    Label lblRemark = (Label)e.Row.FindControl("lblRemark");
                    lblRemark.Text = HttpUtility.HtmlDecode(lblRemark.Text);
                }

            }

        }


        //取消打印，查看订单　
        protected void btnToViewOrder6_Click(object sender, EventArgs e)
        {
            //panelSearch.Visible = true;
            //panelNav.Visible = true;
            ViewShowOrderItemDataBind(MyConvert.ToInt32(lblOrderId6.Text));
            multiViewOrder.SetActiveView(viewShowOrderItem);
        }


        #endregion

        #region viewAddPayment 暂时作废。
        /// <summary>
        /// 余额支付数据填充
        /// </summary>
        /// <param name="orderId"></param>
        /// <remarks>暂时作废。</remarks>
        public void ViewAddPaymentDataBind(int orderId)
        {
            //OrdersManage order = new OrdersManage();
            //OrdersInfo model = new OrdersInfo();
            //model = order.EditLoad(orderId);


            //MemberManage user = new MemberManage();
            //MemberInfo userModel = new MemberInfo();
            //userModel = user.GetInfo("");

            //string msg;
            //if (model != null && userModel != null)
            //{
            //    if (order.IsPaid(model.ID))
            //    {

            //        msg = "该订单已付清！";
            //        MsgBox.Alert(msg, UpdatePanel1);
            //        ViewShowOrderItemDataBind(orderId);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //        return;
            //    }
            //    if (userModel.Balance < (model.MoneyTotal - model.MoneyReceived))
            //    {
            //        msg = "您的资金余额不足！";
            //        MsgBox.Alert(msg, UpdatePanel1);

            //        ViewShowOrderItemDataBind(orderId);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //        return;

            //    }


            //    lblAddPayment_orderId.Text = orderId.ToString();
            //    lblAddPayment_balance.Text = userModel.Balance.ToString();

            //    lblAddPayment_moneyTotal.Text = model.MoneyTotal.ToString();
            //    lblAddPayment_orderNum.Text = model.OrderNum;
            //    lblAddPayment_moneyReceived.Text = model.MoneyReceived.ToString();
            //    lblAddPayment_userName.Text = model.UserName;
            //    txtAddPayment_payout.Text = Convert.ToString((model.MoneyTotal - model.MoneyReceived));
            //    lblAddPayment_payoutMsg.Text = "<span style=\"color:black\">支付成功后，将从您的资金余额中扣除相应款项。</span>";
            //    txtAddPayment_remark.Text = model.OrderNum;

            //    multiViewOrder.SetActiveView(viewAddPayment);
            //}
            //else
            //{
            //    msg = "该订单不存在！";
            //    MsgBox.Alert(msg, UpdatePanel1);

            //    ViewShowOrderItemDataBind(orderId);
            //    multiViewOrder.SetActiveView(viewShowOrderItem);
            //    return;
            //}

        }


        //虚拟货币支付
        protected void btnAddPayment_confirm_Click(object sender, EventArgs e)
        {
            ////判断输入信息有没有出错
            //DateTime dateAndTime = DateTime.Now;
            //Decimal money = 0;
            //bool isErr = false;
            //OrdersManage order = new OrdersManage();
            //OrdersInfo orderModel = new OrdersInfo();

            //TopWinCMS.BLL.MemberManage user = new TopWinCMS.BLL.MemberManage();
            //MemberInfo userModel = new MemberInfo();
            //userModel = user.EditLoad(AdminName);

            //orderModel = order.EditLoad(MyConvert.StrToInt(lblAddPayment_orderId.Text));
            //if (orderModel != null)
            //{
            //    if (orderModel.MoneyReceived >= orderModel.MoneyTotal)
            //    {
            //        ViewShowOrderItemDataBind(orderModel.ID);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //        return;
            //    }
            //}

            //if (!decimal.TryParse(txtAddPayment_payout.Text.Trim(), out money))
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "汇款金额输入错误！";
            //}
            //if (money == 0)
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "汇款金额不能为０！";
            //}
            //if (money > (orderModel.MoneyTotal - orderModel.MoneyReceived))
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "金额不能大于所欠货款！";
            //}

            //if (userModel.Balance < (orderModel.MoneyTotal - orderModel.MoneyReceived))
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "您的资金余额不足！";
            //}

            //if (isErr)
            //{
            //    return;
            //}

            //if (orderModel != null)
            //{
            //    //保存信息到bankrollItem表（扣除）
            //    BankrollListManage bankroolItem = new BankrollListManage();
            //    BankrollListInfo model = new BankrollListInfo();
            //    model.UserName = lblAddPayment_userName.Text;
            //    model.ClientId = orderModel.ClientId;
            //    model.DateAndTime = DateTime.Now.ToString();
            //    model.Money = -money;
            //    model.MoneyType = (int)BLL.Shop.MoneyType.PayByBalance;
            //    model.CurrencyType = (int)BLL.Shop.CurrencyType.RMB;
            //    model.eBankID = 0;
            //    model.Bank = string.Empty;
            //    model.Income_Payout = (int)BLL.Shop.Income_Payout.Payout;
            //    model.ID = orderModel.ID;
            //    model.PaymentId = 0;
            //    model.Remark = "支付订单费用，订单号：" + orderModel.OrderNum + "  " + txtAddPayment_remark.Text;
            //    model.LogTime = DateTime.Now.ToString();
            //    model.Inputer = AdminName;
            //    model.Ip = WebUtils.GetIP();

            //    string msg = string.Empty;
            //    if (bankroolItem.Add(model, out msg) > 0)
            //    {
            //        //更新订单信息

            //        orderModel.MoneyReceived = money + orderModel.MoneyReceived;
            //        if (orderModel.MoneyReceived > 0)
            //        {
            //            orderModel.Status = (int)TopWinCMS.BLL.Shop.Status.ReceivedEarnest;
            //            if (orderModel.MoneyReceived == orderModel.MoneyTotal)
            //                orderModel.Status = (int)TopWinCMS.BLL.Shop.Status.Payoff;
            //        }

            //        order.Update(orderModel);

            //        //更新用户信息
            //        userModel.Balance = userModel.Balance - money;
            //        user.Update(userModel);

            //        //提示成功信息

            //        msg = "操作成功！";
            //        MsgBox.Alert(msg, UpdatePanel1);

            //        ViewShowOrderItemDataBind(orderModel.ID);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //    }
            //    else
            //    {
            //        msg = "系统错误发生错误，请重新汇款！";
            //        MsgBox.Alert(msg, UpdatePanel1);
            //    }
            //}
        }

        #endregion

        #region  ViewInvoice

        //开具发票视图数据绑定
        protected void ViewInvoiceDataBind(int orderId)
        {
            OrdersManage order = new OrdersManage();
            OrdersInfo model = new OrdersInfo();
            model = order.EditLoad(orderId);
            if (model != null)
            {
                lblOrderId15.Text = orderId.ToString();
                lblContacterName15.Text = model.ContacterName;
                lblUserName15.Text = model.UserName;
                lblMoneyTotal15.Text = string.Format("{0:c}", model.MoneyTotal);
                lblMoneyReceived15.Text = string.Format("{0:c}", model.MoneyReceived);
                lblOrderNum15.Text = model.OrderNum;
                lblInvoiceContent15.Text = model.InvoiceContent;
                lblTotalMoneyMsg15.Text = string.Empty;
                lblInvoiceDateMsg15.Text = string.Empty;

                txtTotalMoney15.Text = model.MoneyTotal.ToString();
                txtInvoiceDate15.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtInvoiceContent15.Text = HttpUtility.HtmlDecode(model.InvoiceContent).Replace("<br>", "\r\n");

                multiViewOrder.SetActiveView(viewInvoice);
            }
        }

        //录入发票信息到InvoiceManage表
        protected void btnInvoiceConfirm_Click(object sender, EventArgs e)
        {
            //检查输入

            DateTime invoiceDate;
            decimal totalMoney = 0;

            bool isErr = false;
            if (!decimal.TryParse(txtTotalMoney15.Text.Trim(), out totalMoney))
            {
                isErr = true;
                lblTotalMoneyMsg15.Text = "金额输入错误！";
            }
            if (!DateTime.TryParse(txtInvoiceDate15.Text.Trim(), out invoiceDate))
            {
                isErr = true;
                lblInvoiceDateMsg15.Text = "日期格式不对！";
            }
            if (isErr)
                return;

            //录入发票信息
            InvoiceManage invoiceItem = new InvoiceManage();
            InvoiceInfo model = new InvoiceInfo();
            OrdersManage order = new OrdersManage();
            OrdersInfo orderModel = new OrdersInfo();
            orderModel = order.EditLoad(MyConvert.ToInt32(lblOrderId15.Text));
            if (orderModel != null)
            {

                //model.ClientID = orderModel.ClientId;
                model.UserName = orderModel.UserName;
                model.OrderID = orderModel.ID;
                model.InvoiceType = MyConvert.ToInt32(drpInvoiceType15.SelectedValue);
                model.InvoiceNum = txtInvoiceNum15.Text.Trim();
                model.InvoiceTitle = txtInvoiceTitle15.Text.Trim();
                model.InvoiceContent = HttpUtility.HtmlEncode(txtInvoiceContent15.Text.Trim());
                model.InvoiceDate = invoiceDate;
                model.TotalMoney = totalMoney;
                model.Drawer = txtDrawer15.Text.Trim();
                model.Admin = AdminLoginInfo.UserName;
                model.CheckInDate = DateTime.Now;

                string Err = null;
                invoiceItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err))
                {
                    //更新订单信息

                    orderModel.Invoiced = 1;
                    order.Add(orderModel, null, ref Err);
                    if (string.IsNullOrEmpty(Err) == false)
                    {
                        MsgBox.Alert(Err, UpdatePanel1);
                        return;
                    }

                    //提示成功信息


                    MsgBox.Alert("操作成功！", UpdatePanel1);

                    ViewShowOrderItemDataBind(orderModel.ID);
                    multiViewOrder.SetActiveView(viewShowOrderItem);
                }
                else
                {
                    MsgBox.Alert("系统错误发生错误，请重新开具发票！", UpdatePanel1);
                }

            }
        }
        #endregion


        protected void SwitchOrderListDataBindType()
        {
            //switch (hidOrderListDataBindType.Value.ToLower())
            //{
            //    case "quick":
            //        OrderListQuickSearch();
            //        break;
            //    case "advanced":
            //        OrderListAdancedSearch();
            //        break;
            //    default:
            //        GridOrderListDataBind();
            //        break;
            //}
        }
    }
}
