using BLL;
using System;
using System.Data;
using System.Linq;

namespace FineUIPro.Web.BaseInfo
{
    public partial class HyperLinkSet : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();//按钮权限
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                if (CurrUser.Account == BLL.Const.Gly)
                {
                    SimpleForm1.Hidden = false;
                }
                else
                {
                    SimpleForm1.Hidden = true;
                }
                // 绑定表格
                BindGrid();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindGrid()
        {
            string strSql = @"SELECT * FROM  dbo.Base_HyperLinkSet";
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, null);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }

        /// <summary>
        /// 改变索引事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion

        #region 分页下拉选择
        /// <summary>
        /// 分页下拉选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            BLL.HyperLinkSetService.DeleteHyperLinkSetById(hfFormID.Text);
            this.hfFormID.Text = string.Empty;
            this.txtHyperLinkName.Text = string.Empty;
            this.txtHyperLinkUrl.Text = string.Empty;
            this.txtRemark.Text = string.Empty;
            BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete the application information");
            // 重新绑定表格,并模拟点击[新增按钮]
            BindGrid();
            //PageContext.RegisterStartupScript("onNewButtonClick();");

        }
        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();

                    BLL.HyperLinkSetService.DeleteHyperLinkSetById(rowID);
                    this.hfFormID.Text = string.Empty;
                    this.txtHyperLinkName.Text = string.Empty;
                    this.txtHyperLinkUrl.Text = string.Empty;
                    this.txtRemark.Text = string.Empty;
                    BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete the application information");
                }

                BindGrid();
            }
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 右键编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }

        /// <summary>
        /// 编辑数据方法
        /// </summary>
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("Please select at least one record!", MessageBoxIcon.Warning);
                return;
            }
            string Id = Grid1.SelectedRowID;
            var hyper = BLL.HyperLinkSetService.GetHyperLinkSetById(Id);
            if (hyper != null)
            {
                this.txtHyperLinkName.Text = hyper.HyperLinkName;
                this.txtHyperLinkUrl.Text = hyper.HyperLinkUrl;
                this.txtRemark.Text = hyper.Remark;
                hfFormID.Text = Id;
                this.btnDelete.Enabled = true;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strRowID = hfFormID.Text;
            if (!BLL.HyperLinkSetService.IsExitHyperLinkName(this.txtHyperLinkName.Text.Trim(), strRowID))
            {
                Model.Base_HyperLinkSet hyper = new Model.Base_HyperLinkSet
                {
                    HyperLinkName = this.txtHyperLinkName.Text.Trim(),
                    HyperLinkUrl = this.txtHyperLinkUrl.Text.Trim(),
                    Remark = txtRemark.Text.Trim()
                };
                if (string.IsNullOrEmpty(strRowID))
                {
                    hyper.HyperLinkId = SQLHelper.GetNewID(typeof(Model.Base_HyperLinkSet));
                    BLL.HyperLinkSetService.AddHyperLinkSet(hyper);
                    BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add the application information");
                    ShowNotify("Save Successfully!", MessageBoxIcon.Success);
                }
                else
                {
                    hyper.HyperLinkId = strRowID;
                    BLL.HyperLinkSetService.UpdateHyperLinkSet(hyper);
                    BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify the application information");
                    ShowNotify("Save Successfully!", MessageBoxIcon.Success);
                }
                this.SimpleForm1.Reset();
                // 重新绑定表格,并点击当前编辑或者新增的行
                BindGrid();
            }
            else
            {
                ShowNotify("The application name already exists!", MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.HyperLinkSetMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNew.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuEdit.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnDelete.Hidden = false;
                }
            }
        }
        #endregion

        protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e)
        {
            this.EditData();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.hfFormID.Text = string.Empty;
            this.txtHyperLinkName.Text = string.Empty;
            this.txtHyperLinkUrl.Text = string.Empty;
            this.txtRemark.Text = string.Empty;
            this.btnDelete.Enabled = false;
        }
    }
}