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.Common;

namespace HySoft.BaseCallCenter.Web.calloutmanage
{
    public partial class calloutoptshow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region 绑定呼叫结果
                InitHJJG();
                #endregion
                try
                {
                    LoginUser p_LoginUser = new LoginUser(this.Context);
                    txtUserId.Value = p_LoginUser.UserID.ToString();
                }
                catch { }
                GetSysConfig();
                if (!string.IsNullOrEmpty(Request.QueryString["taskid"]))
                {
                    txtId.Value = Request.QueryString["id"].Trim();
                    txtTaskId.Value = Request.QueryString["taskid"].Trim();
                    txtPhone.Value = Request.QueryString["phone"].Trim();
                    InitField(Request.QueryString["id"].Trim(), Request.QueryString["taskid"].Trim());
                    try
                    {
                        Model.T_CTI_Task model = new BLL.T_CTI_Task().GetModel(Convert.ToInt32(Request.QueryString["taskid"].Trim()));
                        if (model != null)
                        {
                            if (model.y_TkModelId != 0)
                            {
                                txtPagerId.Value = model.y_TkModelId.ToString();
                                Model.T_Ask_PagerInfo modelpager = new BLL.T_Ask_PagerInfo().GetModel(Convert.ToInt32(model.y_TkModelId.ToString()));
                                spanTitle.InnerHtml = modelpager.F_Title.Trim();
                                if (modelpager.F_Remark != null)
                                {
                                    spanRemark.InnerHtml = modelpager.F_Remark.Trim();
                                }
                                LoadViewList(model.y_TkModelId.ToString(), Request.QueryString["id"].Trim());
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }

        #region 加载呼叫结果

        private void InitHJJG()
        {
            string list = "";
            string res = "";
            res += "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 252px; background-color: White; border: 1px solid #D6D6D6;\">";
            DataTable dt = new DataTable();
            try
            {
                dt = new BLL.T_Sys_DictionaryValue().GetList("F_DictionaryFlag='HJJG' order by F_Sort ").Tables[0];
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    string check = "";
                    if (i == 0)
                    {
                        check = "checked";
                        txtHJJGId.Value = dr["F_DictionaryValueId"].ToString().Trim();
                        txtHJJGName.Value = dr["F_Name"].ToString().Trim();
                    }
                    res += "<tr><td style=\"border-bottom: 1px dotted #cccccc; padding:3px; height:23px;\">";
                    res += "&nbsp;&nbsp;&nbsp;<input id=\"rdHJJG_" + dr["F_DictionaryValueId"].ToString().Trim() + "\" type=\"radio\" name=\"rdHJJG\" " + check + " onclick=\"CheckHJJG('" + dr["F_DictionaryValueId"].ToString().Trim() + "','" + dr["F_Name"].ToString().Trim() + "');\" /><label for=\"rdHJJG_" + dr["F_DictionaryValueId"].ToString().Trim() + "\">&nbsp;&nbsp;" + dr["F_Name"].ToString().Trim() + "</label></td></tr>";

                    list += "<div style='float:left; width:150px;height:23px; padding:3px;'>&nbsp;&nbsp;&nbsp;<input id=\"rdHJJGList_" + dr["F_DictionaryValueId"].ToString().Trim() + "\" type=\"radio\" name=\"rdHJJGList\" " + check + "  onclick=\"CheckHJJGList('" + dr["F_DictionaryValueId"].ToString().Trim() + "','" + dr["F_Name"].ToString().Trim() + "');\" /><label for=\"rdHJJGList_" + dr["F_DictionaryValueId"].ToString().Trim() + "\">&nbsp;&nbsp;" + dr["F_Name"].ToString().Trim() + "</label></div>";
                    i++;
                }
            }
            catch
            { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            res += "</table>";
            divHJJGList.InnerHtml = list;
        }

        #endregion

        private void GetSysConfig()
        {
            try
            {
                BLL.T_Sys_SystemConfig bll_T_Sys_SystemConfig = new BLL.T_Sys_SystemConfig();
                string calloutflag = bll_T_Sys_SystemConfig.GetParamValueByParamCode("calloutflag");//CTIIP
                if (!string.IsNullOrEmpty(calloutflag))
                {
                    txtcallstate.Value = calloutflag.Trim();
                }
            }
            catch
            { }
        }

        private void InitField(string id, string taskid)
        {
            string strHtml = "";
            DataTable dt = new DataTable();
            DataTable dtphone = new DataTable();
            try
            {
                string sql = " ";
                try
                {
                    if (taskid.Trim() != "")
                    {
                        sql += " and [F_TaskId]=" + taskid + " ";
                    }
                    else
                    {
                        sql += " and [F_TaskId]=0 ";
                    }
                }
                catch
                { }
                BLL.T_Call_PageField bll = new BLL.T_Call_PageField();
                dtphone = new BLL.T_Call_TaskTelNum().GetList(" F_Id=" + id + " ").Tables[0];
                dt = bll.GetList(" 1=1 " + sql + " order by F_Sort ").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string fieldname = dt.Rows[i]["F_DBFieldlName"].ToString().Trim();
                        string style = "";
                        string read = "";
                        if (fieldname == "F_Phone")
                        {
                            style = "background-color: #cccccc;color:red;";
                            read = "readonly";
                        }
                        strHtml += "<div style=\"float:left; width:49.9%; height:30px; text-align:center; padding:0px;\">";
                        strHtml += "<div style=\"height: 30px; padding: 0px; text-align: center;\">";
                        strHtml += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"";
                        strHtml += " width=\"100%\" height=\"100%\" style=\" text-align:center;padding: 0px; border: 1px dotted #CCCCCC;\" class=\"form_table\">";
                        strHtml += "<tr><th style=' height:30px;text-align:right;padding: 0px; padding-right:5px; width:100px; border-right: 1px dotted #CCCCCC;'><input type=\"text\" id=\"txt_id_" + dt.Rows[i]["F_Id"].ToString() + "\" value='" + dt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "' name=\"forcount\" style=\"display:none\" />";
                        strHtml += "<span id=\"span_name_" + dt.Rows[i]["F_Id"].ToString() + "\">" + dt.Rows[i]["F_Name"].ToString() + "：</span></th>";
                        strHtml += "<td style='height:30px;text-align:left;padding: 0px;'>&nbsp;&nbsp;<input id='txt_field_" + dt.Rows[i]["F_Id"].ToString() + "' name='txt_field_" + dt.Rows[i]["F_Id"].ToString() + "' value='" + dtphone.Rows[0][fieldname].ToString() + "' " + read + " style='width:120px;" + style + "' class='txtInput normal' /></td></tr></table></div></div>";
                    }

                }

                if (dtphone.Rows[0]["F_HJJGName"].ToString().Trim() != "")
                {
                    txtHJJGId.Value = dtphone.Rows[0]["F_HJJGId"].ToString().Trim();
                    txtHJJGName.Value = dtphone.Rows[0]["F_HJJGName"].ToString().Trim();
                }
            }
            catch
            {
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
                dtphone.Clear();
                dtphone.Dispose();
            }
            divFieldList.InnerHtml = strHtml;
        }

        #region 加载试卷
        private void LoadViewList(string pagerid, string telid)
        {
            StringBuilder res = new StringBuilder();
            DataTable dt = new DataTable();
            string sql = " ";
            string itemtable = "T_Ask_PagerItems";
            string questionidname = "F_QuestionId";
            string sortname = "F_Sort";
            string itemidname = "F_ItemId";
            string key = "T_Ask_PagerItems.F_ItemId";
            try
            {
                if (pagerid.Trim() != "")
                {
                    itemtable = "T_Ask_PagerItems";
                    questionidname = "F_QuestionId";
                    sortname = "F_Sort";
                    itemidname = "F_ItemId";
                    key = "T_Ask_PagerItems.F_ItemId";
                    sql += " and [T_Ask_PagerItems].[F_PagerId]=" + pagerid + " ";
                }
                int recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                    "vw_Ask_Question LEFT OUTER JOIN " + itemtable + " ON vw_Ask_Question.F_QuestionId = " + itemtable + "." + questionidname + " ",
                    key,
                    "vw_Ask_Question.*, '" + telid + "' as TelId," + itemtable + "." + sortname + " as F_Sort," + itemtable + "." + itemidname + " as F_ItemId",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY " + itemtable + "." + sortname + " ",
                    100,
                    1,
                    true,
                    out recordCount);

                if (dt != null)
                {
                    string ctrl = "~/askmanage/_controls/ctl_pagerview.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);
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            divDataItems.InnerHtml = res.ToString();
        }
        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
        }
    }
}