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

public partial class member_user_edit : System.Web.UI.Page
{
    user u = new user();
    sqlServer sql = new sqlServer();
    javasrcipt js = new javasrcipt();

    public string idTemp = HttpContext.Current.Request["id"];
    public string webTitle = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        u.check("", url.before);
        webTitle = xml.read("xabara.config", "webName", 180);
        Page.Title = "个人资料　" + webTitle + xml.read("xabara.config", "titleMeta", 180);

        if (!IsPostBack)
        {
            regExpression reg = new regExpression();
            textNameRegularExpressionValidator.ValidationExpression = reg.chinese;
            textNameRegularExpressionValidator.ErrorMessage = reg.chineseError;
            zipRegularExpressionValidator.ValidationExpression = reg.zip;
            zipRegularExpressionValidator.ErrorMessage = reg.zipError;
            telRegularExpressionValidator.ValidationExpression = reg.phone;
            telRegularExpressionValidator.ErrorMessage = reg.phoneError;
            faxRegularExpressionValidator.ValidationExpression = reg.phone;
            faxRegularExpressionValidator.ErrorMessage = reg.phoneError;
            mobileRegularExpressionValidator.ValidationExpression = reg.mobile;
            mobileRegularExpressionValidator.ErrorMessage = reg.mobileError;
            qqRegularExpressionValidator.ValidationExpression = reg.qq;
            qqRegularExpressionValidator.ErrorMessage = reg.qqError;
            dateRegExpree.ValidationExpression = reg.ymd;
            dateRegExpree.ErrorMessage = reg.ymdError;

            DataTable dt;
            dt = sql.getDataTable("select * from XabaraUser where userCardID='" + u.userCardID + "'");
            if (dt.Rows.Count > 0)
            {
                txtName.Text = dt.Rows[0]["userName"].ToString().Trim();
                if (!string.IsNullOrEmpty(dt.Rows[0]["userSex"].ToString().Trim() as string))
                {
                    this.sexList.Items.FindByText(dt.Rows[0]["userSex"].ToString().Trim()).Selected = true;
                }
                if (!string.IsNullOrEmpty(dt.Rows[0]["userBirthday"].ToString().Trim() as string))
                {
                    uBirthday.Text = Convert.ToDateTime(dt.Rows[0]["userBirthday"].ToString().Trim()).ToString("yyyy-MM-dd");
                }
                uAdd.Text = dt.Rows[0]["userAdd"].ToString().Trim();
                zip.Text = dt.Rows[0]["userZip"].ToString().Trim();
                tel.Text = dt.Rows[0]["userTel"].ToString().Trim();
                mobile.Text = dt.Rows[0]["userMobile"].ToString().Trim();
                fax.Text = dt.Rows[0]["userFax"].ToString().Trim();
                qq.Text = dt.Rows[0]["userQQ"].ToString().Trim();
                wangwang.Text = dt.Rows[0]["userWW"].ToString().Trim();

                int uLevel = Convert.ToInt32(dt.Rows[0]["userLevel"].ToString().Trim());
                if (uLevel > 0)
                {
                    txtName.ReadOnly = true;
                }

                long pidTemp = xabara.isNum(dt.Rows[0]["userProvince"].ToString().Trim());   //省份
                long cityIDTemp = xabara.isNum(dt.Rows[0]["userCity"].ToString().Trim());   //市级
                long aIDTemp = xabara.isNum(dt.Rows[0]["userArea"].ToString().Trim());   //县区级

                if (pidTemp < 1)  //省市县cookies检查
                {
                    if (Request.Cookies["xabaraWhere"] != null)
                    {
                        pidTemp = xabara.isNum(Request.Cookies["xabaraWhere"]["p"].ToString().Trim());
                        cityIDTemp = xabara.isNum(Request.Cookies["xabaraWhere"]["c"].ToString().Trim());
                        aIDTemp = xabara.isNum(Request.Cookies["xabaraWhere"]["a"].ToString().Trim());
                    }
                }

                dt = sql.getDataTable("select * from selectProvince order by id");
                this.ProvinceList.Items.Clear();
                this.ProvinceList.Items.Add(new ListItem("== 省份 ==", ""));
                for (int p = 0; p < dt.Rows.Count; p++)
                {
                    this.ProvinceList.Items.Add(new ListItem(dt.Rows[p]["provinceTxt"].ToString().Trim(), dt.Rows[p]["provinceID"].ToString().Trim()));
                }
                this.ProvinceList.DataBind();
                if (pidTemp > 0)
                {
                    this.ProvinceList.Items.FindByValue(pidTemp.ToString()).Selected = true;
                }
                dt.Dispose();

                DataTable dtCity = sql.getDataTable("select * from selectCity where cFather=" + pidTemp.ToString() + " order by id");
                //this.CityList.Items.Clear();
                if (dtCity.Rows.Count > 0)
                {
                    for (int p = 0; p < dtCity.Rows.Count; p++)
                    {
                        this.CityList.Items.Add(new ListItem(dtCity.Rows[p]["cityTxt"].ToString().Trim(), dtCity.Rows[p]["cityID"].ToString().Trim()));
                    }
                }
                this.CityList.DataBind();
                dtCity.Dispose();
                if (cityIDTemp > 0)
                {
                    this.CityList.Items.FindByValue(cityIDTemp.ToString()).Selected = true;
                }

                DataTable dtArea = sql.getDataTable("select * from selectArea where aFather='" + cityIDTemp.ToString() + "' order by id");
                //this.areaList.Items.Clear();
                if (dtArea.Rows.Count > 0)
                {
                    for (int p = 0; p < dtArea.Rows.Count; p++)
                    {
                        this.areaList.Items.Add(new ListItem(dtArea.Rows[p]["areaTxt"].ToString().Trim(), dtArea.Rows[p]["areaID"].ToString().Trim()));
                    }
                }
                this.areaList.DataBind();
                if (aIDTemp > 0)
                {
                    this.areaList.Items.FindByValue(aIDTemp.ToString()).Selected = true;
                }
                dtArea.Dispose();
            }

            dt.Dispose();
        }
    }

    protected void postButton_Click(object sender, ImageClickEventArgs e)
    {
        string strDim = "@userName,@userSex,@userBirthday,@userProvince,@userCity,@userArea,@userAdd,@userZip,@userTel,@userMobile,@userFax,@userQQ,@userWW";
        string[] strValue = new string[13];
        strValue[0] = txtName.Text;
        strValue[1] = sexList.Text;
        strValue[2] = uBirthday.Text;
        strValue[3] = ProvinceList.Text;
        strValue[4] = CityList.Text;
        strValue[5] = areaList.Text;
        strValue[6] = uAdd.Text;
        strValue[7] = zip.Text;
        strValue[8] = tel.Text;
        strValue[9] = mobile.Text;
        strValue[10] = fax.Text;
        strValue[11] = qq.Text;
        strValue[12] = wangwang.Text;

        string str = sql.update("xabaraUser", strDim, strValue, "userCardID='" + u.userCardID + "'");
        sql.log(u.userCardID, "user", "更新资料");

        js.div(false, "", "text", str + "！", 400, 0, "edit.aspx", "top");
    }

    protected void ProvinceList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.CityList.Items.Clear();
        this.CityList.Items.Add(new ListItem("== 地级市 ==", ""));
        if (!string.IsNullOrEmpty(ProvinceList.Text as string))
        {
            DataTable dt;
            dt = sql.getDataTable("select * from selectCity where cFather=" + ProvinceList.Text.Trim() + " order by id");
            for (int p = 0; p < dt.Rows.Count; p++)
            {
                this.CityList.Items.Add(new ListItem(dt.Rows[p]["cityTxt"].ToString().Trim(), dt.Rows[p]["cityID"].ToString().Trim()));
            }
            this.CityList.DataBind();
            dt.Dispose();
        }
        this.areaList.Items.Clear();
        this.areaList.Items.Add(new ListItem("== 县级市 ==", ""));
    }

    protected void cityList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.areaList.Items.Clear();
        if (!string.IsNullOrEmpty(CityList.Text as string))
        {
            DataTable dt;
            dt = sql.getDataTable("select * from selectArea where aFather=" + CityList.Text.Trim() + " order by id");
            if (dt.Rows.Count > 0)
            {
                for (int p = 0; p < dt.Rows.Count; p++)
                {
                    this.areaList.Items.Add(new ListItem(dt.Rows[p]["areaTxt"].ToString().Trim(), dt.Rows[p]["areaID"].ToString().Trim()));
                }
            }
            else
            {
                this.areaList.Items.Add(new ListItem("== 暂无 ==", ""));
            }
            this.areaList.DataBind();
            dt.Dispose();
        }
    }
}