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;

namespace eFrameWork.Manage
{
    public partial class ReportItems : System.Web.UI.Page
    {
        public string ReportID = eParameters.QueryString("ReportID");
        public DataRow row;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable tb = eOleDB.getDataTable("select * from a_eke_sysReports where ReportID='" + ReportID + "'");
            if (tb.Rows.Count == 0) Response.End();
            row = tb.Rows[0];


            string sql = "";
            string act = eParameters.QueryString("act").ToLower();
            if (act.Length == 0) 
            {
                List();
            }
            else
            {

                string ReportItemID = eParameters.QueryString("ReportItemID");
                string value = eParameters.QueryString("value").Replace("'", "''");
                string item = eParameters.QueryString("item").ToLower();
                #region 获取数据
                if (act == "getdata")
                {
                    Response.Clear();
                    List();
                    System.IO.StringWriter sw = new System.IO.StringWriter();
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    ControlGroup.RenderControl(htw);
                    ControlGroup.Visible = false;

                    Response.Write(sw.ToString());
                    Response.End();
                }
                #endregion
                #region 添加
                if (act == "additem")
                {
                    string type = eParameters.QueryString("type");

                    eOleDB.Execute("insert into a_eke_sysReportItems (ReportID,Type) values ('" + ReportID + "','" + type + "')");
                    eOleDB.Execute(sql);

                    eJson json = new eJson();
                    json.Add("success", "1");
                    json.Add("message", "添加成功");
                    Response.Write(json.ToString());
                    Response.End();
                }
                #endregion
                #region 修改动作
                if (act == "setreport")
                {

                    sql = "update a_eke_sysReports set " + item + "='" + value + "' where ReportID='" + ReportID + "'";
                    if (value == "NULL") sql = "update a_eke_sysReports set " + item + "=" + value + " where ReportID='" + ReportID + "'";
                    eOleDB.Execute(sql);
                    eBase.clearDataCache("a_eke_sysReports");
                    Response.End();
                }
                if (act == "setitem")
                {
                    if (item == "px" && value == "0") value = "999999";
                    sql = "update a_eke_sysReportItems set " + item + "='" + value + "' where ReportItemID='" + ReportItemID + "'";
                    if (value == "NULL") sql = "update a_eke_sysReportItems set " + item + "=" + value + " where ReportItemID='" + ReportItemID + "'"; 
                    eOleDB.Execute(sql);
                    eBase.clearDataCache("a_eke_sysReportItems");
                    Response.End();
                }
                #endregion
                #region  删除
                if (act == "delitem")
                {
                    eOleDB.Execute("delete from a_eke_sysReportItems where ReportID='" + ReportID + "' and ReportItemID='" + ReportItemID + "'");
                    eBase.clearDataCache("a_eke_sysReportItems");
                    eJson json = new eJson();
                    json.Add("success", "1");
                    json.Add("message", "删除成功");
                    Response.Write(json.ToString());
                    Response.End();
                }
                #endregion

                //拖动排序
                if (act == "setorders")
                {
                    string ids = eParameters.Form("ids");
                    string[] arr = ids.Split(",".ToCharArray());
                    for (int i = 0; i < arr.Length; i++)
                    {
                        value = (i + 1).ToString();
                        eOleDB.Execute("update a_eke_sysReportItems set PX='" + value + "' where ReportID='" + ReportID + "' and ReportItemID='" + arr[i] + "'");
                    }
                    //eOleDB.Execute("update a_eke_sysReportItems set PX='999999' where ReportID='" + ReportID + "' and ReportItemID not in ('" + ids.Replace(",", "','") + "')");
                    eBase.clearDataCache("a_eke_sysReportItems");
                    eJson json = new eJson();
                    json.Add("success", "1");
                    json.Add("message", "排序成功");
                    Response.Write(json.ToString());
                    Response.End();
                }



               
            }
        }
        private void List()
        {
            #region 列表
            eList elist = new eList("a_eke_sysReportItems");
            elist.Where.Add("ReportID='" + ReportID + "' and Type='Y'");
            elist.OrderBy.Add("px,addTime");
            elist.Bind(RepY);


            elist = new eList("a_eke_sysReportItems");
            elist.Where.Add("ReportID='" + ReportID + "' and Type='X'");
            elist.OrderBy.Add("px,addTime");
            elist.Bind(RepX);
            #endregion
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            Literal lit = (Literal)Master.FindControl("LitTitle");
            if (lit != null)
            {
                lit.Text = "报表 - " + eConfig.manageName() ;
            }
        }
    }
}