using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.Web.home.space
{
    public partial class edit_edu : JuSNS.UI.Page.UserPage
    {
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        public static string GetTiShiDivStr = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["f"] != string.Empty && Request.QueryString["f"] != null)
            {
                GetTiShiDivStr = "<div class=\"tishicss\">您是第一次登陆本系统，请您完善基本资料。以方便未来的朋友认识您！特别是您的头像！</div>";
            }
            else
            {
                GetTiShiDivStr = string.Empty;
            }

            for (int i = 2008; i >= 1950; i--)
            {
                string str = i.ToString();
                selYear_0.Items.Add(new ListItem(str, str));
                selYear_1.Items.Add(new ListItem(str, str));
                selYear_2.Items.Add(new ListItem(str, str));
                selYear_3.Items.Add(new ListItem(str, str));
                selYear_4.Items.Add(new ListItem(str, str));
                selYear_5.Items.Add(new ListItem(str, str));
                selYear_6.Items.Add(new ListItem(str, str));
                selYear_7.Items.Add(new ListItem(str, str));
            }

            LoadEducationInfo();
            try
            {
                string type = Request.Form["Type"];

                if (type == "Education")
                {
                    LoadEducationInfo();
                }
                else if (type == "Shool")
                {
                    string ShoolId = Request.Form["ShoolID"];
                    getUserInfo(int.Parse(ShoolId));
                }
                else if (type == "Edit")
                {
                    UpdateAddEducation();
                }
            }
            catch { }
        }

        #region 初始化教育信息
        public void LoadEducationInfo()
        {
            DataTable dt = sp.getUserInfo(this.UserID, 10);
            if (dt != null && dt.Rows.Count > 0)
            {
                this.txtCount.Value = dt.Rows.Count.ToString();
                if (int.Parse(this.txtCount.Value) > 8) { this.txtCount.Value = "8"; }
                #region
                switch (this.txtCount.Value)
                {
                    case "1":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        break;
                    case "2":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        break;
                    case "3":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        getEducation(dt, selAcademy_2, SelType_2, selYear_2, txtSchoolName_2, txtID_2, txtShoolID_2, 2);
                        break;
                    case "4":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        getEducation(dt, selAcademy_2, SelType_2, selYear_2, txtSchoolName_2, txtID_2, txtShoolID_2, 2);
                        getEducation(dt, selAcademy_3, SelType_3, selYear_3, txtSchoolName_3, txtID_3, txtShoolID_3, 3);
                        break;
                    case "5":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        getEducation(dt, selAcademy_2, SelType_2, selYear_2, txtSchoolName_2, txtID_2, txtShoolID_2, 2);
                        getEducation(dt, selAcademy_3, SelType_3, selYear_3, txtSchoolName_3, txtID_3, txtShoolID_3, 3);
                        getEducation(dt, selAcademy_4, SelType_4, selYear_4, txtSchoolName_4, txtID_4, txtShoolID_4, 4);
                        break;
                    case "6":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        getEducation(dt, selAcademy_2, SelType_2, selYear_2, txtSchoolName_2, txtID_2, txtShoolID_2, 2);
                        getEducation(dt, selAcademy_3, SelType_3, selYear_3, txtSchoolName_3, txtID_3, txtShoolID_3, 3);
                        getEducation(dt, selAcademy_4, SelType_4, selYear_4, txtSchoolName_4, txtID_4, txtShoolID_4, 4);
                        getEducation(dt, selAcademy_5, SelType_5, selYear_5, txtSchoolName_5, txtID_5, txtShoolID_5, 5);
                        break;
                    case "7":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        getEducation(dt, selAcademy_2, SelType_2, selYear_2, txtSchoolName_2, txtID_2, txtShoolID_2, 2);
                        getEducation(dt, selAcademy_3, SelType_3, selYear_3, txtSchoolName_3, txtID_3, txtShoolID_3, 3);
                        getEducation(dt, selAcademy_4, SelType_4, selYear_4, txtSchoolName_4, txtID_4, txtShoolID_4, 4);
                        getEducation(dt, selAcademy_5, SelType_5, selYear_5, txtSchoolName_5, txtID_5, txtShoolID_5, 5);
                        getEducation(dt, selAcademy_6, SelType_6, selYear_6, txtSchoolName_6, txtID_6, txtShoolID_6, 6);
                        break;
                    case "8":
                        getEducation(dt, selAcademy_0, SelType_0, selYear_0, txtSchoolName_0, txtID_0, txtShoolID_0, 0);
                        getEducation(dt, selAcademy_1, SelType_1, selYear_1, txtSchoolName_1, txtID_1, txtShoolID_1, 1);
                        getEducation(dt, selAcademy_2, SelType_2, selYear_2, txtSchoolName_2, txtID_2, txtShoolID_2, 2);
                        getEducation(dt, selAcademy_3, SelType_3, selYear_3, txtSchoolName_3, txtID_3, txtShoolID_3, 3);
                        getEducation(dt, selAcademy_4, SelType_4, selYear_4, txtSchoolName_4, txtID_4, txtShoolID_4, 4);
                        getEducation(dt, selAcademy_5, SelType_5, selYear_5, txtSchoolName_5, txtID_5, txtShoolID_5, 5);
                        getEducation(dt, selAcademy_6, SelType_6, selYear_6, txtSchoolName_6, txtID_6, txtShoolID_6, 6);
                        getEducation(dt, selAcademy_7, SelType_7, selYear_7, txtSchoolName_7, txtID_7, txtShoolID_7, 7);
                        break;
                }
                #endregion
            }
            dt.Clear();
        }
        #endregion

        public void getEducation(DataTable dt, HtmlSelect selAcademy, HtmlSelect SelType, HtmlSelect selYear, HtmlInputText SchoolName, HtmlInputHidden ID, HtmlInputHidden ShoolID, int k)
        {
            if (selAcademy.Items.Count > 0)
            {
                for (int j = 0; j < selAcademy.Items.Count; j++)
                {
                    selAcademy.Items.RemoveAt(j);
                }
            }
            selAcademy.DataSource = sp.getUserInfo(int.Parse(dt.Rows[k]["SchoolID"].ToString()), 15);
            selAcademy.DataTextField = "Name";
            selAcademy.DataValueField = "ID";
            selAcademy.DataBind();

            selAcademy.Value = dt.Rows[k]["AcademyID"].ToString();
            ID.Value = dt.Rows[k]["ID"].ToString();
            SelType.Value = dt.Rows[k]["DegreeID"].ToString();
            selYear.Value = dt.Rows[k]["MatriculationYear"].ToString();
            SchoolName.Value = dt.Rows[k]["SchoolName"].ToString();
            ShoolID.Value = dt.Rows[k]["SchoolID"].ToString();
        }

        #region 根据学校编号加载院系信息
        public void getUserInfo(int id)
        {
            DataTable dt = sp.getUserInfo(id, 15);
            string strReulst = string.Empty;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strReulst += dt.Rows[i]["ID"].ToString() + "," + dt.Rows[i]["Name"].ToString() + "|";
                }
            }
            else
            {
                strReulst += "0,--|";
            }
            dt.Clear();
            Response.Write(strReulst);
            Response.End();
        }
        #endregion

        #region 添加/修改会员教育信息
        public void UpdateAddEducation()
        {
            UserEducationInfo info = new UserEducationInfo();
            info.UserID = this.UserID;
            String[] DegreeID = Request.Form["DegreeID"].Split('|');
            String[] SchoolID = Request.Form["SchoolID"].Split('|');
            String[] AcademyID = Request.Form["AcademyID"].Split('|');
            String[] MatriculationYear = Request.Form["MatriculationYear"].Split('|');
            String[] ID = Request.Form["ID"].Split('|');
            String[] DelID = Request.Form["DelID"].Split('|');

            info.PostIP = Public.GetClientIP();


            if (DelID.Length > 1)
            {
                for (int j = 0; j < DelID.Length - 1; j++)
                {
                    if (DelID[j] != "0")
                    {
                        sp.DelInfo(int.Parse(DelID[j]), 0);//删除教育信息
                    }
                }
            }
            for (int i = 0; i < int.Parse(Request.Form["count"]); i++)
            {
                info.DegreeID = int.Parse(DegreeID[i]);
                info.SchoolID = int.Parse(SchoolID[i]);
                info.AcademyID = int.Parse(AcademyID[i]);
                info.MatriculationYear = int.Parse(MatriculationYear[i]);
                sp.AddEducationInfo(info, int.Parse(ID[i]));
            }
            LoadEducationInfo();
            Response.Write("保存成功！");
            Response.End();
        }
        #endregion
    }
}

