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 System.Xml;
using System.IO;
using xabaraCOM;

public partial class _Default : System.Web.UI.Page
{
    webPublic wp = new webPublic();
    fileDocument f = new fileDocument();
    sqlServer sql = new sqlServer();
    advertisement ad = new advertisement();
    classify c = new classify();

    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql = string.Empty;
        string ifStr = string.Empty;
        DataTable dt;

        if (!IsPostBack)
        {
            widthAD1.ClassID = "2|400|300";  //广告参数

            ad250.Text = ad.read("", 250, 250, 1, false, true, "");
            ad705.Text = ad.read("20130427191139307", 705, 0, 1, false, true, "");
            ad960.Text = ad.read("", 960, 90, 1, false, true, "");

            string pca = string.Empty;
            //首页需要按区域数据过滤的，自行开启
            /*long p = 0;   //省份
            long cc = 0;   //市级
            long a = 0;   //县区级

            p = xabara.isNum(xml.read("xabara.config", "province", 1));
            cc = xabara.isNum(xml.read("xabara.config", "city", 1));
            a = xabara.isNum(xml.read("xabara.config", "area", 1));
            
            if (p < 1)
            {
                if (Request.Cookies["xabaraWhere"] != null)   //省市县cookies检查
                {
                    p = xabara.isNum(Request.Cookies["xabaraWhere"]["p"].ToString().Trim());
                    cc = xabara.isNum(Request.Cookies["xabaraWhere"]["c"].ToString().Trim());
                    a = xabara.isNum(Request.Cookies["xabaraWhere"]["a"].ToString().Trim());
                }
            }            
            if (p > 0)
            {
                pca = " and teacherInfo.provinceID=" + p.ToString();
                if (cc > 0)
                {
                    pca += " and teacherInfo.cityID=" + cc.ToString();
                }
                if (a > 0)
                {
                    pca += " and teacherInfo.areaID=" + a.ToString();
                }
            }*/

            strSql = "select top 7 XabaraUser.Uid,XabaraUser.userImg,XabaraUser.userName,XabaraUser.userSex,teacherInfo.tID,teacherInfo.teachTypeID,teacherInfo.readNums,teacherInfo.postDate from teacherInfo left join XabaraUser on XabaraUser.userCardID = teacherInfo.uID where teacherInfo.workFlag>0 and teacherInfo.workFlag<3 " + pca + " order by teacherInfo.adBeginDate DESC,teacherInfo.readNums DESC";  //推荐教员
            //Response.Write(strSql);
            teachCount.DataSource = sql.getDataTable(strSql).DefaultView;
            teachCount.DataBind();
            teachCount.Dispose();

            strSql = "select top 48 XabaraUser.Uid,XabaraUser.userImg,XabaraUser.userName,XabaraUser.userSex,teacherInfo.tID,teacherInfo.teachTypeID,teacherInfo.readNums,teacherInfo.postDate from teacherInfo left join XabaraUser on XabaraUser.userCardID = teacherInfo.uID where teacherInfo.workFlag>0 and XabaraUser.userImg is not null " + pca + " order by teacherInfo.adBeginDate DESC,teacherInfo.readNums DESC";  //滚动图片教员
            dt = sql.getDataTable(strSql);
            int picCount = dt.Rows.Count;
            if (picCount > 0)
            {
                for (int i = 0; i < picCount; i++)
                {
                    readPicScroll.Text += "<li><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><a href=\"/teacher/view/" + dt.Rows[i]["tID"].ToString().Trim() + ".htm\" target=\"_blank\"><img src=\"" + f.picDisplay(dt.Rows[i]["userImg"].ToString().Trim()) + "\" style=\"width:110px;height:110px;\" class=\"imgLine\" /></a></td></tr><tr><td style=\"height:3px;font-weight:0px;\"></td></tr><tr><td style=\"height:22px; line-height:22px; background-color: #f5fcec; padding:2px;\">&nbsp;<a href=\"/teacher/view/" + dt.Rows[i]["tID"].ToString().Trim() + ".htm\" target=\"_blank\">姓名：" + wp.cnName(dt.Rows[i]["userName"].ToString().Trim(), dt.Rows[i]["userSex"].ToString().Trim(), "t") + "</a></td></tr><tr><td style=\"height:3px;font-weight:0px;\"></td></tr><tr><td style=\"height:22px; line-height:22px; background-color: #f5fcec; padding:2px;\">&nbsp;<a href=\"/teacher/view/" + dt.Rows[i]["tID"].ToString().Trim() + ".htm\" target=\"_blank\">" + CHAR.left(idsName(dt.Rows[i]["teachTypeID"].ToString().Trim()), 7) + "</a></td></tr></table></li>";
                }
            }
            else
            {
                picScroll.Visible = false;
            }
            dt.Dispose();

            strSql = "SELECT XabaraUser.Uid,XabaraUser.userCardID,XabaraUser.userName,XabaraUser.userSex,news.* FROM (SELECT top 9 uID,tID,workFlag,teachTypeID,studySchool,studyProfession,nowWork,postDate FROM teacherinfo where workFlag>0 " + pca + " ORDER BY tid DESC) AS news LEFT JOIN xabarauser ON xabarauser.userCardID=news.uID ORDER BY news.tID DESC";
            tList.DataSource = sql.getDataTable(strSql).DefaultView;  //最新教员
            tList.DataBind();
            tList.Dispose();

            teacherList.DataSource = sql.getDataTable("select top 5 XabaraUser.Uid,XabaraUser.userCardID,XabaraUser.userName,XabaraUser.userSex,teacherInfo.tID,teacherInfo.workFlag,teacherInfo.teachTypeID,teacherInfo.studySchool,teacherInfo.studyProfession,teacherInfo.nowWork,teacherInfo.postDate from teacherInfo left join XabaraUser on XabaraUser.userCardID = teacherInfo.uID where teacherInfo.workFlag>0 and teacherInfo.nowWork=68 " + pca + " order by teacherInfo.workFlag ASC,teacherInfo.tID DESC").DefaultView;  //专职教员
            teacherList.DataBind();
            teacherList.Dispose();

            schoolList.DataSource = sql.getDataTable("select top 5 XabaraUser.Uid,XabaraUser.userCardID,XabaraUser.userName,XabaraUser.userSex,teacherInfo.tID,teacherInfo.workFlag,teacherInfo.teachTypeID,teacherInfo.studySchool,teacherInfo.studyProfession,teacherInfo.nowWork,teacherInfo.postDate from teacherInfo left join XabaraUser on XabaraUser.userCardID = teacherInfo.uID where teacherInfo.workFlag>0 and teacherInfo.nowWork=67 " + pca + " order by teacherInfo.workFlag ASC,teacherInfo.tID DESC").DefaultView;  //在校大学生
            schoolList.DataBind();
            schoolList.Dispose();

            //学员表格更新
            pca = pca.Replace("teacherInfo", "studentInfo");

            sList.DataSource = sql.getDataTable("select top 8 XabaraUser.Uid,XabaraUser.userName,XabaraUser.userSex,studentInfo.sID,studentInfo.GradeIDs,studentInfo.teachInfo,studentInfo.myGradeId from studentInfo left join XabaraUser on XabaraUser.userCardID = studentInfo.uID where studentInfo.workFlag>0 " + pca + " order by studentInfo.workFlag ASC,studentInfo.sID DESC").DefaultView;  //最新学员
            sList.DataBind();
            sList.Dispose();

            strSql = "select top 10 XabaraUser.Uid,XabaraUser.userName,XabaraUser.userSex,studentInfo.sID,studentInfo.GradeIDs,studentInfo.teachInfo,studentInfo.myGradeId from studentInfo left join XabaraUser on XabaraUser.userCardID = studentInfo.uID where studentInfo.workFlag>0 and studentInfo.workFlag<3 " + pca + " order by studentInfo.adBeginDate DESC,studentInfo.readNums DESC";  //推荐学员
            studentCount.DataSource = sql.getDataTable(strSql).DefaultView;
            studentCount.DataBind();
            studentCount.Dispose();

            dt = sql.getDataTable("select top 5 * from NewsTree where TreeType='study' and TreeID=0 order by ListID ASC,Tid ASC");    //所有分类读取
            int classCount = dt.Rows.Count;
            for (int t = 0; t < classCount; t++)
            {
                classList.Text += "<tr><td colspan=\"3\" style=\"text-align:left; font-weight:bold; height:18px; line-height:18px;\"><img  src=\"/images/icon05.gif\" align=\"absmiddle\" /> " + dt.Rows[t]["TreeName"].ToString().Trim() + "</td></tr>";

                DataTable td = sql.getDataTable("select * from NewsTree where TreeType='study' and TreeID=" + dt.Rows[t]["Tid"].ToString().Trim() + " order by ListID ASC,Tid ASC");
                for (int tt = 0; tt < td.Rows.Count; tt++)
                {
                    if ((tt + 1) % 3 == 1)
                    {
                        classList.Text += "<tr>";
                    }
                    classList.Text += "<td style=\"text-align:left;\"><a href=\"/teacher/" + td.Rows[tt]["Tid"].ToString().Trim() + ".htm\" style=\"color:#457b09;\">" + td.Rows[tt]["TreeName"].ToString().Trim() + "</a></td>";
                    if ((tt + 1) % 3 == 0)
                    {
                        classList.Text += "</tr>";
                    }
                }
                td.Dispose();

                if (t < classCount - 1)
                {
                    classList.Text += "<tr><td colspan=\"3\" style=\"background-image:url('/images/line.jpg'); background-repeat:repeat-x; height:5px;\"></td></tr>";
                }
            }
            dt.Dispose();
            
            string cidStr = c.ReadAllClassID("help", 0); //得到帮助所有ID
            ifStr = string.Empty;
            if (cidStr.Length > 1)
            {
                string[] ids = cidStr.Split(new char[] { ',' });
                for (int i = 0; i <= ids.GetUpperBound(0); i++)
                {
                    ifStr += " select * from (select top 10 Nid,NewTitle,titleFontB,titleFontC,TopDate,PostDate from NewsXabara where adminPass=1 and  treeTid=" + ids[i].ToString().Trim() + " Order By TopDate desc,PostDate desc) as t" + i.ToString() + " union ";
                }
                ifStr = "(" + ifStr.Substring(0, ifStr.Length - 6) + ") as news";
            }
            else
            {
                ifStr = "NewsXabara";
            }
            strSql = "select top 8 * from " + ifStr + " Order By TopDate desc,PostDate desc";
            //Response.Write(strSql);
            //Response.End();
            memberRepeater.DataSource = sql.getDataTable(strSql).DefaultView;  //帮助中心
            memberRepeater.DataBind();
            memberRepeater.Dispose();

            sql.readRepeater("news", newsRepeater, 165, 6, "", "TopDate");  //最新通告
            sql.readRepeater("news", news166, 166, 5, "", "TopDate");  //教育新闻
            sql.readRepeater("news", news167, 167, 5, "", "TopDate");  //学习方法
            sql.readRepeater("news", news168, 168, 5, "", "TopDate");  //教育方法
            sql.readRepeater("news", news170, 170, 5, "", "TopDate");  //家长课堂

            string tel = xml.read("xabara.config", "freePhone",180);
            if (string.IsNullOrEmpty(tel as string))
            {
                tel = xml.read("xabara.config", "mobile",180);
            }            
            if (string.IsNullOrEmpty(tel as string))
            {
                tel = xml.read("xabara.config", "webTel",180);
            }
            telInfo.Text = tel;

            string qqID = xml.read("xabara.config", "qq",180);
            string[] qq = xabara.QQ(qqID, "46").Replace("&nbsp;", "|").Split(new char[] { '|' });
            int qqCount=qq.GetUpperBound(0);
            for (int q = 0; q < qqCount; q++)
            {
                qqLink.Text += qq[q].ToString().Trim() + "&nbsp;";
                if (q == 2)
                {
                    break;
                }
            }

            //友情链接
            strSql = "select top 4 * from linkXabara where classID=185 order by listID ASC,lid ASC";
            linkPartner.DataSource = sql.getDataTable(strSql);
            linkPartner.DataBind();
            linkPartner.Dispose();

            dt = sql.getDataTable("select * from linkXabara where classID=3 order by listID ASC,lid ASC");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string nameStr = dt.Rows[i]["linkName"].ToString().Trim();
                    linkSite.Text += "<a href=\"" + dt.Rows[i]["linkUrl"].ToString().Trim() + "\" target=\"_blank\">" + nameStr + "</a>&nbsp;&nbsp;";
                }
            }
            linkSite.Text += "<a href=\"http://jiajiao.zdianpu.com\" target=\"_blank\" style=\"color:red;\">期待您的加盟</a>　";
            dt.Dispose();
        }

        this.Page.Title = xml.read("xabara.config", "webName",180) + xml.read("xabara.config", "titleMeta",180);
    }

    public string getTreeName(string str)
    {
        return c.readClassName_Link(xabara.isNum(str), "", "");
    }

    public string idsName(string str)
    {
        return wp.IDsTreeName(str, ", ");
    }

    public string name(string txt, string sex, string ts)
    {
        return wp.cnName(txt, sex, ts);
    }

    public string img(string str)
    {
        return f.picDisplay(str);
    }

    public string getPCA(string p, string cc, string a)  //得到省市区镇
    {
        return c.cityName(p, cc, a);
    }

    public string cutWords(string str, int num)
    {
        return CHAR.left(str, num);
    }

    public string getTitleColor(string title, string b, string c)
    {
        string titleBC = string.Empty;
        if (!string.IsNullOrEmpty(title as string))
        {
            if (b.Equals("1"))
            {
                titleBC = "font-weight:bold;";
            }

            if (!string.IsNullOrEmpty(c as string))
            {
                titleBC += "color:" + c + ";";
            }

            titleBC = "<span style=\"" + titleBC + "\">" + title + "</span>";
        }

        return titleBC;
    }

    public string cNums(string sOrt, string id)  //编号
    {
        return wp.createNums(sOrt, id);
    }

    public string getStudentInfo(string id)
    {
        return wp.infoTeachFlag(id);
    }
}