using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class personal_Modifyuser : PageBase
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            DataTable tb = new DataTable();
            string strnumber;
            strnumber = Request.QueryString["name"];



            string indexTopMainStr = "SELECT  [departmentname],[PersonnelName], [LoginName], [Pwd],  [WorkName], [Sex], [IdCardNum],[Birthday],[CallPhone],[Phone],[FamilyAddress],[ContactAddress],[WorkEmail],[WorkQQ],[PersonalEmail],[PersonalQQ],[UrgetRelationName],[UrgetRelationRelation],[UrgetRelationPhone],[workname],[pid] FROM [v_tabp_work] where pid like '%" + strnumber + "%'";
            conn.Open();
            SqlDataAdapter rst = new SqlDataAdapter(indexTopMainStr, conn);
            conn.Close();
            rst.Fill(tb);

            string strdepartmentname = tb.Rows[0][0].ToString();
            string strworkname = tb.Rows[0][19].ToString();
            string strPersonnelName = tb.Rows[0][1].ToString();
            string strPwd = tb.Rows[0][3].ToString();
            string nsex = tb.Rows[0][5].ToString();
            string strLoginName = tb.Rows[0][2].ToString();
            string strIdCardNum = tb.Rows[0][6].ToString();
            string strBirthday = tb.Rows[0][7].ToString();
            string strCallPhone = tb.Rows[0][8].ToString();
            string strPhone = tb.Rows[0][9].ToString();
            string strFamilyAddress = tb.Rows[0][10].ToString();
            string strContactAddress = tb.Rows[0][11].ToString();
            string strWorkEmail = tb.Rows[0][12].ToString();
            string strWorkQQ = tb.Rows[0][13].ToString();
            string strPersonalEmail = tb.Rows[0][14].ToString();
            string strPersonalQQ = tb.Rows[0][15].ToString();
            string strUrgetRelationName = tb.Rows[0][16].ToString();
            string strUrgetRelationRelation = tb.Rows[0][17].ToString();
            string strUrgetRelationPhone = tb.Rows[0][18].ToString();
            string strid = tb.Rows[0][20].ToString();

            Lblid.Text = strid;
            DropDownList1.Items.Add(strdepartmentname);
            DropDownList2.Items.Add(strworkname);

            TextUrgetRelationPhone.Text = strUrgetRelationPhone;
            TextUrgetRelationRelation.Text = strUrgetRelationRelation;
            TextUrgetRelationName.Text = strUrgetRelationName;
            TextBox15.Text = strPersonalQQ;
            TextBox14.Text = strPersonalEmail;
            TextBox13.Text = strWorkQQ;
            TextEmail.Text = strWorkEmail;
            TextBox19.Text = strContactAddress;
            TextFamilyAddress.Text = strFamilyAddress;
            TextPhone.Text = strPhone;
            TextNumber.Text = strnumber;
            TextPersonnelName.Text = strPersonnelName;
            TextLoginName.Text = strLoginName;

            TextIdCardNum.Text = strIdCardNum;
            TextBox20.Text = strBirthday;
            TextCallPhone.Text = strCallPhone;
            TextPwd.Text = strPwd;
            Labpwd.Text = strPwd;



            if (nsex == "0")
            {
                RadioButton1.Checked = true;
            }
            else
            {
                RadioButton2.Checked = true;
            }

            string strSQL = "select departmentname from tabp_Department";
            conn.Open();
            SqlCommand com = new SqlCommand(strSQL, conn);
            SqlDataReader dr = com.ExecuteReader();

            while (dr.Read())
            {
                this.DropDownList1.Items.Add(dr["departmentname"].ToString());
            }

            conn.Close();
            woek();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strPwd;
        string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        SqlConnection conn = new SqlConnection(sqlconn);
        conn.Open();
        string strzw = DropDownList2.Text.ToString();
        string strSQL = "select workid from tabP_Work where workname like '%" + strzw + "%'";
        SqlCommand com = new SqlCommand(strSQL, conn);
        int intWorkId = Convert.ToInt32(com.ExecuteScalar());
        int intSex;
        DateTime dateBirthday;
        conn.Close();

        if (RadioButton1.Checked == true)
        {
            intSex = 0;
        }
        else
        {
            intSex = 1;
        }

        string strnumber = TextNumber.Text;
        string LoginName = TextLoginName.Text;

        string strPersonnelName = TextPersonnelName.Text;
        string strIdCardNum = TextIdCardNum.Text;
        string strCallPhone = TextCallPhone.Text;
        string strPhone = TextPhone.Text;
        string strFamilyAddress = TextFamilyAddress.Text;
        string strContactAddress = TextBox19.Text;
        string strWorkEmail = TextEmail.Text;
        string strWorkQQ = TextBox13.Text;
        string strPersonalEmail = TextBox14.Text;
        string strPersonalQQ = TextBox15.Text;
        string strUrgetRelationName = TextUrgetRelationName.Text;
        string strUrgetRelationRelation = TextUrgetRelationRelation.Text;
        string strUrgetRelationPhone = TextUrgetRelationPhone.Text;
        string strid = Lblid.Text;

        dateBirthday = Convert.ToDateTime(TextBox20.Text);
        conn.Open();
        SqlCommand cmd = new SqlCommand("updateuser", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        if (CheckBox1.Checked == true)
        {
            strPwd = TextPwd.Text;

        }
        else
        {
            strPwd = Labpwd.Text;
        }
        cmd.Parameters.Add("@Pid", SqlDbType.Int, 4).Value = Convert.ToInt32(strid);
        cmd.Parameters.Add("@Number", SqlDbType.NVarChar, 50).Value = strnumber;
        cmd.Parameters.Add("@WorkId", SqlDbType.NVarChar, 50).Value = intWorkId;
        cmd.Parameters.Add("@PersonnelName", SqlDbType.NVarChar, 50).Value = strPersonnelName;
        cmd.Parameters.Add("@LoginName", SqlDbType.NVarChar, 500).Value = LoginName;
        cmd.Parameters.Add("@Pwd", SqlDbType.NVarChar, 50).Value = strPwd;
        cmd.Parameters.Add("@Sex", SqlDbType.Int, 4).Value = intSex;
        cmd.Parameters.Add("@IdCardNum", SqlDbType.NVarChar, 80).Value = strIdCardNum;

        cmd.Parameters.Add("@Birthday", SqlDbType.DateTime).Value = dateBirthday;
        cmd.Parameters.Add("@CallPhone", SqlDbType.NVarChar, 50).Value = strCallPhone;
        cmd.Parameters.Add("@Phone", SqlDbType.NVarChar, 50).Value = strPhone;
        cmd.Parameters.Add("@FamilyAddress", SqlDbType.NVarChar, 50).Value = strFamilyAddress;
        cmd.Parameters.Add("@ContactAddress", SqlDbType.NVarChar, 50).Value = strContactAddress;
        cmd.Parameters.Add("@WorkEmail", SqlDbType.NVarChar, 50).Value = strWorkEmail;
        cmd.Parameters.Add("@WorkQQ", SqlDbType.NVarChar, 50).Value = strWorkQQ;
        cmd.Parameters.Add("@PersonalEmail", SqlDbType.NVarChar, 50).Value = strPersonalEmail;
        cmd.Parameters.Add("@PersonalQQ", SqlDbType.NVarChar, 50).Value = strPersonalQQ;
        cmd.Parameters.Add("@UrgetRelationName", SqlDbType.NVarChar, 50).Value = strUrgetRelationName;
        cmd.Parameters.Add("@UrgetRelationRelation", SqlDbType.NVarChar, 50).Value = strUrgetRelationRelation;
        cmd.Parameters.Add("@UrgetRelationPhone", SqlDbType.NVarChar, 50).Value = strUrgetRelationPhone;

        cmd.ExecuteNonQuery();

        conn.Close();

        Response.Write("<script>alert('修改成功！')</script>");

        Response.Write("<script>window.location='Staffmanagemen.aspx'</script>");


    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.location='Staffmanagemen.aspx'</script>");
    }
    public void woek()
    {
        string strSQL = "select workname from tabp_Work";
        conn.Open();
        SqlCommand com1 = new SqlCommand(strSQL, conn);
        SqlDataReader dr2 = com1.ExecuteReader();

        while (dr2.Read())
        {
            this.DropDownList2.Items.Add(dr2["workname"].ToString());
        }

        conn.Close();

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        string strdepartment = DropDownList1.Text.ToString();
        string strdatertnaid = "select departmentid from tabP_Department where departmentname like '%" + strdepartment + "%'";


        SqlConnection conn = new SqlConnection(sqlconn);
        conn.Open();

        SqlCommand sqlcmd = new SqlCommand(strdatertnaid, conn);


        int ndepartmentid = Convert.ToInt32(sqlcmd.ExecuteScalar());

        conn.Close();


        try
        {
            conn.Open();

            string strSQL = "select workname from tabp_Work where departmentid like '%" + ndepartmentid + "%'";
            SqlCommand com = new SqlCommand(strSQL, conn);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                this.DropDownList2.Items.Add(dr["workname"].ToString());
            }
        }
        catch (Exception ex)
        {
            Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");
        }
        finally
        {
            conn.Close();
        }

    }
}