using System;
using System.Web.UI.WebControls;

namespace Stat.web.Manager
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ShowDatabase : System.Web.UI.Page
    {
        Model.PageInfo pageInfoModel = new Stat.Model.PageInfo(20);
        Model.Database databaseModel = new Stat.Model.Database();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pageInfoModel.PageIndex = Plugin.Common.Rq_Int("page");
            pageInfoModel.PageIndex = pageInfoModel.PageIndex < 1 ? 1 : pageInfoModel.PageIndex;
            if (!IsPostBack)
            {
                Initialize();
            }
            GetList();

        }

        void Initialize()
        {

            FilldllTables();

            this.ddlBase.SelectedIndex = -1;
            if (this.ddlBase.Items.FindByValue(Plugin.Common.Rq("s")) != null)
                this.ddlBase.Items.FindByValue(Plugin.Common.Rq("s")).Selected = true;
            //
            this.ddlTables.SelectedIndex = -1;
            if (this.ddlTables.Items.FindByText(Plugin.Common.Rq("t")) != null)
                this.ddlTables.Items.FindByText(Plugin.Common.Rq("t")).Selected = true;
            //
            this.txtGetFilde.Text = Plugin.Common.Rq("gf");
            this.txtSearchFilde.Text = Plugin.Common.Rq("qf");
            this.txtKeyword.Text = Plugin.Common.Rq("w");
            this.ddlSort.Items.FindByValue(Plugin.Common.Rq_Int("st").ToString()).Selected = true;

        }

        private void FilldllTables()
        {
            int baseId = Convert.ToInt32("0" + this.ddlBase.SelectedValue);

            string[,] arr = BLL.Database.GetTables(baseId);
            this.ddlTables.Items.Clear();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                this.ddlTables.Items.Add(new ListItem(arr[i, 1], arr[i, 0]));
            }
        }


        void GetList()
        {
            databaseModel.DatabaseId = Convert.ToInt32("0" + this.ddlBase.SelectedValue);
            databaseModel.Datatable = this.ddlTables.SelectedItem.Text.Trim();
            databaseModel.SortField = this.ddlTables.SelectedValue;
            databaseModel.GetField = this.txtGetFilde.Text.Trim() == "" ? "*" : this.txtGetFilde.Text.Trim();
            databaseModel.SortStype = this.ddlSort.SelectedValue == "0" ? 0 : 1;
            databaseModel.QueryField = this.txtSearchFilde.Text.Trim();
            databaseModel.Keyword = this.txtKeyword.Text.Trim();


            this.gvList.DataSource = BLL.Database.GetList(databaseModel, ref pageInfoModel);
            this.gvList.DataBind();

            GetIndex();
        }

        void GetIndex()
        {
            string keyStr = "t=" + databaseModel.Datatable;
            keyStr += "&s=" + databaseModel.DatabaseId;
            keyStr += "&gf=" + databaseModel.GetField;
            keyStr += "&qf=" + databaseModel.QueryField;
            keyStr += "&w=" + databaseModel.Keyword;
            keyStr += "&st=" + databaseModel.SortStype;
            this.ltrPageIndex.Text = JPageIndex.GetIndex.Get(pageInfoModel.RecordCount, pageInfoModel.PageIndex, pageInfoModel.PageSize, 0, 1, keyStr);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlBase_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilldllTables();
        }
    }
}
