using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.IO;
using HySoft.DBUtility;

namespace HySoft.BaseCallCenter.Web.workordermanage.workorder
{
    public partial class workordershow : System.Web.UI.Page
    {
        public string workorderid = "0";
        public string workintanceid = "0";
        public string html = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    workorderid = Request.QueryString["id"].Trim();

                    Model.T_Wo_WorkOrderBase order = new BLL.T_Wo_WorkOrderBase().GetModel(Convert.ToInt32(workorderid));
                    if (order != null)
                    {
                        workintanceid = order.F_INSTANCEID.ToString();
                        //绑定工单客户信息
                        BindCustomerInfo(order.F_CUSTOMERID.ToString());
                        //绑定工单基本信息
                        BindWorkOrderBaseInfo(workintanceid);
                        html = GetFlowList();
                    }
                }
            }
        }

        private void BindCustomerInfo(string customerid)
        {
            //绑定工单客户信息
            int cid = Convert.ToInt32(customerid);
            if (cid > 0)
            {
                Model.T_Cus_CustomerBase tccb = new BLL.T_Cus_CustomerBase().GetModel(cid);
                if (tccb != null)
                {
                    if (tccb.F_CustomerName != null)
                        txtCustomerName.Text = tccb.F_CustomerName.ToString();
                    //if (tccb.F_CustomerNature != null)
                    //    txtCustomerNature.Text = tccb.F_CustomerNature.ToString();
                    if (tccb.F_CustomerCode != null)
                        txtCustomerCode.Text = tccb.F_CustomerCode.ToString();
                    ////if (tccb.F_ServiceDept != null)
                    ////    txtServiceDept.Text = tccb.F_ServiceDept.ToString();
                    ////if (tccb.F_CustomerClass != null)
                    ////    txtCustomerClass.Text = tccb.F_CustomerClass.ToString();
                    ////if (tccb.F_AfterSaleName != null)
                    ////    txtAfterSaleName.Text = tccb.F_AfterSaleName.ToString();
                    ////if (tccb.F_QualityGuaranteeEndTime != null)
                    ////    txtQualityGuaranteeEndTime.Text = tccb.F_QualityGuaranteeEndTime.ToString();
                    if (tccb.F_Province != null)
                        txtProvince.Text = tccb.F_Province.ToString();
                    if (tccb.F_City != null)
                        txtProvince.Text += "-" + tccb.F_City.ToString();
                    //if (tccb.F_SystemType != null)
                    //    txtSystemType.Text = tccb.F_SystemType.ToString();
                }
            }
        }

        private void BindWorkOrderBaseInfo(string instanceid)
        {
            DataTable dt = DbHelperSQL.Query("select * from V_WorkOrderBase where F_INSTANCEID=" + instanceid).Tables[0];
            Model.T_Wo_WorkOrderBase order = new BLL.T_Wo_WorkOrderBase().DataTableToList(dt)[0];
            if (order != null)
            {
                if (order.F_WORKORDERLEVELID != null)
                {
                    DataTable dt1 = new BLL.T_Sys_DictionaryValue().GetList("F_DictionaryValueId='" + order.F_WORKORDERLEVELID + "'").Tables[0];
                    if (dt1.Rows.Count > 0)
                    {
                        txtWorkOrderType.Text = dt1.Rows[0]["F_Name"].ToString();
                    }
                }
                if (order.F_CODE != null)
                    txtWorkOrderNum.Text = order.F_CODE;
                if (order.F_TypeName != null)
                    txtCallInType.Text = order.F_TypeName;
                if (order.F_CREATEDATE != null)
                    txtCreateTime.Text = order.F_CREATEDATE.ToString();
                if (order.F_Name != null)
                    txtWorkOrderState.Text = order.F_Name;
                //if (order.F_REPAIRLEVELNAME != null)
                //    txtWorkOrderClass.Text = order.F_REPAIRLEVELNAME;
                if (order.F_REPAIRMANNAME != null)
                    txtCallInName.Text = order.F_REPAIRMANNAME;
                if (order.F_REPAIRMANPHONE != null)
                    txtCallInPhone.Text = order.F_REPAIRMANPHONE;
                if (order.F_USERNAME != null)
                    txtCreateName.Text = order.F_USERNAME;
            }
        }

        #region 根据工单流节点输出HTML

        private string GetNoteHtml(int historyid)
        {
            StringBuilder res = new StringBuilder();
            return res.ToString();
        }

        #endregion

        #region 拼接工作流程列表

        /// <summary>
        /// 拼接工作流程列表
        /// </summary>
        /// <returns></returns>
        protected string GetFlowList()
        {
            StringBuilder res = new StringBuilder();
            DataTable dt=new DataTable();
            try
            {
                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_INSTANCEID "
                    + "INNER JOIN dbo.T_Wo_WorkOrderHistoryInfo ON dbo.T_Wo_WorkOrderHistory.F_HISTORYID = dbo.T_Wo_WorkOrderHistoryInfo.F_HISTORYID "
                    + "WHERE F_INSTANCEID=" + workintanceid.Trim();
                dt = new BLL.T_Wo_WorkOrderHistory().GetListView("F_INSTANCEID=" + workintanceid.Trim() + " ORDER BY F_OPTDATE ASC").Tables[0];
                //dt = DbHelperSQL.Query(sql).Tables[0];
                if (dt != null)
                {
                    hfFlowCount.Value = dt.Rows.Count.ToString();
                    string ctrl = "~/workordermanage/_controls/ctl_workorderhistrory.ascx";

                    Control uc_rep = LoadControl(ctrl);
                    Repeater rptItems = (Repeater)uc_rep.FindControl("rptitems");
                    rptItems.DataSource = dt;
                    rptItems.DataBind();
                    StringWriter sw = new StringWriter(res);
                    HtmlTextWriter htw = new HtmlTextWriter(sw);

                    rptItems.RenderControl(htw);

                    #region
                    //for (int i = 0; i < dt.Rows.Count; i++)
                    //{
                    //    res.Append("<div id=\"divOrderFlow" + i.ToString() + "\">");

                    //    res.Append("<input id=\"hfFlowTitle" + i.ToString() + "\" value=\"" + dt.Rows[i]["F_StateName"].ToString().Trim() + "：" + dt.Rows[i]["F_UserName"].ToString().Trim() + "-" + dt.Rows[i]["F_Name"].ToString().Trim() + "\" style=\"display: none;\" />");

                    //    int height = 100;

                    //    res.Append("<table class=\"form_table\" style=\"width: 100%;\">");
                    //    res.Append("<colgroup><col width=\"100\" /><col /><col width=\"100\" /><col /><col width=\"100\" /><col /></colgroup>");


                    //    res.Append("<tr style=\"height:25px;\">");
                    //    res.Append("<th style=\"height:25px; padding:0px 8px 0px 0px;\">操作时间：</th>");
                    //    res.Append("<td style=\"height:25px; padding:2px 0px 0px 8px;\">&nbsp;" + dt.Rows[i]["F_OPTDATE"].ToString().Trim() + "</td>");
                    //    res.Append("<th style=\"height:25px; padding:0px 8px 0px 0px;\">操作人：</th>");
                    //    res.Append("<td style=\"height:25px; padding:2px 0px 0px 8px;\">&nbsp;" + dt.Rows[i]["F_UserName"].ToString().Trim() + "</td>");
                    //    res.Append("<th style=\"height:25px; padding:0px 8px 0px 0px;\">耗时：</th>");
                    //    res.Append("<td style=\"height:25px; padding:2px 0px 0px 8px;\">&nbsp;" + dt.Rows[i]["F_ELAPSEDTIME"].ToString().Trim() + "</td>");
                    //    res.Append("</tr>");

                    //    res.Append("</table>");
                    //    res.Append("<input id=\"hfFlowHeight" + i.ToString() + "\" value=\"" + height.ToString() + "\" style=\"display: none;\" />");
                    //    res.Append("</div>");
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
            return res.ToString();
        }
        #endregion
    }
}