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()
        {
            string[,] arr = BLL.Database.GetTables();
            this.ddlTables.Items.Clear();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                this.ddlTables.Items.Add(new ListItem(arr[i, 1], arr[i, 1]));
            }
            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;

        }


        void GetList()
        {
            databaseModel.Datatable = this.ddlTables.SelectedItem.Text.Trim();
            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 += "&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)
        {
     
        }
    }
}
