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_work : JuSNS.UI.Page.UserPage
    {
        Space sp = new 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;
            }
            LoadCareerInfo();
            try
            {
                string type = Request.Form["Type"];

                if (type == "Edit")
                {
                    UpdateAddCareer();
                }
            }
            catch { }
        }

        #region 初始化工作信息
        public void LoadCareerInfo()
        {
            DataTable dt = sp.getUserInfo(this.UserID, 9);
            if (dt != null && dt.Rows.Count > 0)
            {
                this.txtCount.Value = dt.Rows.Count.ToString();
                if (int.Parse(this.txtCount.Value) > 5) { this.txtCount.Value = "5"; }
                #region
                switch (this.txtCount.Value)
                {
                    case "1":
                        getCareer(dt, companyName_0, company_desc_0, Check_0, company_from_0, company_from_month_0, company_to_0, company_to_month_0, txtID_0, 0);
                        break;
                    case "2":
                        getCareer(dt, companyName_0, company_desc_0, Check_0, company_from_0, company_from_month_0, company_to_0, company_to_month_0, txtID_0, 0);
                        getCareer(dt, companyName_1, company_desc_1, Check_1, company_from_1, company_from_month_1, company_to_1, company_to_month_1, txtID_1, 1);
                        break;
                    case "3":
                        getCareer(dt, companyName_0, company_desc_0, Check_0, company_from_0, company_from_month_0, company_to_0, company_to_month_0, txtID_0, 0);
                        getCareer(dt, companyName_1, company_desc_1, Check_1, company_from_1, company_from_month_1, company_to_1, company_to_month_1, txtID_1, 1);
                        getCareer(dt, companyName_2, company_desc_2, Check_2, company_from_2, company_from_month_2, company_to_2, company_to_month_2, txtID_2, 2);
                        break;
                    case "4":
                        getCareer(dt, companyName_0, company_desc_0, Check_0, company_from_0, company_from_month_0, company_to_0, company_to_month_0, txtID_0, 0);
                        getCareer(dt, companyName_1, company_desc_1, Check_1, company_from_1, company_from_month_1, company_to_1, company_to_month_1, txtID_1, 1);
                        getCareer(dt, companyName_2, company_desc_2, Check_2, company_from_2, company_from_month_2, company_to_2, company_to_month_2, txtID_2, 2);
                        getCareer(dt, companyName_3, company_desc_3, Check_3, company_from_3, company_from_month_3, company_to_3, company_to_month_3, txtID_3, 3);
                        break;
                    case "5":
                        getCareer(dt, companyName_0, company_desc_0, Check_0, company_from_0, company_from_month_0, company_to_0, company_to_month_0, txtID_0, 0);
                        getCareer(dt, companyName_1, company_desc_1, Check_1, company_from_1, company_from_month_1, company_to_1, company_to_month_1, txtID_1, 1);
                        getCareer(dt, companyName_2, company_desc_2, Check_2, company_from_2, company_from_month_2, company_to_2, company_to_month_2, txtID_2, 2);
                        getCareer(dt, companyName_3, company_desc_3, Check_3, company_from_3, company_from_month_3, company_to_3, company_to_month_3, txtID_3, 3);
                        getCareer(dt, companyName_4, company_desc_4, Check_4, company_from_4, company_from_month_4, company_to_4, company_to_month_4, txtID_4, 4);
                        break;
                }
                #endregion
            }
            dt.Clear(); dt.Dispose();
        }
        #endregion

        public void getCareer(DataTable dt, HtmlInputText companyName, HtmlTextArea companyDesc, HtmlInputCheckBox Checkbox, HtmlSelect FromYear, HtmlSelect FromMonth, HtmlSelect ToYear, HtmlSelect ToMonth, HtmlInputHidden CareerID, int k)
        {
            string check = dt.Rows[k]["IsStill"].ToString();
            if (check == "True")
            {
                Checkbox.Checked = true;
            }
            else
            {
                Checkbox.Checked = false;
            }

            companyName.Value = dt.Rows[k]["Company"].ToString();
            companyDesc.Value = dt.Rows[k]["Description"].ToString();
            FromYear.Value = dt.Rows[k]["JoinYear"].ToString();
            FromMonth.Value = dt.Rows[k]["JoinMonth"].ToString();
            ToYear.Value = dt.Rows[k]["LeaveYear"].ToString();
            ToMonth.Value = dt.Rows[k]["LeaveMonth"].ToString();
            CareerID.Value = dt.Rows[k]["ID"].ToString();
        }


        #region 添加/修改会员工作信息
        public void UpdateAddCareer()
        {
            UserCareerInfo info = new UserCareerInfo();
            info.UserID = this.UserID;
            String[] companyName = Request.Form["companyName"].Split('|');
            String[] companyDesc = Request.Form["companyDesc"].Split('|');
            string[] CheckValue = Request.Form["CheckValue"].Split('|');
            String[] FromYear = Request.Form["FromYear"].Split('|');
            String[] FromMonth = Request.Form["FromMonth"].Split('|');
            String[] ToYear = Request.Form["ToYear"].Split('|');
            String[] ToMonth = Request.Form["ToMonth"].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]), 1);//删除工作信息
                    }
                }
            }
            for (int i = 0; i < int.Parse(Request.Form["count"]); i++)
            {
                info.Company = companyName[i];
                info.Description = companyDesc[i];
                info.IsStill = int.Parse(CheckValue[i]);
                info.JoinYear = int.Parse(FromYear[i]);
                info.JoinMonth = int.Parse(FromMonth[i]);
                info.LeaveYear = int.Parse(ToYear[i]);
                info.LeaveMonth = int.Parse(ToMonth[i]);
                sp.AddCareerInfo(info, int.Parse(ID[i]));
            }

            LoadCareerInfo();
            Response.Write("保存成功！");
            Response.End();
        }
        #endregion
    }
}
