using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using BLL;

namespace FineUIPro.Web.common.BaseInfo
{
    public partial class Unit : PageBase
    {
        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                BindGrid();
            }
        }
        #endregion

        #region BindGrid

        private void BindGrid()
        {
            string strSql = @"select u.UnitId,u.UnitCode,u.UnitName,u.ProjectRange,u.Corporate,u.Address,
                                     u.Telephone,u.Fax,t.UnitTypeName
                              from dbo.Base_Unit u
                              LEFT JOIN dbo.Base_UnitType t ON t.UnitTypeId = u.UnitTypeId
                              where 1=1 ";
           
            List<SqlParameter> parms = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(this.txtUnitName.Text))
            {
                strSql += " and u.UnitName LIKE  @UnitName";
                parms.Add(new SqlParameter("@UnitName", "%" + this.txtUnitName.Text.Trim() + "%"));
            }

            strSql += " order by UnitCode";
            SqlParameter[] parameter = parms.ToArray();

            DataTable dt = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = dt.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, dt);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        #endregion

        #region 增加按钮事件
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (GetButtonPower(Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("UnitEdit.aspx", "新增 - ")));
            }
            else
            {
                Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion

        #region  编辑/查看/删除数据

        /// <summary>
        /// 编辑
        /// </summary>
        private void EditData()
        {
            if (GetButtonPower(BLL.Const.BtnModify))
            {
                if (Grid1.SelectedRowIndexArray.Length == 0)
                {
                    Alert.ShowInParent(Resources.Lan.SelectLeastOneRecord);
                    return;
                }
                string Id = Grid1.SelectedRowID;
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("UnitEdit.aspx?unitId={0}", Id, "编辑 - ")));
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt,MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// Grid行双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            EditData();
        }

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

        protected void btnMenuView_Click(object sender, EventArgs e)
        {
            if (GetButtonPower(BLL.Const.BtnSee))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("UnitView.aspx?UnitId={0}", Grid1.SelectedRowID, "查看 - ")));
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (GetButtonPower(BLL.Const.BtnDelete))
            {
                string strShowNotify = string.Empty;
                if (Grid1.SelectedRowIndexArray.Length > 0)
                {
                    foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                    {
                        string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                        string content = judgementDelete(rowID);
                        var unit = BLL.Base_UnitService.GetUnit(rowID);

                        if (string.IsNullOrEmpty(content))
                        {
                            BLL.Base_UnitService.DeleteUnitByUnitId(rowID);
                            BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.UnitMenuId, Const.BtnDelete, rowID);
                        }
                        else
                        {
                            strShowNotify += Resources.Lan.UnitName + ":" + unit.UnitName + content;
                        }
                    }
                }

                if (!string.IsNullOrEmpty(strShowNotify))
                {
                    Alert.ShowInTop(strShowNotify, MessageBoxIcon.Warning);
                }
                else
                {
                    BindGrid();
                    ShowNotify(Resources.Lan.DeletedSuccessfully, MessageBoxIcon.Success);
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region 分页/排序
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        /// <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();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        #endregion

        #region 关闭窗口
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

        #region 获取按钮权限
        /// <summary>
        /// 获取按钮权限
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        private bool GetButtonPower(string button)
        {
            return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitMenuId, button);
        }
        #endregion

        #region 判断是否可删除
        /// <summary>
        /// 判断是否可以删除
        /// </summary>
        /// <returns></returns>
        private string judgementDelete(string userId)
        {
            string content = string.Empty ;
            if (Funs.DB.Project_Unit.FirstOrDefault(x=>x.UnitId== userId)!=null)
            {
                content += "已在【项目单位】中使用,不能删除!";
            }
            return content;
        }
        #endregion
    }
}