using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Mall.Order
{
    public partial class Order_Login : System.Web.UI.Page
    {
        public string r_code;
        protected void Page_Load(object sender, EventArgs e)
        {
            string cartn = Request.Cookies["cart"] == null ? string.Empty : Request.Cookies["cart"].Value;
            cartn = Server.UrlDecode(cartn);
            if (string.IsNullOrEmpty(cartn))
            {
                Response.Redirect("/");
               // return RedirectToAction("index", "index");
            }
            string username = string.Empty;
            string userpwd = string.Empty;
            string login_type = string.Empty;
            login_type = Request["login_type"] == null ? string.Empty : Request["login_type"].ToString();
            MODEL.MMember model_member = new MODEL.MMember();
            //判断是否为登陆

            if (!string.IsNullOrEmpty(login_type))
            {



                if (Request["email"] != null)
                {
                    model_member.UserName = Request["email"] == null ? "" : Request["email"].ToString();
                    model_member.UserPwd = Request["pwd"] == null ? "" : Request["pwd"].ToString();
                    //BMember bll_member = new BMember();
                    BLL.Models.MemberModels models_member = new BLL.Models.MemberModels();
                    if (models_member.CheckUser(model_member))
                    {//bll_member.CheckUser(model_member.Email,model_member.UserPwd,"")){

                        Response.Cookies["MemberName"].Value = model_member.UserName;
                        Session["MemberName"] = model_member.UserName;

                       // return RedirectToAction("order_address", "order");

                        Response.Redirect("/order/order_address.aspx");
                    }
                    else
                    {

                        Response.Redirect("/member/member_login.aspx");
                      // TempData["show_message"] = "用户名或密码错误，系统5秒钟后会自动返回登陆页面。";
                       // TempData["msg_script"] = " location.href='/Member/Member_Login'; ";
                       // TempData["msg_back"] = "location.href='/Member/Member_Login'; ";

                      //  return RedirectToAction("ShowMessage", "Index");
                    }

                }
                else
                {

                   // return View();

                }
            }
            Random rad = new Random();
            r_code = rad.Next(1000, 9999).ToString();
            //TempData["order_Login_r_code"] = r_code;
            // MODEL.MMember model_member = new MODEL.MMember();
            //  BLL.BMember bll_member = new BLL.BMember();
            MODEL.MOrder model_order = new MODEL.MOrder();
            if (Request["ctl00_MainPlace_txtCustomerName"] != null)
            {

                username = DateTime.Now.ToString("yyyyMMddhhmmss");
                Random rnd = new Random();
                username = username + rnd.Next(100000).ToString();
                userpwd = "123456";
                model_member.UserName = username;
                model_member.UserPwd = userpwd;
                BLL.Models.MemberModels model_members = new BLL.Models.MemberModels();
                Response.Cookies["MemberName"].Value = model_member.UserName;
                Session["MemberName"] = model_member.UserName;
                model_members.CreateMemberTemp(model_member);
                BLL.Models.MemberModels models_member = new BLL.Models.MemberModels();

                model_order.Userid = Convert.ToInt32(models_member.GetUserIdByName(model_member.UserName));
                model_order.OrderNum = model_member.UserName + rad.Next(100000, 999999).ToString();
                model_order.RecMobile = Request["ctl00_MainPlace_txtCellphone1"];
                model_order.RecZip = Request["ctl00_MainPlace_txtPostCode1"];
                model_order.RecAddress = Request["ctl00_MainPlace_cmbProvince"] + Request["ctl00_MainPlace_cmbCity"] + Request["ctl00_MainPlace_txtAddress"];
                model_order.RecName = Request["ctl00_MainPlace_txtCustomerName"];
                Session["model_order"] = model_order;
                //TempData["model_order"] = model_order;
                MODEL.MMember model_menber = new MODEL.MMember();
                model_menber.UserName = username;
                model_menber.Mobile = model_order.RecMobile;
                model_menber.ZipCode = model_order.RecZip;
                model_menber.Address = model_order.RecAddress;
                model_member.Name = model_order.RecName;
                models_member.UpdateSimgUser(model_menber);
                //return RedirectToAction("order_ship", "order");
                Response.Redirect("/order/order_ship.aspx");



            }

        }
    }
}