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 webXabara_adminUser_userEdit : System.Web.UI.Page
{
    sqlServer sql = new sqlServer();
    javasrcipt js = new javasrcipt();
    classify c = new classify();
    admin a = new admin();

    protected void Page_Load(object sender, EventArgs e)
    {
        a.check(string.Empty);

        if (!IsPostBack)
        {
            c.bindingDropDownList("all", false, 0, "city", cityID);
            c.bindingDropDownList("all", false, 0, "group", groupID);
            c.bindingDropDownList("all", false, 0, "class", classID);

            string uidTemp = Request.QueryString["uid"];
            if (!string.IsNullOrEmpty(uidTemp as string))
            {
                if (a.classID > 0 && uidTemp != a.uid)
                {
                    Response.Redirect("userEdit.aspx", true);
                }
            }
            else
            {
                uidTemp = a.uid;
            }
            Uid.Text = uidTemp;

            string strSql = "select * from AdminUser where userID='" + uidTemp + "'";
            DataTable dt = sql.getDataTable(strSql);

            if (dt.Rows.Count > 0)
            {
                adminName.Text = dt.Rows[0]["UserName"].ToString().Trim();
                oldPWHidden.Value = dt.Rows[0]["UserPW"].ToString().Trim();
                adminMail.Text = dt.Rows[0]["userEmail"].ToString().Trim();
                adminTel.Text = dt.Rows[0]["userTel"].ToString().Trim();
                adminMobile.Text = dt.Rows[0]["userMobile"].ToString().Trim();
                adminQQ.Text = dt.Rows[0]["userQQ"].ToString().Trim();
                this.postIF.Items.FindByValue(dt.Rows[0]["postIF"].ToString()).Selected = true;
                long ifLogin = xabara.isNum(dt.Rows[0]["loginFlag"].ToString());
                if (ifLogin < 1)
                {
                    this.ifLogin.Items.FindByValue("0").Selected = true;
                }
                else
                {
                    this.ifLogin.Items.FindByValue("1").Selected = true;
                }

                if (xabara.isNum(dt.Rows[0]["cityID"].ToString().Trim()) > 0 && xabara.isNum(dt.Rows[0]["classID"].ToString().Trim()) > 0 && xabara.isNum(dt.Rows[0]["groupID"].ToString().Trim()) > 0)
                {
                    try
                    {
                        this.cityID.Items.FindByValue(dt.Rows[0]["cityID"].ToString().Trim()).Selected = true;
                        this.classID.Items.FindByValue(dt.Rows[0]["classID"].ToString().Trim()).Selected = true;
                        this.groupID.Items.FindByValue(dt.Rows[0]["groupID"].ToString().Trim()).Selected = true;
                    }
                    catch { }

                    if (a.groupID > 0)
                    {
                        this.cityID.Enabled = false;
                        this.classID.Enabled = false;
                        this.groupID.Enabled = false;
                        this.postIF.Enabled = false;
                        this.ifLogin.Enabled = false;
                    }
                }
                else
                {
                    this.cityID.Items.Clear();
                    this.cityID.Items.Add(new ListItem("超级管理员", "0"));
                    this.classID.Items.Clear();
                    this.classID.Items.Add(new ListItem("超级管理员", "0"));
                    this.groupID.Items.Clear();
                    this.groupID.Items.Add(new ListItem("超级管理员", "0"));
                }
            }
            else
            {
                js.div(false, "", "text", "您的操作有误！", 300, 0, "userList.aspx?page=" + Request.QueryString["page"], "goto");
            }
            dt.Dispose();

            regExpression reg = new regExpression();
            adminNameRegularExpressionValidator.ValidationExpression = reg.chinese;
            adminNameRegularExpressionValidator.ErrorMessage = reg.chineseError;
            adminPWRegularExpressionValidator.ValidationExpression = reg.passWord(6, 20);
            adminPWRegularExpressionValidator.ErrorMessage = reg.passWordError(6, 20);
            mailRegularExpressionValidator.ValidationExpression = reg.email;
            mailRegularExpressionValidator.ErrorMessage = reg.emailError;
            telRegularExpressionValidator.ValidationExpression = reg.phone;
            telRegularExpressionValidator.ErrorMessage = reg.phoneError;
            mobileRegularExpressionValidator.ValidationExpression = reg.mobile;
            mobileRegularExpressionValidator.ErrorMessage = reg.mobileError;
            qqRegularExpressionValidator.ValidationExpression = reg.qq;
            qqRegularExpressionValidator.ErrorMessage = reg.qqError;
        }
    }

    protected void editAdmin_Click(object sender, EventArgs e)
    {
        string dimStr = "@UserName,@UserPW,@userEmail,@userTel,@userMobile,@userQQ,@classID,@postIF,@cityID,@groupID";

        string[] strValue = new string[12];
        strValue[0] = adminName.Text.Trim();
        string pw = adminPW.Text;
        if (!string.IsNullOrEmpty(pw as string))
        {
            pw = xabara.md5(pw);
        }
        else
        {
            pw = oldPWHidden.Value;
        }
        strValue[1] = pw;
        strValue[2] = adminMail.Text.Trim();
        strValue[3] = adminTel.Text.Trim();
        strValue[4] = adminMobile.Text.Trim();
        strValue[5] = adminQQ.Text.Trim();
        strValue[6] = classID.Text;
        strValue[7] = postIF.Text;
        strValue[8] = cityID.Text;
        strValue[9] = groupID.Text;

        if (sql.update("AdminUser", dimStr, strValue, " userID='" + Uid.Text + "'").Equals("更新成功"))
        {
            sql.log(a.uid, "admin", "修改资料");

            js.div(false, "", "text", "更新资料成功！", 300, 0, "userList.aspx?page=" + xabara.isNum(Request.QueryString["page"]).ToString(), "goto");
        }
        else
        {
            js.div(false, "", "text", "更新资料失败！", 300, 0, "", "");
        }
    }
}
