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 HySoft.DBUtility;
using System.Text;

namespace HySoft.BaseCallCenter.Web.workordermanage.workorder
{
    public partial class workorderdetail : System.Web.UI.Page
    {
        protected string printstring = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["workorderid"] != null)
                {
                    string workorderid = Request.QueryString["workorderid"];//工单编号
                    printstring = GetHistoryList(workorderid);
                }
            }
        }
        private string GetHistoryList(string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            string sql = "SELECT * FROM dbo.V_WorkOrderHistory INNER JOIN dbo.V_WorkOrderBase ON dbo.V_WorkOrderHistory.F_INSTANCEID = dbo.V_WorkOrderBase.F_WORKORDERID WHERE dbo.V_WorkOrderHistory.F_INSTANCEID=" + workorderid + "";
            //string sql = "SELECT * FROM dbo.T_Wo_WorkOrderHistory INNER JOIN dbo.T_Wo_WorkOrderBase ON dbo.T_Wo_WorkOrderHistory.F_INSTANCEID = dbo.T_Wo_WorkOrderBase.F_WORKORDERID WHERE dbo.T_Wo_WorkOrderHistory.F_INSTANCEID=" + workorderid + "";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                #region 输出表格
                for (int n = 0; n < rowsCount; n++)
                {
                    //开始
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "12")
                    {
                        sb.Append(PrintBaoXiu(dt, n, workorderid));
                    }
                    //待指派信息
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "4")
                    {
                        sb.Append(PrintZhiPai(dt, n, workorderid));
                    }
                    //待接单信息
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "6")
                    {
                        sb.Append(PrintJieShou(dt, n, workorderid));
                    }
                    //待处理信息
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "7")
                    {
                        sb.Append(PrintChuLi(dt, n, workorderid));
                    }
                    //待回访信息
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "8")
                    {
                        sb.Append(PrintHuiFang(dt, n, workorderid));
                    }
                    //待结单信息
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "9")
                    {
                        sb.Append(PrintJieDan(dt, n, workorderid));
                    }
                    //结束
                    if (dt.Rows[n]["F_WORKORDERSTATEID"].ToString() == "0")
                    {
                        sb.Append(PrintJieShu(dt, n, workorderid));
                    }
                }
                #endregion
            }
            return sb.ToString();
        }

        private string PrintBaoXiu(string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            string sql = "select * from V_WorkOrderBase where F_WORKORDERID=" + workorderid + "";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            List<Model.T_Wo_WorkOrderBase> baseModel = new BLL.T_Wo_WorkOrderBase().DataTableToList(dt);
            if (baseModel.Count > 0)
            {
                sb.Append("<table class=\"form_table\">");
                sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">工单报修信息</th></tr>");
                if (baseModel[0].F_CUSTOMERNAME == null) baseModel[0].F_CUSTOMERNAME = "";
                if (baseModel[0].F_CustomerNature == null) baseModel[0].F_CustomerNature = "";
                sb.AppendFormat("<tr><th>客户名称：</th><td>{0}</td><th>客户属性：</th><td>{1}</td></tr>", baseModel[0].F_CUSTOMERNAME.ToString(), baseModel[0].F_CustomerNature.ToString());
                if (baseModel[0].F_CUSTOMERID == null) baseModel[0].F_CUSTOMERID = -1;
                if (baseModel[0].F_ChargeDept == null) baseModel[0].F_ChargeDept = "";
                sb.AppendFormat("<tr><th>客户编号：</th><td>{0}</td><th>所属客服部：</th><td>{1}</td></tr>", baseModel[0].F_CUSTOMERID.ToString(), baseModel[0].F_ChargeDept.ToString());
                if (baseModel[0].F_CustomerClass == null) baseModel[0].F_CustomerClass = "";
                if (baseModel[0].F_AfterSaleName == null) baseModel[0].F_AfterSaleName = "";
                sb.AppendFormat("<tr><th>客户等级：</th><td>{0}</td><th>售后负责人：</th><td>{1}</td></tr>", baseModel[0].F_CustomerClass.ToString(), baseModel[0].F_AfterSaleName.ToString());
                //if (baseModel[0].F_SystemStartTime == null) baseModel[0].F_SystemStartTime.ToString() = "";
                //if (baseModel[0].F_QualityGuaranteeEndTime == null) baseModel[0].F_QualityGuaranteeEndTime = "";
                sb.AppendFormat("<tr><th>系统开通时间：</th><td>{0}</td><th>工单开始时间：</th><td>{1}</td></tr>", baseModel[0].F_SystemStartTime.ToString(), baseModel[0].F_STARTTIME.ToString());
                if (baseModel[0].F_REPAIRMANNAME == null) baseModel[0].F_REPAIRMANNAME = "";
                if (baseModel[0].F_REPAIRMANPHONE == null) baseModel[0].F_REPAIRMANPHONE = "";
                sb.AppendFormat("<tr><th>来电客户姓名：</th><td>{0}</td><th>来电客户联系方式：</th><td>{1}</td></tr>", baseModel[0].F_REPAIRMANNAME.ToString(), baseModel[0].F_REPAIRMANPHONE.ToString());
                if (baseModel[0].F_CONTENT == null) baseModel[0].F_CONTENT = "";
                sb.AppendFormat("<tr><th>来电内容描述：</th><td colspan=\"3\">{0}</td></tr>", baseModel[0].F_CONTENT.ToString());
                sb.Append("</table>");
            }
            return sb.ToString();
        }
        private string PrintBaoXiu(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"form_table\">");
            sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">工单信息</th></tr>");
            sb.AppendFormat("<tr><th>客户名称：</th><td>{0}</td><th>客户属性：</th><td>{1}</td></tr>", dt.Rows[n]["F_CUSTOMERNAME"].ToString(), dt.Rows[n]["F_CustomerNature"].ToString());
            sb.AppendFormat("<tr><th>系统类型：</th><td>{0}</td><th>资料完善状态：</th><td></td></tr>", dt.Rows[n]["F_SystemType"].ToString());
            sb.AppendFormat("<tr><th>系统开通时间：</th><td>{0}</td><th>报修时间：</th><td>{1}</td></tr>", dt.Rows[n]["F_SystemStartTime"].ToString(), dt.Rows[n]["F_STARTTIME"].ToString());
            sb.AppendFormat("<tr><th>报修人：</th><td>{0}</td><th>报修人联系方式：</th><td>{1}</td></tr>", dt.Rows[n]["F_REPAIRMANNAME"].ToString(), dt.Rows[n]["F_REPAIRMANPHONE"].ToString());
            sb.AppendFormat("<tr><th>报修级别：</th><td colspan=\"3\">{0}</td></tr>", dt.Rows[n]["F_REPAIRLEVELNAME"].ToString());
            sb.AppendFormat("<tr><th>报修内容描述：</th><td colspan=\"3\">{0}</td></tr>", dt.Rows[n]["F_CONTENT"].ToString());
            sb.AppendFormat("<tr><th>报修要求：</th><td colspan=\"3\">{0}</td></tr>", dt.Rows[n]["F_REPAIRREQUEST"].ToString());
            sb.Append("</table>");
            return sb.ToString();
        }
        private string PrintZhiPai(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                sb.Append("<table class=\"form_table\">");
                sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">指派信息</th></tr>");
                sb.AppendFormat("<tr><th>报修记录编号：</th><td>{0}</td><th>派工记录编号：</th><td>{1}</td></tr>", dt.Rows[n]["F_CODE"].ToString(), dt.Rows[n]["F_HISTORYID"].ToString());
                sb.AppendFormat("<tr><th>派工时间：</th><td>{0}</td><th>要求服务时间：</th><td>{1}</td></tr>", dt.Rows[0]["F_OPTDATE"].ToString(), dt.Rows[0]["F_REQUESTSERVICETIME"].ToString());
                sb.AppendFormat("<tr><th>预计解决用时(不含乘车)：</th><td>{0}</td><th>接单人：</th><td>{1}</td></tr>", dt.Rows[0]["F_ELAPSEDTIME"].ToString(), dt.Rows[0]["F_USERNAME"].ToString());
                sb.AppendFormat("<tr><th>派单备注：</th><td colspan=\"3\">{0}</td></tr>", dt.Rows[0]["F_REMARK"].ToString());
                sb.AppendFormat("<tr><th>服务人员工号：</th><td>{0}</td><th>服务人员部门：</th><td>{1}</td></tr>", dt.Rows[0]["F_EMPCODE"].ToString(), dt.Rows[0]["F_DEPTCODE"].ToString());
                sb.AppendFormat("<tr><th>服务人员电话：</th><td>{0}</td><th>派工超期天数：</th><td>{1}</td></tr>", dt.Rows[0]["F_USERPHONE"].ToString(), dt.Rows[0]["F_OVERTIMES"].ToString());
                sb.AppendFormat("<tr><th>工单状态：</th><td>{0}</td><th>派工超期状态：</th><td></td></tr>", dt.Rows[0]["F_Name"].ToString());
                sb.Append("</table>");
            }
            return sb.ToString();
        }
        private string PrintJieShou(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"form_table\">");
            sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">接收信息</th></tr>");
            sb.AppendFormat("<tr><th>接收人：</th><td></td><th>接收时间：</th><td></td></tr>", dt.Rows[0]["F_EMPCODE"].ToString(), dt.Rows[0]["F_Name"].ToString());
            sb.AppendFormat("<tr><th>接收人电话：</th><td>{0}</td></tr>",dt.Rows[0]["F_Name"].ToString());
            sb.Append("</table>");
            return sb.ToString();
        }
        private string PrintChuLi(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"form_table\">");
            sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">处理信息</th></tr>");
            sb.AppendFormat("<tr><th>纸质工单编号：</th><td></td><th>服务类型：</th><td>{0}</td></tr>", dt.Rows[n]["F_SERVICETYPE"].ToString());
            sb.AppendFormat("<tr><th>服务开始时间：</th><td></td><th>服务结束时间：</th><td></td></tr>", dt.Rows[n]["F_OPTDATE"].ToString());
            sb.AppendFormat("<tr><th>申报工时：</th><td>{0}</td><th>乘车时间：</th><td></td></tr>", dt.Rows[n]["F_DECLARATIONTIME"].ToString());
            sb.AppendFormat("<tr><th>服务性质：</th><td></td><th>服务方式：</th><td></td></tr>", dt.Rows[n]["F_SERVICENATURE"].ToString(), dt.Rows[n]["F_SERVICEWAY"].ToString());
            sb.AppendFormat("<tr><th>服务内容及结果：</th><td colspan=\"3\"></td></tr>", dt.Rows[n]["F_REMARK"].ToString());
            sb.AppendFormat("<tr><th>回访建议：</th><td></td><th>结单时间：</th><td></td></tr>", dt.Rows[n]["F_RETURNVISITFLAG"].ToString(), dt.Rows[n]["F_ENDTIME"].ToString());
            sb.AppendFormat("<tr><th>结单情况：</th><td colspan=\"3\"></td></tr>");
            sb.Append("</table>");
            return sb.ToString();
        }
        private string PrintHuiFang(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"form_table\">");
            sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">回访信息</th></tr>");
            sb.AppendFormat("<tr><th>回访人：</th><td>{0}</td><th>回访时间：</th><td>{1}</td></tr>", dt.Rows[n]["F_RETURNVISITMAN"].ToString(), dt.Rows[n]["F_RETURNVISITTIME"].ToString());
            sb.AppendFormat("<tr><th>客户评分：</th><td>{0}</td><th>客户意见类别：</th><td>{1}</td></tr>", dt.Rows[n]["F_RETURNVISITRESULT"].ToString(), dt.Rows[n]["F_RETURNVISITOPINION"].ToString());
            sb.AppendFormat("<tr><th>回访类型：</th><td>{0}</td><th>问题解决情况：</th><td>{1}</td></tr>", dt.Rows[n]["F_SERVICETYPE"].ToString(), dt.Rows[n]["F_RETURNVISITPROBLEM"].ToString());
            sb.AppendFormat("<tr><th>回访备注：</th><td colspan=\"3\">{0}</td></tr>", dt.Rows[n]["F_RETURNVISITCONTENT"].ToString());
            sb.Append("</table>");
            return sb.ToString();
        }
        private string PrintJieDan(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"form_table\">");
            sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">审核信息</th></tr>");
            sb.AppendFormat("<tr><th>工单审核人：</th><td></td><th>工单审核时间：</th><td></td></tr>",dt.Rows[n]["F_RETURNVISITMAN"].ToString(),dt.Rows[n]["F_RETURNVISITMAN"].ToString());
            sb.AppendFormat("<tr><th>服务持续时间(时)：</th><td>{0}</td><th>超期时长：</th><td></td></tr>", dt.Rows[n]["F_ELAPSEDTIME"].ToString(), dt.Rows[n]["F_OVERTIMES"].ToString());
            sb.AppendFormat("<tr><th>审核工时：</th><td></td><th>服务等级：</th><td></td></tr>", dt.Rows[n]["F_RETURNVISITMAN"].ToString());
            sb.AppendFormat("<tr><th>质保结束时间：</th><td>{0}</td><th>维保类别：</th><td></td></tr>", dt.Rows[n]["F_QualityGuaranteeEndTime"].ToString());
            sb.AppendFormat("<tr><th>工单问题类型：</th><td></td><th>审核意见类别：</th><td></td></tr>", dt.Rows[n]["F_RETURNVISITMAN"].ToString());
            sb.AppendFormat("<tr><th>审核备注：</th><td colspan=\"3\"></td></tr>", dt.Rows[n]["F_REMARK"].ToString());
            sb.Append("</table>");
            return sb.ToString();
        }
        private string PrintJieShu(DataTable dt, int n, string workorderid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"form_table\">");
            sb.Append("<tr><th colspan=\"4\" style=\"text-align:left\">工单结束信息</th></tr>");
            sb.AppendFormat("<tr><th>工单开始时间：</th><td></td><th>工单结束时间：</th><td></td></tr>", dt.Rows[n]["F_STARTTIME"].ToString(), dt.Rows[n]["F_ENDTIME"].ToString());
            sb.AppendFormat("<tr><th>工单编号：</th><td>{0}</td></tr>", dt.Rows[n]["F_CODE"].ToString());
            sb.Append("</table>");
            return sb.ToString();

        }
    }
}