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

namespace eFrameWork.Manage
{
    public partial class ModelItems_bindObject : System.Web.UI.Page
    {
        private eUser user;
        public string s1 = eParameters.Request("s1");
        public string type = eParameters.QueryString("type");//type:1搜索，2数据
        public string id = eParameters.QueryString("id");
        private eDataBase _database;
        /// <summary>
        /// 当前模块数据库
        /// </summary>
        public eDataBase DataBase
        {
            get
            {
                if (_database == null)
                {
                    string sql = "";
                    if (type == "1")
                    {
                        sql = "select ModelID from a_eke_sysModelConditions where ModelConditionID='" + id + "'";
                    }
                    else
                    {
                        sql = "select ModelID from a_eke_sysModelItems where ModelItemID='" + id + "'";
                    }
                    string modelid = eBase.DataBase.getValue(sql);
                    string dsid = eBase.DataBase.getValue("select DataSourceID from a_eke_sysModels where ModelID='" + modelid + "'");
                    if (dsid.ToString().Length > 0)
                    {
                        _database = new eDataBase(new Guid(dsid));
                    }
                    else
                    {
                        _database = eConfig.DefaultDataBase;
                    }
                }
                return _database;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            user = new eUser("Manage");
            user.Check();


            eDataBase db;
            string sql = "";
            if (type == "1")
            {
                sql = "select DataSourceID from a_eke_sysModelConditions where ModelConditionID='" + id + "'";
            }
            else
            {
                sql = "select DataSourceID from a_eke_sysModelItems where ModelItemID='" + id + "'";
            }
            string dsid = eBase.DataBase.getValue(sql);
            if (dsid.Length == 0)
            {
                db = DataBase;
            }
            else if (dsid == "maindb")
            {
                db = eConfig.DefaultDataBase;
            }
            else
            {
                db = new eDataBase(new Guid(dsid));
            }


            DataTable dt = db.getSchemaTableViews();
            if (s1.Length > 0)
            {
                DataRow[] rows = (from row in dt.AsEnumerable()
                                  where row["value"].ToString().ToLower().Contains(s1.ToLower())
                                  select row).ToArray();
                Repeater1.DataSource = rows.toDataTable();
            }
            else
            {
                Repeater1.DataSource = dt;
            }           
            Repeater1.DataBind();
        }
    }
}