using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text; 
public partial class WTOSYSadmin_vipmag_orderdo : System.Web.UI.Page
{
    private Label l1 = new Label();
    private Label l2 = new Label();
    private Label l3 = new Label();
    private Label l4 = new Label();
    private Label l5 = new Label();
    private Label l6 = new Label();
    private Label l7 = new Label();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {
            SqlConnection conn = CDatabase.wtoconn();
            conn.Open();
            SqlCommand smd = new SqlCommand("select * from Orderw where id=" + Request.QueryString["id"], conn);
            SqlDataReader sdr = smd.ExecuteReader();
            sdr.Read();
            this.Label4.Text = sdr.GetString(3);
            this.Orderno.Text = sdr.GetString(1);
            this.Ordername.Text = sdr.GetString(3);
            this.OrderEmail.Text = sdr.GetString(4);
            this.OrderTel.Text = sdr.GetString(5);
            this.OrderMobi.Text = sdr.GetString(6);
            string  laear = sdr.GetString(9);
            this.OrderAdd.Text = sdr.GetString(7);
            this.OrderPost.Text = sdr.GetString(8);
            this.OrderPostTime.Text = sdr.GetString(10);
            this.OrderPayM.Text = sdr.GetString(12);
            this.OrderSonF.Text = sdr.GetString(13);
            Label2.Text = sdr["b3"].ToString();
            Label1.Text = Convert.ToString(GetUpperMoney(sdr["OrderTatal"]));
            Label5.Text = sdr["psfy"].ToString();


            if (sdr["xszk"].ToString() != "10")
            {
                this.Label3.Text ="您已经享有"+ sdr["xszk"].ToString()+"折扣";
                this.Label3.Visible = true;
            }
            else
            {
                this.Label3.Visible = false;
            }
            this.OrderAddDate.Text =Convert.ToString(sdr.GetDateTime(15));
            if (sdr.GetString(11) != "")
            {
                this.OrderMone.Text = sdr.GetString(11);
            }
            else
            {
                this.OrderMone.Text = "无";
            }
            this.OrderTatal.Text ="￥ "+Convert.ToString(sdr.GetDecimal(14).ToString("0.00"));
            int orderid = sdr.GetInt32(0);
           
            sdr.Close();
            SqlCommand smdordert = new SqlCommand("select AreaName from ServiceArea where id=" + laear.ToString(), conn);
            this.Orderaear.Text = Convert.ToString(smdordert.ExecuteScalar());

            //----------------------------------------------------------------------------
            # region 表格TB创建[动态]


            l2.Text = "商品名称";
            l3.Text = "优惠价格";
            l4.Text = "商品编号";
            l5.Text = "数量";
            l6.Text = "小计";
            TableRow tr;
            TableCell tc;
            tr = new TableRow();
            tr.HorizontalAlign = HorizontalAlign.Center;
            tc = new TableCell();
            tc.Controls.Add(l2);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l3);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l5);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l6);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l7);
            tr.Controls.Add(tc);
            TB.Controls.Add(tr);
            TB.HorizontalAlign = HorizontalAlign.Center;
            tr.CssClass = "carttr";

            ImageButton img1;

            SqlCommand smd2 = new SqlCommand("select * from OrderInfo  where OrderID=" + orderid, conn);
            SqlDataReader sdr2 = smd2.ExecuteReader();

            Label lbl1 = new Label();
            TableCell tc2;
            TextBox txtBox;
            decimal counttotal = 0;
            while (sdr2.Read())
            {

                tr = new TableRow();
                tr.CssClass = "carttrM";


                l1 = new Label();
                l1.Text = sdr2.GetString(3);
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);

                l1 = new Label();
                l1.Text = "￥：" + Convert.ToString(sdr2.GetDecimal(4));
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);


                //l1 = new Label();
                //l1.Text = sdr2.GetString(6);
                //tc = new TableCell();
                //tc.Controls.Add(l1);
                //tr.Controls.Add(tc);
                //l1 = new Label();

                l1 = new Label();
                l1.Text = Convert.ToString(sdr2.GetInt32(5));
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);


                l1 = new Label();
                l1.Text = "￥：" + Convert.ToString(sdr2.GetInt32(5) * sdr2.GetDecimal(4));
                counttotal += sdr2.GetInt32(5) * sdr2.GetDecimal(4);
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);

                TB.Controls.Add(tr);
            }
            sdr2.Close();
            # endregion
            conn.Close();
        }
    }
        
    public static Decimal GetDecimal(object sNum)
        {
            return GetDecimal(sNum, 0);
        }
        public static Decimal GetDecimal(object sNum, Decimal iDefault)
        {
            return GetDecimal(sNum, 0, -1);
        }
        public static Decimal GetDecimal(object sNum, Decimal iDefault, int iDotNum)
        {
            try
            {
                Decimal d = Convert.ToDecimal(sNum);
                if (iDotNum == -1)
                {
                    string s = d.ToString();

                    for (int i = s.Length - 1; i >= 0; i--)
                    {
                        if (s.IndexOf('.') > -1 && (s[i] == '0' || s[i] == '.'))
                        {
                            s = s.Substring(0, s.Length - 1);
                        }
                        else
                        {
                            break;
                        }
                    }
                    d = Convert.ToDecimal(s);

                    return d;
                }
                else
                {
                    return Math.Round(d, iDotNum);
                }
            }
            catch
            {
                return iDefault;
            }
        }

        public static string GetUpperMoney(object oNum)
        {
            string[] dUnit = new string[] { "角", "分", "厘", "毫", "絲" };
            string[] iUnit = new string[] 
                {
                    "元", 
                    "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", 
                    "拾", "佰", "仟", "萬", "拾", "佰", "仟", "兆", 
                    "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟", "京" 
                };
            string[] sUpperNum = new string[] { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" };

            string sNum = GetDecimal(GetDecimal(oNum, 0, 5)).ToString();

            string iNum = string.Empty, dNum = string.Empty;
            if (sNum.LastIndexOf(".") == -1)
            {
                iNum = UpperMoney(sNum, sUpperNum, iUnit);
            }
            else
            {
                string[] sNums = sNum.ToString().Split(new char[] { '.' });
                iNum = UpperMoney(sNums[0], sUpperNum, iUnit);
                dNum = UpperMoney(sNums[1], sUpperNum, dUnit, true);
            }

            return iNum + dNum;
        }

        private static string UpperMoney(string sNum, string[] sUpperNum, string[] upperUnit)
        {
            return UpperMoney(sNum, sUpperNum, upperUnit, false);
        }

        private static string UpperMoney(string sNum, string[] sUpperNum, string[] upperUnit,bool b)
        {
            char[] cNums = sNum.ToCharArray();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sNum.Length; i++)
            {
                int iNum = int.Parse(cNums[i].ToString());

                string unit = string.Empty;
                if (iNum != 0)
                {
                    if (b)
                        unit = upperUnit[i];
                    else
                        unit = upperUnit[sNum.Length - i - 1];
                }
                else if (AddUnitAtZero(sNum.Length - i))
                {
                    if (!b)
                        unit = upperUnit[sNum.Length - i - 1];
                }

                sb.Append(sUpperNum[iNum] + unit);
            }

            return ReplaceInvalid(sb.ToString());
        }

        private static bool AddUnitAtZero(int index)
        {
            for (int i = 0; i < 8; i++)
            {
                if (index == 4 * i + 1) return true;
            }
            return false;
        }

        private static string ReplaceInvalid(string sNum)
        {
            while (sNum.LastIndexOf("零零") != -1 || sNum.LastIndexOf("零元") != -1|| sNum.LastIndexOf("零萬") != -1|| sNum.LastIndexOf("零億") != -1 
                || sNum.LastIndexOf("零兆") != -1|| sNum.LastIndexOf("億萬") != -1 || sNum.LastIndexOf("兆萬億") != -1 ||sNum.LastIndexOf("京億兆")!=-1)
            {
                sNum = sNum.Replace("零零", "零").Replace("零元", "元").Replace("零萬", "萬").Replace("零億", "億");
                sNum = sNum.Replace("零兆", "兆").Replace("億萬", "億").Replace("兆萬億", "兆").Replace("京億兆", "京");
            }
            return sNum + "整" == "元整" ? "零元整" : sNum + "整";
        }
}
