using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using EKETEAM.Data;
using EKETEAM.FrameWork;
using EKETEAM.UserControl;
using System.Xml;
using System.Xml.Serialization;
using EKETEAM.ePinyin;
using LitJson;


namespace eFrameWork.Manage
{
    public partial class Templates : System.Web.UI.Page
    {
        public eList elist;
        public eForm edt;
        public eUser user;
        string sql = "";
        public string act = eParameters.Request("act");
        public eFormControl f7 = new eFormControl("f7");
        public string jsonstr = "[]";
        public string jsonitems = "未添加";
        protected void Page_Load(object sender, EventArgs e)
        {
            user = new eUser("Manage");
            edt = new eForm("a_eke_sysDataTemplates", user);
            if (act.Length == 0)
            {
                List();
                return;
            }
            if (act == "copy")
            {
                sql = "Insert into a_eke_sysDataTemplates (TemplateID,ServiceID,Name,RecordIDS,designJson,designData,remark)";
                sql += " SELECT '" + Guid.NewGuid().ToString() + "',ServiceID,Name + ' 复件',RecordIDS,designJson,designData,remark  FROM a_eke_sysDataTemplates where TemplateID='" + eParameters.QueryString("id") + "'";

                eBase.DataBase.Execute(sql);
                if (Request.ServerVariables["HTTP_REFERER"] == null)
                {
                    Response.Redirect("Templates.aspx", true);
                }
                else
                {
                    Response.Redirect(Request.ServerVariables["HTTP_REFERER"].ToString(), true);
                }
                Response.End();

            }
            if (act == "add" || act == "edit")
            {
                LitParent.Text = getRecordTree(f7.Value.ToString());
            }

            edt.AddControl(f1);//名称
            //f7.Field = "RecordID";
            //f7.FieldType = "varchar";
            //edt.AddControl(f7);//数据集
            edt.AddControl(f3);//说明

            eFormControl f5 = new eFormControl("json");
            f5.Field = "RecordIDS";
            edt.AddControl(f5);//数据集

            edt.onChange += new eFormTableEventHandler(edt_onChange);
            edt.Handle();

            if (act == "edit" && f7.Value.ToString().Length > 0)
            {
                //f7.Text = eBase.DataBase.getValue("select name from a_eke_sysDataRecords where RecordID='" + f7.Value.ToString() + "'");
            }
            if (act == "edit" && f5.Value.ToString().Length > 3)
            {
                jsonstr = f5.Value.ToString();
                jsonitems = "";
                JsonData jd = JsonMapper.ToObject(f5.Value.ToString());
                foreach (JsonData item in jd)
                {
                    jsonitems += "<a href=\"javascript:;\" class=\"esearchitem\" style=\"margin-right:8px;\"><span>" + item["text"].ToString() + " (" + item["asname"].ToString() + ")</span><i onclick=\"removeRecord(this);\"></i></a>";
                }
            }
        }
        public string getRecords(string json)
        {
            string result = "";
            if (json.Length > 3)
            {
                JsonData jds = JsonMapper.ToObject(json);
                for (int i = 0; i < jds.Count; i++)
                {
                    if (i > 0) result += ",";
                    result += jds[i]["text"].ToString();
                }
            }
            return result;
        }
        private string getRecordTree( string value)
        {
            string sql = "Select RecordID,Name from a_eke_sysDataRecords where delTag=0 ";
            sql += " order by addTime desc";

            DataTable dt = eBase.DataBase.getDataTable(sql);
            if (dt.Rows.Count == 0) return "";
            string temp = "";
            foreach (DataRow dr in dt.Rows)
            {
                temp += "<li title=\"" + dr["name"].ToString() + "\" onclick=\"set_filterText(this);\" py=\"" + ePinyin.getFirstLetter(dr["name"].ToString()).ToLower() + "\" value=\"" + dr["RecordID"].ToString() + "\">" + dr["name"].ToString() + "</li>";
            }
            return temp;
        }
        public void edt_onChange(object sender, eFormTableEventArgs e)
        {
            //eBase.End();
            if (e.eventType == eFormTableEventType.Inserting)
            {
                if (user["ServiceID"].Length > 0) edt.Fields.Add("ServiceID", user["ServiceID"]);
            }
        }
        private void List()
        {
            elist = new eList("a_eke_sysDataTemplates");
            //elist.Fields.Add("*");
            elist.Where.Add("delTag=0");
            elist.Where.Add("ServiceID" + (user["ServiceID"].Length == 0 ? " is null" : "='" + user["ServiceID"] + "'"));

            elist.Where.Add(eSearchControlGroup);
            elist.OrderBy.Add("addTime desc");

            elist.Bind(Rep, ePageControl1);
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Master == null) return;
            Literal lit = (Literal)Master.FindControl("LitTitle");
            if (lit != null)
            {
                lit.Text = "数据模板管理 - " + eConfig.manageName(); 
            }
        }
    }
}