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;

public partial class personal_registers : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;

            SqlConnection conn = new SqlConnection(sqlconn);

            try
            {
                conn.Open();

                string strSQL = "select departmentname from tabp_Department";
                SqlCommand com = new SqlCommand(strSQL, conn);
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    this.DropDownList1.Items.Add(dr["departmentname"].ToString());
                }
            }
            catch (Exception ex)
            {
                Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");
            }
            finally
            {
                conn.Close();
            }
        }
        string sqlconn1 = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;




    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextNumber.Text = "";
        TextLoginName.Text = "";
        TextPwd.Text = "";
        TextPwd1.Text = "";
        RadioButton1.Checked = true;
        TextBox20.Text = "";
        TextPersonnelName.Text = "";
        TextIdCardNum.Text = "";
        TextCallPhone.Text = "";
        TextPhone.Text = "";
        TextFamilyAddress.Text = "";
        TextBox19.Text = "";
        TextEmail.Text = "";
        TextBox13.Text = "";
        TextBox14.Text = "";
        TextBox15.Text = "";
        TextUrgetRelationName.Text = "";
        TextUrgetRelationRelation.Text = "";
        TextUrgetRelationPhone.Text = "";



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextLoginName.Text.Length == 0)
        {
            Response.Write("<script>alert('用户名不能为空！')</script>");
            return;

        }
        string sqlconn3 = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        SqlConnection conn3 = new SqlConnection(sqlconn3);
        conn3.Open();

        string strzw1 = TextLoginName.Text.ToString();
        string strSQL1 = "select count(*) from tabP_Personnel where LoginName like '%" + strzw1 + "%'";
        SqlCommand com3 = new SqlCommand(strSQL1, conn3);

        int intWorkId1 = Convert.ToInt32(com3.ExecuteScalar());

        if (intWorkId1 > 0)
        {
            Response.Write("<script>alert('用户名重复！')</script>");
            conn3.Close();
        }

        else
        {
            if (TextBox20.Text.ToString() == "" || TextLoginName.Text.ToString() == "")
            {

                Response.Write("<script>alert('资料输入不完整!')</script>");
                return;
            }
            else
            {
                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());
                conn.Close();
                DateTime dateBirthday;
                int intSex;
                string strNumber, strPersonnelName, strLoginName, strPwd, strIdCardNum, strCallPhone, strPhone;
                string strFamilyAddress, strContactAddress, strWorkEmail, strWorkQQ;
                string strPersonalEmail, strPersonalQQ, strUrgetRelationName, strUrgetRelationRelation, strUrgetRelationPhone;
                int intappprovestate;


                strNumber = TextNumber.Text.ToString();
                strPersonnelName = TextPersonnelName.Text.ToString();
                strLoginName = TextLoginName.Text.ToString();
                strPwd = TextPwd.Text.ToString();



                if (shenpi.Checked == true)
                {
                    intappprovestate = 0;
                }
                else
                {
                    intappprovestate = 1;
                }

                if (RadioButton1.Checked == true)
                {
                    intSex = 0;
                }
                else
                {
                    intSex = 1;
                }
                dateBirthday = Convert.ToDateTime(TextBox20.Text);
                strIdCardNum = TextIdCardNum.Text.ToString();
                strCallPhone = TextCallPhone.Text.ToString();
                strPhone = TextPhone.Text.ToString();
                strFamilyAddress = TextFamilyAddress.Text.ToString();
                strContactAddress = TextBox19.Text.ToString();
                strWorkEmail = TextEmail.Text.ToString();
                strWorkQQ = TextBox13.Text.ToString();
                strPersonalEmail = TextBox14.Text.ToString();
                strPersonalQQ = TextBox15.Text.ToString();
                strUrgetRelationName = TextUrgetRelationName.Text.ToString();
                strUrgetRelationRelation = TextUrgetRelationRelation.Text.ToString();
                strUrgetRelationPhone = TextUrgetRelationPhone.Text.ToString();


                conn.Open();
                SqlCommand com2 = new SqlCommand("INSERT INTO tabP_Personnel(WorkId,Number,PersonnelName,LoginName,Pwd,Sex,IdCardNum,Birthday,CallPhone,Phone,FamilyAddress,ContactAddress,WorkEmail,WorkQQ,PersonalEmail,PersonalQQ,UrgetRelationName,UrgetRelationRelation,UrgetRelationPhone,ApproveState) values ('" + intWorkId + "','" + strNumber + "','" + strPersonnelName + "','" + strLoginName + "','" + strPwd + "','" + intSex + "','" + strIdCardNum + "','" + dateBirthday + "','" + strCallPhone + "','" + strPhone + "','" + strFamilyAddress + "','" + strContactAddress + "','" + strWorkEmail + "','" + strWorkQQ + "','" + strPersonalEmail + "','" + strPersonalQQ + "','" + strUrgetRelationName + "','" + strUrgetRelationRelation + "','" + strUrgetRelationPhone + "','" + intappprovestate + "')", conn);
                int i = com2.ExecuteNonQuery();

                Response.Write("<script>alert('注册成功');window.window.location.href='registers.aspx';</script>");
                conn.Close();

            }
        }
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        if (TextLoginName.Text.Length == 0)
        {
            Response.Write("<script>alert('用户名不能为空！')</script>");
            return;

        }
        string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        SqlConnection conn = new SqlConnection(sqlconn);
        conn.Open();

        string strzw = TextLoginName.Text.ToString();
        string strSQL = "select count(*) from tabP_Personnel where LoginName like '%" + strzw + "%'";
        SqlCommand com = new SqlCommand(strSQL, conn);

        int intWorkId = Convert.ToInt32(com.ExecuteScalar());

        if (intWorkId > 0)
        {
            Response.Write("<script>alert('用户名重复！')</script>");
        }
        else
            Response.Write("<script>alert('该用户名可用')</script>");

    }


    protected void DropDownList1_SelectedIndexChanged1(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();
        }


    }
}
