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

using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Xml;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
public class ShopService : System.Web.UI.Page, IHttpHandler, IReadOnlySessionState
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain"; 
        string action = context.Request.Params["Action"];
        switch (action)
        {
            case "GetProductMessage":
                GetProductMessage(context);
                break;
            case "ShowOrderItem":
                ShowOrderItem(context);
                break;
            case "GetRequestMessage":
                GetRequestMessage(context);
                break;
            case "GetUnReadMsg":
                GetMessageCount(context);
                break;
            case "CheckLogin":
                CheckUserLogin(context);
                break;
                
                
        }
        context.Response.Flush();
        context.Response.Close();
    }

    private void GetMessageCount(HttpContext context)
    {
        MessageBLL msg = new MessageBLL();
        string m_UserName = context.Request.Params["UserName"];
        int unreadMsg = 0;
        if(m_UserName!=null && m_UserName!="")
            unreadMsg=msg.GetUnreadMsgByUserName(m_UserName);
        context.Response.Write("("+unreadMsg.ToString()+")");
        

    }

    private void GetRequestMessage(HttpContext context)
    {
        int RequestID = Convert.ToInt32(context.Request.Params["ProductID"]);
        Shop_Message message = new Shop_Message();
        DataTable dt = message.GetMessageByRequestID(RequestID);
        int rows = dt.Rows.Count;
        string str = "暂无留言！";
        string ReplayUrl = "";
        if (rows > 0)
        {
            if (rows > 10)
                rows = 10;
            str = "<dl>";
            string m_SalerID = dt.Rows[0]["M_ReceiverID"].ToString();
            for (int i = 0; i < rows; i++)
            {
                ReplayUrl = "";
                string replayText = dt.Rows[i]["R_Content"].ToString();
                
                //如果登录的人和卖家ID相同则显示所有的留言,否则只显示已经回复过的留言.
                if (Session["Howave_UserID"] != null && Session["Howave_UserID"].ToString() == m_SalerID)
                {
                    if (replayText == "")
                    {
                        ReplayUrl = "&nbsp;&nbsp;<a href='Replay_Product_Msg.aspx?msg_id=" + dt.Rows[i]["M_ID"].ToString() + "&requester_name=" + Server.UrlEncode(dt.Rows[i]["M_SenderName"].ToString()) + "&request_date=" + Server.UrlEncode(dt.Rows[i]["M_DateTime"].ToString()) + "&request_text=" + Server.UrlEncode(dt.Rows[i]["M_Content"].ToString()) + "&msgType=Request&RequestID=" + dt.Rows[i]["M_ProductID"].ToString() + "' target='_blank'>回复此留言</a>";
                    }

                }

                //显示问题
                str += "<dt><div>" + dt.Rows[i]["M_SenderName"].ToString() + "：" + dt.Rows[i]["M_Content"].ToString() + ReplayUrl + "</div><b>" + dt.Rows[i]["M_DateTime"].ToString() + "</b></dt>";
                //显示回复
                if (dt.Rows[i]["R_Content"].ToString() != "")
                    str += "<dd><div>" + dt.Rows[i]["M_ReceiverName"].ToString() + "：" + dt.Rows[i]["R_Content"].ToString() + "</div><b>" + dt.Rows[i]["R_DateTime"].ToString() + "</b></dd>";

            }
            str += "</dl>";
           
        }
        context.Response.Write(str);
    }

    private void GetProductMessage(HttpContext context)
    {
        int productID = Convert.ToInt32(context.Request.Params["ProductID"]);       
        Shop_Message message = new Shop_Message();
        DataTable dt = message.GetMessageByProductID(productID);
        int rows = dt.Rows.Count;     
        bool IsReplay = true;        
        string str = "暂无留言！";
        if (rows > 0)
        {
            string m_SalerID = dt.Rows[0]["M_ReceiverID"].ToString();
            str = "<dl>";
            if (rows > 10)
                rows = 10;
            
            for (int i = 0; i < rows; i++)
            {
                string ReplayUrl = "";
                string replayText = dt.Rows[i]["R_Content"].ToString();
                if (replayText == "")
                {
                    IsReplay = false;
                    ReplayUrl = "&nbsp;&nbsp;<a href='Replay_Product_Msg.aspx?msg_id=" + dt.Rows[i]["M_ID"].ToString() + "&requester_name=" + Server.UrlEncode(dt.Rows[i]["M_SenderName"].ToString()) + "&request_date=" + Server.UrlEncode(dt.Rows[i]["M_DateTime"].ToString()) + "&request_text=" + Server.UrlEncode(dt.Rows[i]["M_Content"].ToString()) + "&ProductID=" + dt.Rows[i]["M_ProductID"].ToString() + "' target='_blank'>回复此留言</a>";
                }             
               
                //如果登录的人和卖家ID相同则显示所有的留言,否则只显示已经回复过的留言.
                if (Session["Howave_UserID"] != null && Session["Howave_UserID"].ToString() == m_SalerID)
                {
                    IsReplay = true;
                }
                
                if (IsReplay)
                {
                    //显示问题
                    str += "<dt><div>" + dt.Rows[i]["M_SenderName"].ToString() + "：" + dt.Rows[i]["M_Content"].ToString() + ReplayUrl + "</div><b>" + dt.Rows[i]["M_DateTime"].ToString() + "</b></dt>";
                    //显示回复
                    if (dt.Rows[i]["R_Content"].ToString() != "")
                        str += "<dd><div>" + dt.Rows[i]["M_ReceiverName"].ToString() + "：" + dt.Rows[i]["R_Content"].ToString() + "</div><b>" + dt.Rows[i]["R_DateTime"].ToString() + "</b></dd>";

                }
                else
                {
                    if (dt.Rows[i]["R_Content"].ToString() != "")
                        str += "<dd><div>" + dt.Rows[i]["ReceiverName"].ToString() + "：" + dt.Rows[i]["R_Content"].ToString() + "</div><b>" + dt.Rows[i]["R_DateTime"].ToString() + "</b></dd>";
                }

                    

            }
            str += "</dl>";
           
        }        
        context.Response.Write(str);

    }

    public void ShowOrderItem(HttpContext context)
    {      
        string m_type = context.Request["type"];
        switch (m_type)
        {
            case "bank":
                ShowBankRollItemByOrderFormID(context);
                break;
            case "deliver":
                ShowDeliverItemByOrderFormID(context);
                break;
            case "invoice":
                ShowInvoicedItemByOrderFormID(context);
                break;

        }

    }

    private void ShowBankRollItemByOrderFormID(HttpContext context)
    {
        int OrderFormID = Convert.ToInt32(context.Request["OrderFormID"]);
        Shop_BankrollItem _sbk = new Shop_BankrollItem();
        DataTable dt = _sbk.GetBanKrollItemByOrderFormID(OrderFormID);
        StringBuilder sb = new StringBuilder();
        if (dt.Rows.Count > 0)
        {
            sb.Append("<table class=\"tableHead\" ><tr><th>交易时间</th><th>交易方式</th><th>状态</th><th>金额</th><th>备注/说明</th></tr>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string m_payType = "汇款";
                switch (dt.Rows[i]["MoneyType"].ToString())
                {
                    case "0":
                        m_payType = "现金";
                        break;
                    case "1":
                        m_payType = "银行汇款";
                        break;
                    case "2":
                        m_payType = "在线支付";
                        break;
                    case "3":
                        m_payType = "虚拟货币";
                        break;
                    case "4":
                        m_payType = "邮寄汇款";
                        break;
                    case "5":
                        m_payType = "上门收款";
                        break;
                    case "6":
                        m_payType = "支票";
                        break;                    

                }
                sb.Append("<tr><td>" + dt.Rows[i]["DateAndTime"].ToString() + "</td><td>" + m_payType + "</td><td>" + dt.Rows[i]["Status"].ToString() + "</td><td>" + dt.Rows[i]["Money"].ToString() + "</td><td>" + dt.Rows[i]["Remark"].ToString() + "</td></tr>");
                
            }
            sb.Append("</table>");
            

        }
        else
            sb.Append("<br>暂无记录");
        context.Response.Write(sb.ToString());

    }
    private void ShowDeliverItemByOrderFormID(HttpContext context)
    {
        int OrderFormID = Convert.ToInt32(context.Request["OrderFormID"]);
        Shop_DeliverItem _sd = new Shop_DeliverItem();
        DataTable dt = _sd.GetDeliverItemByOrderFormID(OrderFormID);
        StringBuilder sb = new StringBuilder();
        if (dt.Rows.Count > 0)
        {           
            sb.Append("<table class=\"tableHead\" ><tr><th>日期</th><th>方向</th><th>快递公司</th><th>快递号</th><th>客户签收</th><th>经手人</th><th>备注</th></tr>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string m_type = (dt.Rows[i]["DeliverType"].ToString() == "0") ? "发货" : "退货";
                string m_receive = (dt.Rows[i]["Received"].ToString() == "False") ? "未签收" : "已签收";
                sb.Append("<tr><td>" + dt.Rows[i]["DeliverDate"].ToString() + "</td><td>" + m_type + "</td><td>" + dt.Rows[i]["DeliverCompany"].ToString() + "</td><td>" + dt.Rows[i]["ExpressNumber"].ToString() + "</td><td>" + m_receive + "</td><td>" + dt.Rows[i]["HandlerName"].ToString() + "</td><td>" + dt.Rows[i]["Remark"].ToString() + "</td></tr>");

            }
            sb.Append("</table>");
           

        }
        else
            sb.Append("<br>暂无记录");
        context.Response.Write(sb.ToString());

    }
    private void ShowInvoicedItemByOrderFormID(HttpContext context)
    {
        int OrderFormID = Convert.ToInt32(context.Request["OrderFormID"]);
        Shop_InvoiceItem _sd = new Shop_InvoiceItem();
        DataTable dt = _sd.GetInvoiceByOrderFormID(OrderFormID);
        StringBuilder sb = new StringBuilder();
        if (dt.Rows.Count > 0)
        {
            sb.Append("<table class=\"tableHead\"><tr><th>金额</th><th>类型</th><th>发票号</th><th>抬头</th><th>开票人</th><th>录入员</th><th>开票时间</th></tr>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string m_type = dt.Rows[i]["InvoiceType"].ToString();
                switch (m_type)
                {
                    case "0":
                        m_type = "地税普通发票";
                        break;
                    case "1":
                        m_type = "国税普通发票";
                        break;
                    case "2":
                        m_type = "增值税发票";
                        break;

                }
                sb.Append("<tr><td>" + dt.Rows[i]["TotalMoney"].ToString() + "</td><td>" + m_type + "</td><td>" + dt.Rows[i]["InvoiceNum"].ToString() + "</td><td>" + dt.Rows[i]["Invoicetitle"].ToString() + "</td><td>" + dt.Rows[i]["Drawer"].ToString() + "</td><td>" + dt.Rows[i]["Inputer"].ToString() + "</td><td>" + dt.Rows[i]["InputTime"].ToString() + "</td></tr>");

            }
            sb.Append("</table>");

        }
        else
            sb.Append("<br>暂无记录");
        context.Response.Write(sb.ToString());

    }

    protected void CheckUserLogin(HttpContext context)
    {
        string result = "0";
        if (Session["Howave_UserID"] == null || Session["Howave_UserID"].ToString() == "" || Session["Howave_UserID"].ToString() == "0")
        {
            result = "0";
        }
        else
        {
            string m_UserName = context.Request["UserName"];
            string m_UserPassword = context.Request["Password"];
            UserBLL user = new UserBLL();
            DataTable dt = user.GetUserByUserName(m_UserName);
            if (dt.Rows.Count == 1)
            {
                if (dt.Rows[0]["Password"].ToString().ToLower() == m_UserPassword.ToLower() && dt.Rows[0]["UserID"].ToString() == Session["Howave_UserID"].ToString())
                {
                    result = "1";

                }
            }
            result = "1";

        }
        context.Response.Write(result);
       
       

    }

}