using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EKETEAM.Data;
using EKETEAM.FrameWork;
using EKETEAM.UserControl;
using LitJson;

namespace eFrameWork.Manage
{
    public partial class DataViews : System.Web.UI.Page
    {
        public string id = eParameters.Request("id");
        public string act = eParameters.Request("act");
        public string sql = "";
        public eForm edt;
        public eUser user;
        public eList elist;
        protected void Page_Load(object sender, EventArgs e)
        {
            user = new eUser("Manage");
            edt = new eForm("a_eke_sysDataViews", user);
            if (act.Length == 0)
            {

                List();
                return;
            }
            if (act == "viewdata")
            {
                eDataView dv = new eDataView();
                dv.DataID = id;
                string html = dv.getControlHTML();
                JsonData json = new JsonData();
                json["html"] = html;
                eResult.WriteJson(json);
            }
            if (act == "copy")
            {
                sql = "Insert into a_eke_sysDataViews (DataViewID,ServiceID,MC,SQL,HeaderTemplate,ItemTemplate,SplitTemplate,FooterTemplate,SM,Condition,GroupBy,OrderBy,PageSize,PageNum,PageMode) SELECT '" + Guid.NewGuid().ToString() + "',ServiceID,MC + ' 复件',SQL,HeaderTemplate,ItemTemplate,SplitTemplate,FooterTemplate,SM,Condition,GroupBy,OrderBy,PageSize,PageNum,PageMode  FROM a_eke_sysDataViews where DataViewID='" + id + "'";

                eBase.DataBase.Execute(sql);
                if (Request.ServerVariables["HTTP_REFERER"] == null)
                {
                    Response.Redirect("DataViews.aspx", true);
                }
                else
                {
                    Response.Redirect(Request.ServerVariables["HTTP_REFERER"].ToString(), true);
                }
                Response.End();
                return;
            }
            #region 信息添加、编辑

            /*
        foreach (Control control in Controls)
        {
            if (control is eFormControl)
            {
                eFormControl ec = control as eFormControl;
                Response.Write(ec.Field + "::" + ec.ID + " AA<br>");
            }
            if (control is MasterPage)
            {
                foreach (Control ctrl in control.Controls)
                {
                    if (ctrl is ContentPlaceHolder)
                    {
                        foreach (Control ct in ctrl.Controls)
                        {
                            if (ct is eFormControl)
                            {
                                eFormControl ec = ct as eFormControl;
                                Response.Write(ec.Field + "::" + ec.ID + " VV<br>");
                            }
                        }
                    }
                }
            }
        }
        */

            edt.AddControl(eFormControlGroup);
            edt.onChange += new eFormTableEventHandler(edt_onChange);
            edt.Handle();
            #endregion

        }
        public void edt_onChange(object sender, eFormTableEventArgs e)
        {
            if (e.eventType == eFormTableEventType.Inserting)
            {
                if (user["ServiceID"].Length > 0) edt.Fields.Add("ServiceID", user["ServiceID"]);
            }
            if (e.eventType == eFormTableEventType.Updated || e.eventType == eFormTableEventType.Inserted)
            {
                eBase.clearDataCache("a_eke_sysDataViews");
            }
        }
        private void List()
        {
            elist = new eList("a_eke_sysDataViews");
            elist.Where.Add("delTag=0");
            elist.Where.Add("ServiceID" + (user["ServiceID"].Length == 0 ? " is null" : "='" + user["ServiceID"] + "'"));
            elist.OrderBy.Add("addTime desc");
            elist.Where.Add(eSearchControlGroup);
            elist.OrderBy.Default = "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(); 
            }
        }
    }
}