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 member_student_edit : System.Web.UI.Page
{
    sqlServer sql = new sqlServer();
    webPublic wp = new webPublic();
    user u = new user();
    javasrcipt js = new javasrcipt();
    ipAddress ip = new ipAddress();

    protected void Page_Load(object sender, EventArgs e)
    {
        u.check("", url.before);
        wp.checkUserInfo();
        Page.Title = "修改家教信息　" + xml.read("xabara.config", "webName", 180);

        if (!IsPostBack)
        {
            long idTemp = xabara.isNum(Request["id"]);

            string strSQL = "select * from NewsTree where TreeType='study' and treeID=0 order by ListID ASC,Tid ASC";
            DataTable dt = sql.getDataTable(strSQL);
            this.gradeList.Items.Add(new ListItem("== 请选择 ==", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.gradeList.Items.Add(new ListItem(dt.Rows[i]["TreeName"].ToString().Trim(), dt.Rows[i]["Tid"].ToString().Trim()));
            }
            this.gradeList.DataBind();

            strSQL = "select * from NewsTree where TreeType='grade' and treeID=0 order by ListID ASC,Tid ASC";
            dt = sql.getDataTable(strSQL);
            this.studyGradeList.Items.Add(new ListItem("== 请选择 ==", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.studyGradeList.Items.Add(new ListItem(dt.Rows[i]["TreeName"].ToString().Trim(), dt.Rows[i]["Tid"].ToString().Trim()));
            }
            this.studyGradeList.DataBind();

            dt = sql.getDataTable("select * from selectProvince order by id");
            this.ProvinceList.Items.Clear();
            this.ProvinceList.Items.Add(new ListItem("== 省份 ==", ""));
            for (int p = 0; p < dt.Rows.Count; p++)
            {
                this.ProvinceList.Items.Add(new ListItem(dt.Rows[p]["provinceTxt"].ToString().Trim(), dt.Rows[p]["provinceID"].ToString().Trim()));
            }
            this.ProvinceList.DataBind();

            strSQL = "select * from NewsTree where TreeType='studytype' order by ListID ASC,Tid ASC";
            dt = sql.getDataTable(strSQL);
            this.studyType.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.studyType.Items.Add(new ListItem(dt.Rows[i]["TreeName"].ToString().Trim(), dt.Rows[i]["Tid"].ToString().Trim()));
            }
            this.studyType.DataBind();

            strSQL = "select * from NewsTree where TreeType='teachertype' order by ListID ASC,Tid ASC";
            dt = sql.getDataTable(strSQL);
            this.teacherType.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.teacherType.Items.Add(new ListItem(dt.Rows[i]["TreeName"].ToString().Trim(), dt.Rows[i]["Tid"].ToString().Trim()));
            }
            this.teacherType.DataBind();

            strSQL = "select * from NewsTree where TreeType='teacherstudy' order by ListID ASC,Tid ASC";
            dt = sql.getDataTable(strSQL);
            this.teachStudy.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.teachStudy.Items.Add(new ListItem(dt.Rows[i]["TreeName"].ToString().Trim(), dt.Rows[i]["Tid"].ToString().Trim()));
            }
            this.teachStudy.DataBind();

            this.teachWeek.Items.Clear();
            this.teachWeek.Items.Add(new ListItem("面议", "0"));
            for (int i = 1; i < 8; i++)
            {
                this.teachWeek.Items.Add(new ListItem("每周" + i.ToString() + "次", i.ToString()));
            }
            this.teachWeek.Items.Add(new ListItem("包月", "8"));
            this.teachWeek.DataBind();

            this.studyTime.Items.Clear();
            for (int j = 1; j < 4; j++)
            {
                string jStr = string.Empty;
                switch (j)
                {
                    case 1:
                        jStr = "上";
                        break;
                    case 2:
                        jStr = "下";
                        break;
                    case 3:
                        jStr = "晚";
                        break;
                }

                for (int i = 1; i < 8; i++)
                {
                    string iStr = string.Empty;
                    switch (i)
                    {
                        case 1:
                            iStr = "周一";
                            break;
                        case 2:
                            iStr = "周二";
                            break;
                        case 3:
                            iStr = "周三";
                            break;
                        case 4:
                            iStr = "周四";
                            break;
                        case 5:
                            iStr = "周五";
                            break;
                        case 6:
                            iStr = "周六";
                            break;
                        case 7:
                            iStr = "周日";
                            break;
                    }
                    this.studyTime.Items.Add(new ListItem(iStr + jStr, i.ToString() + j.ToString()));
                }
            }
            this.studyTime.DataBind();

            this.payBit.Items.Clear();
            this.payBit.Items.Add(new ListItem("面议", "0"));
            for (int i = 1; i < 9; i++)
            {
                this.payBit.Items.Add(new ListItem("每" + i.ToString() + "小时", i.ToString()));
            }
            this.payBit.Items.Add(new ListItem("包月", "9"));
            this.payBit.DataBind();

            regExpression reg = new regExpression();
            payRegularExpressionValidator.ValidationExpression = reg.Double;
            payRegularExpressionValidator.ErrorMessage = reg.DoubleError;

            dt = sql.getDataTable("select * from studentInfo where uID='" + u.userCardID + "' and sID=" + idTemp.ToString());    //修改信息读取
            if (dt.Rows.Count > 0)
            {
                if (xabara.isNum(dt.Rows[0]["workFlag"].ToString()) > 2)
                {
                    js.div(false, "", "text", "对不起，已安排的信息不能再次修改！", 350, 150, "add.aspx", "top");
                }

                this.gradeList.Items.FindByValue(dt.Rows[0]["gradeID"].ToString().Trim()).Selected = true;
                DataTable dtGrade = sql.getDataTable("select * from NewsTree where TreeType='study' and TreeID=" + gradeList.Text.Trim() + " order by ListID ASC,Tid ASC");
                this.gradeCheck.Items.Clear();
                for (int p = 0; p < dtGrade.Rows.Count; p++)
                {
                    this.gradeCheck.Items.Add(new ListItem(dtGrade.Rows[p]["TreeName"].ToString().Trim(), dtGrade.Rows[p]["Tid"].ToString().Trim()));
                }
                this.gradeCheck.DataBind();
                gradeCheck.Dispose();
                wp.readCheckBox(gradeCheck, dt.Rows[0]["GradeIDs"].ToString().Trim());
                try
                {
                    this.studyGradeList.Items.FindByValue(dt.Rows[0]["myGradeId"].ToString().Trim()).Selected = true;
                }
                catch { }
                try
                {
                    this.ProvinceList.Items.FindByValue(dt.Rows[0]["provinceID"].ToString().Trim()).Selected = true;
                }
                catch { }

                DataTable dtCity = sql.getDataTable("select * from selectCity where cFather=" + ProvinceList.Text.Trim() + " order by id");
                this.CityList.Items.Clear();
                for (int p = 0; p < dtCity.Rows.Count; p++)
                {
                    this.CityList.Items.Add(new ListItem(dtCity.Rows[p]["cityTxt"].ToString().Trim(), dtCity.Rows[p]["cityID"].ToString().Trim()));
                }
                this.CityList.DataBind();
                dtCity.Dispose();
                try
                {
                    this.CityList.Items.FindByValue(dt.Rows[0]["cityID"].ToString().Trim()).Selected = true;
                }
                catch { }

                string areaStr = dt.Rows[0]["areaID"].ToString().Trim();
                DataTable dtArea = sql.getDataTable("select * from selectArea where aFather='" + dt.Rows[0]["cityID"].ToString().Trim() + "' order by id");
                this.areaList.Items.Clear();
                if (dtArea.Rows.Count > 0)
                {
                    for (int p = 0; p < dtArea.Rows.Count; p++)
                    {
                        this.areaList.Items.Add(new ListItem(dtArea.Rows[p]["areaTxt"].ToString().Trim(), dtArea.Rows[p]["areaID"].ToString().Trim()));
                    }
                }
                else
                {
                    this.areaList.Items.Add(new ListItem("== 暂无 ==", ""));
                }
                this.areaList.DataBind();
                if (!string.IsNullOrEmpty(areaStr as string))
                {
                    try
                    {
                        this.areaList.Items.FindByValue(areaStr).Selected = true;
                    }
                    catch { }
                }
                dtArea.Dispose();

                school.Text = CHAR.htmReturn(dt.Rows[0]["schoolName"].ToString().Trim());
                try
                {
                    this.sexList.Items.FindByValue(dt.Rows[0]["sexMy"].ToString().Trim()).Selected = true;
                }
                catch { }
                try
                {
                    this.studyType.Items.FindByValue(dt.Rows[0]["studyTypeID"].ToString().Trim()).Selected = true;
                }
                catch { }
                myAbout.Text = CHAR.htmReturn(dt.Rows[0]["myInfo"].ToString().Trim());
                try
                {
                    this.sexTeacher.Items.FindByValue(dt.Rows[0]["sexTeacher"].ToString().Trim()).Selected = true;
                }
                catch { }
                wp.readCheckBox(teacherType, dt.Rows[0]["teacherTypeIDs"].ToString().Trim());
                wp.readCheckBox(teachStudy, dt.Rows[0]["teachStudyIDs"].ToString().Trim());
                aboutTeacher.Text = CHAR.htmReturn(dt.Rows[0]["teachInfo"].ToString().Trim());
                try
                {
                    this.teachWeek.Items.FindByValue(dt.Rows[0]["teachWeek"].ToString().Trim()).Selected = true;
                }
                catch { }
                wp.readCheckBox(studyTime, dt.Rows[0]["studyTimeIDs"].ToString().Trim());
                studyAbout.Text = CHAR.htmReturn(dt.Rows[0]["studyInfo"].ToString().Trim());
                payMoney.Text = dt.Rows[0]["payMoney"].ToString();
                try
                {
                    this.payBit.Items.FindByValue(dt.Rows[0]["payBit"].ToString().Trim()).Selected = true;
                }
                catch { }
                try
                {
                    this.carMoney.Items.FindByValue(dt.Rows[0]["carMoney"].ToString().Trim()).Selected = true;
                }
                catch { }

                countHid.Value = dt.Rows[0]["readNums"].ToString().Trim();
            }
            else
            {
                js.div(false, "", "text", "您的操作有误！", 350, 150, "default.aspx", "top");
            }

            dt.Dispose();
        }

        xabara.safeHistory();
    }

    protected void gradeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(gradeList.Text.Trim() as string))
        {
            string strSQL = "select * from NewsTree where TreeType='study' and TreeID=" + gradeList.Text.Trim() + " order by ListID ASC,Tid ASC";
            DataTable dt = sql.getDataTable(strSQL);
            this.gradeCheck.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.gradeCheck.Items.Add(new ListItem(dt.Rows[i]["TreeName"].ToString().Trim(), dt.Rows[i]["Tid"].ToString().Trim()));
            }
            this.gradeCheck.DataBind();
            this.gradeBox.Visible = true;
        }
        else
        {
            this.gradeCheck.Items.Clear();
            this.gradeBox.Visible = false;
        }
    }

    protected void ProvinceList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.CityList.Items.Clear();
        this.CityList.Items.Add(new ListItem("== 地级市 ==", ""));
        if (!string.IsNullOrEmpty(ProvinceList.Text as string))
        {
            DataTable dt;
            dt = sql.getDataTable("select * from selectCity where cFather=" + ProvinceList.Text.Trim() + " order by id");
            for (int p = 0; p < dt.Rows.Count; p++)
            {
                this.CityList.Items.Add(new ListItem(dt.Rows[p]["cityTxt"].ToString().Trim(), dt.Rows[p]["cityID"].ToString().Trim()));
            }
            this.CityList.DataBind();
            dt.Dispose();
        }
        this.areaList.Items.Clear();
        this.areaList.Items.Add(new ListItem("== 县级市 ==", ""));
    }

    protected void cityList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.areaList.Items.Clear();
        if (!string.IsNullOrEmpty(CityList.Text as string))
        {
            DataTable dt;
            dt = sql.getDataTable("select * from selectArea where aFather=" + CityList.Text.Trim() + " order by id");
            if (dt.Rows.Count > 0)
            {
                for (int p = 0; p < dt.Rows.Count; p++)
                {
                    this.areaList.Items.Add(new ListItem(dt.Rows[p]["areaTxt"].ToString().Trim(), dt.Rows[p]["areaID"].ToString().Trim()));
                }
            }
            else
            {
                this.areaList.Items.Add(new ListItem("== 暂无 ==", ""));
            }
            this.areaList.DataBind();
            dt.Dispose();
        }
    }

    protected void postButton_Click(object sender, EventArgs e)
    {
        string strDim = "@uID,@gradeID,@GradeIDs,@myGradeId,@provinceID,@cityID,@areaID,@schoolName,@sexMy,@studyTypeID,@myInfo,@sexTeacher,@teacherTypeIDs,@teachStudyIDs,@teachInfo,@teachWeek,@studyTimeIDs,@studyInfo,@payMoney,@payBit,@carMoney,@adBeginDate,@adEndDate,@workFlag,@workDate,@readNums,@postDate,@postIP";
        string[] strValue = new string[28];
        strValue[0] = u.userCardID;
        strValue[1] = gradeList.Text.Trim();
        strValue[2] = wp.getCheckBox(gradeCheck, 1);
        strValue[3] = studyGradeList.Text.Trim();
        strValue[4] = ProvinceList.Text.Trim();
        strValue[5] = CityList.Text.Trim();
        strValue[6] = areaList.Text.Trim();
        strValue[7] = CHAR.htmSafe(school.Text.Trim());
        strValue[8] = sexList.Text.Trim();
        strValue[9] = studyType.Text.Trim();
        strValue[10] = CHAR.htmSafe(myAbout.Text);
        strValue[11] = sexTeacher.Text.Trim();
        strValue[12] = wp.getCheckBox(teacherType, 1);
        strValue[13] = wp.getCheckBox(teachStudy, 1);
        strValue[14] = CHAR.htmSafe(aboutTeacher.Text);
        strValue[15] = teachWeek.Text.Trim();
        strValue[16] = wp.getCheckBox(studyTime, 1);
        strValue[17] = CHAR.htmSafe(studyAbout.Text);
        strValue[18] = payMoney.Text.Trim();
        strValue[19] = payBit.Text.Trim();
        strValue[20] = carMoney.Text.Trim();
        strValue[21] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        strValue[22] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        strValue[23] = "0";//信息条默认不通过，需审核
        strValue[24] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        strValue[25] = countHid.Value;
        strValue[26] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        strValue[27] = ip.address();

        if (wp.checkBoxValue(strValue[2] + "," + strValue[12] + "," + strValue[13] + "," + strValue[16]))
        {
            sql.log(u.userCardID, "user", "修改家教");

            wp.tzMail(u.userCardID, "家长修改信息啦", "快去审核了喽");

            long idTemp = xabara.isNum(Request["id"]);
            string str = sql.update("studentInfo", strDim, strValue, "uID='" + u.userCardID + "' and sID=" + idTemp.ToString());
            js.div(false, "", "text", str + "，待审核通过后即可正常显示！", 400, 150, "default.aspx", "goto");
        }
    }
}