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;

namespace HySoft.BaseCallCenter.Web.formmanage
{
    public partial class formset : System.Web.UI.Page
    {
        private int count = 2;
        private string eid = "0";
        private string fid = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["fid"]))
                {
                    fid = Request.QueryString["fid"].Trim();
                }
                if (!string.IsNullOrEmpty(Request.QueryString["count"]))
                {
                    try
                    {
                        count = Convert.ToInt32(Request.QueryString["count"].Trim());
                    }
                    catch (Exception ex)
                    { 
                    }
                }
                if (!string.IsNullOrEmpty(Request.QueryString["eid"]))
                {
                    eid = Request.QueryString["eid"].Trim();
                    InitHtml();
                }
            }
        }

        private void InitHtml()
        { 
            StringBuilder res = new StringBuilder();
            res.Append("<table class=\"form_table\" style=\"width:100%;\">");
            DataTable dt=new DataTable();
            DataTable dtdic = new DataTable();
            try
            {
                dt = new BLL.T_Form_EntityAttribute().GetList("F_EntityId=" + eid + " and F_VisibleToPlatform=1 ").Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        res.Append("<tr>");
                    }
                    else
                    {
                        if (i % count == 0)
                        {
                            res.Append("</tr><tr>");
                        }
                    }
                    res.Append("<th><input type='text' id='span_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' name='span_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' value='" + dt.Rows[i]["F_Name"].ToString().Trim() + "' class='txtInput' style='border-left:0px;border-right:0px;border-top:0px;' /></th>");
                    if (dt.Rows[i]["F_IsLookup"].ToString().Trim().ToLower() == "true")
                    {
                        dtdic = new BLL.T_Sys_DictionaryValue().GetList("F_DictionaryFlag='" + dt.Rows[i]["F_LookupKey"].ToString().Trim() + "'").Tables[0];
                        string listhtml = "<option value=\"\">请选择" + dt.Rows[i]["F_Name"].ToString().Trim() + "</option>";
                        foreach (DataRow drdic in dtdic.Rows)
                        {
                            listhtml += "<option value=\"" + drdic["F_DictionaryValueId"].ToString().Trim() + "\">" + drdic["F_Name"].ToString().Trim() + "</option>";
                        }
                        res.Append("<td><select id='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' name='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' value='" + dt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "' class='selectBox'>"+listhtml+"</select></td>");
                    }
                    else
                    {
                        switch (dt.Rows[i]["F_DBTypeName"].ToString().Trim().ToLower())
                        { 
                            case "int":
                                res.Append("<td><input type='text' id='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' name='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' value='" + dt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "' class='txtInput' onKeyUp=\"this.value=this.value.replace(/[^\\d]/g,'');\" /></td>");
                                break;
                            case "decimal":
                                res.Append("<td><input type='text' id='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' name='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' value='' class='txtInput' onKeyUp=\"this.value=this.value.replace(/[^\\.\\d]/g,'');if(this.value.split('.').length>2){this.value=this.value.split('.')[0]+'.'+this.value.split('.')[1]}\" /></td>");
                                break;
                            case "datetime":
                                res.Append("<td><input type='text' id='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' name='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' value='" + dt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "'  onfocus=\"WdatePicker({skin:'whyGreen'})\" class='txtInput' /></td>");
                                break;
                            default:
                                res.Append("<td><input type='text' id='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' name='txt_arr_" + dt.Rows[i]["F_AttributeId"].ToString().Trim() + "' value='" + dt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "' class='txtInput' /></td>");
                                break;
                        }
                    }
                    if (i == (dt.Rows.Count - 1))
                    {
                        int tdcount = 0;
                        if ((dt.Rows.Count % count) != 0)
                        {
                            tdcount=count - (dt.Rows.Count % count);
                        }
                        for (int m = 0; m < tdcount; m++)
                        {
                            res.Append("<th>&nbsp;</th><td>&nbsp;</td>");
                        }

                        res.Append("</tr>");
                    }
                }
            }
            catch
            { }
            finally
            {
                dt.Dispose();
                dt.Clear();
            }

            res.Append("</table>");

            divForm.InnerHtml = res.ToString();
        }
    }
}