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;

namespace HySoft.BaseCallCenter.Web.workordermanage._controls
{
    public partial class ctl_workorderhistrory : System.Web.UI.UserControl
    {
        public int i = 0;
        public bool b = false;
        public bool b1 = false;
        public bool b2 = false;
        public string state = "-1";
        public string state1 = "-1";
        public string state2 = "-1";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string GetTimeInfo(string times)
        {
            string res = "";
            try
            {
                int t = Convert.ToInt32(times);
                if (t < 60)
                {
                    res = "" + t + "秒";
                }
                else if (t < 3600)
                {
                    res = "" + Convert.ToInt32(t / 60).ToString() + "分" + "" + Convert.ToInt32(t % 60).ToString() + "秒";
                }
                else if (t < 86400)
                {
                    res = "" + Convert.ToInt32(t / 3600).ToString() + "时" + "" + Convert.ToInt32((t % 3600) / 60).ToString() + "分" + "" + Convert.ToInt32((t % 3600) % 60).ToString() + "秒";
                }
                else
                {
                    res = "" + Convert.ToInt32(t / 86400).ToString() + "天" + "" + Convert.ToInt32((t % 86400) / 3600).ToString() + "时" + "" + Convert.ToInt32(((t % 86400) % 3600) / 60).ToString() + "分" + "" + Convert.ToInt32(((t % 86400) % 3600) % 60).ToString() + "秒";
                }
            }
            catch
            { }
            return res;
        }

        public int GetWorkorderNoteHeight(string state)
        {
            /*
                0	结束
                4	待指派
                5	待预约
                6	待接单
                7	待处理
                8	待回访
                9	待结单
                12	开始
             */
            int height = 100;

            switch (Convert.ToInt32(state))
            {
                case 4:
                    height = 230;
                    break;
                case 6:
                    height = 200;
                    break;
                case 7:
                    height = 350;
                    break;
                case 8:
                    height = 230;
                    break;
                case 9:
                    height = 230;
                    break;
                case 12:
                    height = 390;
                    break;
                case 13:
                    height = 380;
                    break;
            }

            return height;
        }

        public string GetWorkorderNoteHtml(string state, string id, string instanceid)
        {
            StringBuilder res = new StringBuilder();
            DataTable dt = new DataTable();
            try
            {
                string ctrl = "~/workordermanage/_controls/ctl_workordernote.ascx";
                Control uc_rep = LoadControl(ctrl);
                Repeater rptItems = (Repeater)uc_rep.FindControl("rptitems_" + state);
                switch (state)
                {
                    case "12":
                        //DataTable dt1 = new BLL.T_Wo_WorkOrderHistory().GetList("F_INSTANCEID=" + instanceid.Trim() + "").Tables[0];
                        //if (dt1.Rows[0]["F_OPTBTNID"].ToString() == "18") { b = true; }
                        //if (dt1.Rows[0]["F_OPTBTNID"].ToString() == "19") { b1 = true; state1 = "12"; }
                        //dt = new BLL.T_Wo_WorkOrderBase().GetList("F_INSTANCEID=" + instanceid.Trim() + "").Tables[0];
                        dt = new BLL.T_Wo_WorkOrderHistory().GetList("F_HISTORYID=" + id.Trim() + "").Tables[0];
                        if (dt.Rows[0]["F_OPTBTNID"].ToString() == "19") { b1 = true; state1 = "12"; }
                        if (dt.Rows[0]["F_OPTBTNID"].ToString() == "18") { b2 = true; state2 = "18"; }
                        break;
                    case "4":
                    case "6":
                    case "13":
                        dt = new BLL.T_Wo_WorkOrderHistory().GetList("F_HISTORYID=" + id.Trim() + "").Tables[0];
                        break;
                    default:
                        dt = new BLL.T_Wo_WorkOrderHistoryInfo().GetList("F_HISTORYID=" + id.Trim() + "").Tables[0];
                        break;
                }
                if (dt != null)
                {
                    rptItems.DataSource = dt;
                    rptItems.DataBind();
                    StringWriter sw = new StringWriter(res);
                    HtmlTextWriter htw = new HtmlTextWriter(sw);

                    rptItems.RenderControl(htw);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
            return res.ToString();
        }

        protected string getUserName(object instanceid)
        {
            string strUserName = "";
            DataTable workorderbase =
                new BLL.T_Wo_WorkOrderBase().GetList("F_INSTANCEID=" + instanceid).Tables[0];
            if (workorderbase.Rows.Count > 0)
            {
                List<Model.T_Wo_WorkOrderConstructor> constructorlist =
                    new BLL.T_Wo_WorkOrderConstructor().GetModelList("F_WORKORDERID=" + workorderbase.Rows[0]["F_WORKORDERID"]);
                if (constructorlist.Count > 0)
                {
                    foreach (Model.T_Wo_WorkOrderConstructor constructor in constructorlist)
                    {
                        int id = Convert.ToInt32(constructor.F_USERID);
                        Model.T_Sys_UserAccount user = new BLL.T_Sys_UserAccount().GetModel(id);
                        if (user != null)
                        {
                            if (constructor.F_ISMAIN == 1)
                                strUserName += user.F_UserName + "(主负责人);";
                            else
                                strUserName += user.F_UserName + ";";
                        }
                    }
                }
            }
            return strUserName.TrimEnd(';');
        }
        protected string getUName(object instanceid)
        {
            string strUserName = "";
            DataTable task =
                new BLL.T_Wo_WorkOrderTask().GetList("F_INSTANCEID=" + instanceid + " AND F_WORKORDERSTATEID=13").Tables[0];
            if (task.Rows.Count > 0)
            {
                if (task.Rows[0]["F_OWNERID"] != null)
                {
                    DataTable user =
                        new BLL.T_Sys_UserAccount().GetList("F_UserId=" + task.Rows[0]["F_OWNERID"]).Tables[0];
                    if (user.Rows.Count > 0)
                        if (user.Rows[0]["F_UserName"] != null)
                            strUserName = user.Rows[0]["F_UserName"].ToString();
                }
            }
            return strUserName;
        }
    }
}