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

    protected void Page_Load(object sender, EventArgs e)
    {
        a.check("|study|");

        if (!IsPostBack)
        {
            string strSql = string.Empty;
            string ts = Request.QueryString["ts"];
            long id = xabara.isNum(Request.QueryString["id"]);
            DataTable dt;

            if (ts.Equals("t"))
            {
                strSql = "select * from (select XabaraUser.userName,XabaraUser.userEmail,studentinfo.sID,studentinfo.uID,studentinfo.GradeIDs from studentinfo left join XabaraUser on studentinfo.uID=XabaraUser.userCardID where studentinfo.workFlag>0 and studentinfo.workFlag<3) as newTable left join onlineorder on onlineorder.userID=newTable.uID where ts='s' and onlineorder.relatingID=" + id.ToString() + " order by onlineorder.id ASC ";
                //Response.Write(strSql);
                //Response.End();
                dt = sql.getDataTable(strSql);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.guestList.Items.Add(new ListItem(dt.Rows[i]["userName"].ToString().Trim() + " [" + wp.IDsTreeName(dt.Rows[i]["GradeIDs"].ToString().Trim(), ",") + "]", dt.Rows[i]["sID"].ToString().Trim()));
                }
                dt.Dispose();
            }
            else
            {
                strSql = "select * from (select XabaraUser.userName,XabaraUser.userEmail,teacherInfo.tID,teacherInfo.uID,teacherInfo.teachTypeID from teacherInfo left join XabaraUser on teacherInfo.uID=XabaraUser.userCardID where teacherInfo.workFlag>0 and teacherInfo.workFlag<3) as newTable left join onlineorder on onlineorder.userID=newTable.uID where ts='t' and onlineorder.relatingID=" + id.ToString() + " order by onlineorder.id ASC ";

                //Response.Write(strSql);
                //Response.End();
                dt = sql.getDataTable(strSql);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.guestList.Items.Add(new ListItem(dt.Rows[i]["userName"].ToString().Trim() + " [" + wp.IDsTreeName(dt.Rows[i]["teachTypeID"].ToString().Trim(), ",") + "]", dt.Rows[i]["tID"].ToString().Trim()));
                }
                dt.Dispose();
            }
        }
    }

    protected void postBotton_Click(object sender, EventArgs e)
    {
        string strSql = string.Empty;
        string ts = Request.QueryString["ts"];
        long id = xabara.isNum(Request.QueryString["id"]);
        string guestListID = guestList.Text.ToString();
        string ifStr = string.Empty;
        string[] strValue = new string[10];
        string returnUrl = string.Empty;

        if (ts.Equals("t"))
        {
            if (xabara.isNum(guestListID) > 0)
            {
                ifStr = " and teacherInfo.tID=" + guestListID;
            }
            strSql = "select XabaraUser.userName,XabaraUser.userEmail from teacherInfo left join XabaraUser on teacherInfo.uID=XabaraUser.userCardID where teacherInfo.workFlag<3 " + ifStr;
        }
        else
        {
            if (xabara.isNum(guestListID) > 0)
            {
                ifStr = " and studentinfo.sID=" + id.ToString();
            }

            strSql = "select XabaraUser.userName,XabaraUser.userEmail from studentinfo left join XabaraUser on studentinfo.uID=XabaraUser.userCardID where studentInfo.workFlag<3 " + ifStr;
        }

        DataTable dt = sql.getDataTable(strSql);
        if (dt.Rows.Count > 0)
        {
            strValue[0] = "3";  //结束双方已安排工作
            if (ts.Equals("t"))
            {
                sql.update("teacherInfo", "@workFlag", strValue, "tID=" + id.ToString());
                sql.update("studentinfo", "@workFlag", strValue, "sID=" + guestListID);
                strValue[0] = id.ToString();
                strValue[1] = guestList.Text;

                returnUrl = "/teacher/view/" + id.ToString() + ".htm";
            }
            else
            {
                sql.update("studentinfo", "@workFlag", strValue, "sID=" + id.ToString());
                sql.update("teacherInfo", "@workFlag", strValue, "tID=" + guestListID);
                strValue[0] = guestList.Text;
                strValue[1] = id.ToString();

                returnUrl = "/student/view/" + id.ToString() + ".htm";
            }

            //插入双方家教记录
            string strDim = "@tID,@sID,@myCommentate,@myDate,@myIP,@postID,@postDate,@postIP";
            strValue[2] = CHAR.htmSafe(mInfo.Text);
            strValue[3] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            strValue[4] = ip.address();
            strValue[5] = a.uid;
            strValue[6] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            strValue[7] = ip.address();
            sql.update("ts", strDim, strValue, string.Empty);

            sql.log(a.uid, "admin", dt.Rows[0]["userName"].ToString() + " 教员已安排");

            Response.Redirect(returnUrl, true);
        }
        else
        {
            js.div(false, "", "text", "已安排的请不要重复操作！", 350, 150, returnUrl, "close");
        }
        dt.Dispose();
    }
}