<%@ WebHandler Language="C#" Class="EmployeeSearch" %>

using System;
using System.Web;
using Jayrock.Json;
using EB_BLL;
using System.Data;
public class EmployeeSearch : IHttpHandler {
    HttpRequest Request = null;
    HttpResponse Response = null;
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        Request = context.Request;
        Response = context.Response;

        if (!String.IsNullOrEmpty(Request["mid"]))
        {
            N25175_Meeting_Employee_BLL mebll = new N25175_Meeting_Employee_BLL();

            DataTable data = mebll.GetMeetingEmployee(new Guid(Request["mid"]));
            JsonArray result = new JsonArray();


            foreach (DataRow row in data.Rows)
            {
                JsonObject item = new JsonObject();
                item.Accumulate("name", row["ee_name"].ToString());
                item.Accumulate("id", row["ee_id"].ToString());
                result.Add(item);
            }


            Response.Write(result);
            Response.End();
        }else if (!String.IsNullOrEmpty(Request["did"]))
        {
            N25175_Employee_Department_BLL mebll = new N25175_Employee_Department_BLL();

            DataTable data = mebll.GetDepartmentEmployee(new Guid(Request["did"]));
            JsonArray result = new JsonArray();


            foreach (DataRow row in data.Rows)
            {
                JsonObject item = new JsonObject();
                item.Accumulate("name", row["ee_name"].ToString());
                item.Accumulate("id", row["ee_id"].ToString());
                result.Add(item);
            }


            Response.Write(result);
            Response.End();
        }
        else if (!String.IsNullOrEmpty(Request["query"]))
        {
            String query = Request["query"].ToString();
            String type = Request["t"];

            //支持拼音首字母缩写，使用前缀(@人员,#会议,&机构)可限定搜索范围
            if (type == "e" )
            {
                Employee(query);
            }
            else if (type == "m")
            {
                Meeting(query);
            }
            else if (type == "d" )
            {
                Department(query);
            }
            else
            {
                Employee(query);
            }
            JsonObject result = new JsonObject();
            result.Accumulate("suggestions", "");

            Response.Write(result);
            Response.End();
        }
    }

    private void Employee(String query)
    {
        JsonObject result = new JsonObject();
        JsonArray suggestions = new JsonArray();

        N25175_Employee_BLL goods = new N25175_Employee_BLL();
        DataTable data = goods.GetAutoComplete(query);

        foreach (DataRow row in data.Rows)
        {
            JsonObject item = new JsonObject();

            item.Accumulate("value", row["ee_name"].ToString());
            item.Accumulate("data", row["ee_id"].ToString());
            item.Accumulate("t", "e");
            suggestions.Add(item);
        }

        result.Accumulate("suggestions", suggestions);

        Response.Write(result);
        Response.End();
    }

    private void Meeting(String query)
    {
        JsonObject result = new JsonObject();
        JsonArray suggestions = new JsonArray();

        N25175_Meeting_BLL goods = new N25175_Meeting_BLL();
        DataTable data = goods.GetAutoComplete(query);

        foreach (DataRow row in data.Rows)
        {
            JsonObject item = new JsonObject();

            item.Accumulate("value", row["m_MeetingName"].ToString());
            item.Accumulate("data", row["m_id"].ToString());
            item.Accumulate("t", "m");
            suggestions.Add(item);
        }

        result.Accumulate("suggestions", suggestions);

        Response.Write(result);
        Response.End();
    }

    private void Department(String query)
    {
        JsonObject result = new JsonObject();
        JsonArray suggestions = new JsonArray();

        N25175_Department_BLL goods = new N25175_Department_BLL();
        DataTable data = goods.GetAutoComplete(query);

        foreach (DataRow row in data.Rows)
        {
            JsonObject item = new JsonObject();
         
            item.Accumulate("value", row["d_name"].ToString());
            item.Accumulate("data", row["d_id"].ToString());
            item.Accumulate("t", "d");
            suggestions.Add(item);
        }

        result.Accumulate("suggestions", suggestions);

        Response.Write(result);
        Response.End();
    }


    public bool IsReusable {
        get {
            return false;
        }
    }

}