using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.InformationProject
{
    public partial class ConstructionStandardIdentifyFile : PageBase
    {
 
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                ////权限按钮方法
                this.GetButtonPower();
                this.btnMenuDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项!");
                this.btnMenuDelete.ConfirmText = String.Format("你确定要删除选中的  行数据吗?", Grid1.GetSelectedCountReference());
               
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格
                BindGrid();
 
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
             string projectId = this.CurrUser.LoginProjectId;
 
            string strSql = @"SELECT   files.Id
                                      ,files.Code
                                      ,files.FileName
                                      ,files.VersionNumber
                                      ,files.Remark
                                      ,files.Auditer
                                      ,files.ProjectId
                                      ,users.UserName
                                      ,files.CreateDate
                                  FROM  dbo.InformationProject_ConstructionStandardIdentifyFile AS files
                                  LEFT JOIN dbo.Sys_User AS users ON  users.UserId=files.Creater  where 1=1 ";
            List listStr = new List();
            strSql += " AND files.ProjectId = @ProjectId";
            listStr.Add(new SqlParameter("@ProjectId", projectId));
 
            
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            //tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
            //    int totalPersonNum = 0;
            //    for (int i = 0; i < tb.Rows.Count; i++)
            //    {
            //        totalPersonNum += Funs.GetNewIntOrZero(tb.Rows[i][8].ToString());
            //    }
            //    JObject summary = new JObject
            //{
            //    { "TeachMan", "合计:" },
            //    { "TrainPersonNum", totalPersonNum }
            //};
            //    Grid1.SummaryData = summary;
      
        }
        #endregion
        #region 页索引改变事件
        /// 
        /// 页索引改变事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 分页选择下拉改变事件
        /// 
        /// 分页选择下拉改变事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 增加
        /// 
        /// 增加
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ConstructionStandardIdentifyFileEdit.aspx", "编辑 - ")));
        }
        #endregion
        #region 编辑
        /// 
        /// Grid行双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 编辑
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            var Id = Grid1.SelectedRowID;
            var model = InformationProject_ConstructionStandardIdentifyFileService.GetInformationProject_ConstructionStandardIdentifyFileById(Id);
            if (model != null)
            {
                
              PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ConstructionStandardIdentifyFileEdit.aspx?ConstructionStandardIdentifyFileId={0}", Id, "编辑 - ")));
                
            }
        }
        #endregion
 
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                bool isShow = false;
                if (Grid1.SelectedRowIndexArray.Length == 1)
                {
                    isShow = true;
                }
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    if (this.judgementDelete(rowID, isShow))
                    {
                        var model = InformationProject_ConstructionStandardIdentifyFileService.GetInformationProject_ConstructionStandardIdentifyFileById(rowID);
                        if (model != null)
                        {
                            LogService.AddSys_Log(this.CurrUser, model.Code, model.Id, BLL.Const.ConstructionStandardIdentifyFileMenuId, BLL.Const.BtnDelete);
                            InformationProject_ConstructionStandardIdentifyFileService.DeleteInformationProject_ConstructionStandardIdentifyFileById(rowID);
                            BindGrid();
                            ShowNotify("删除数据成功!(表格数据已重新绑定)", MessageBoxIcon.Success);
                        }
                    }
                }
            }
        }
        /// 
        /// 判断是否可删除
        /// 
        /// 
        /// 
        /// 
        private bool judgementDelete(string rowID, bool isShow)
        {
            string content = string.Empty;
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                if (isShow)
                {
                    Alert.ShowInTop(content);
                }
                return false;
            }
        }
        #endregion
        #region 关闭弹出窗
        /// 
        /// 关闭弹出窗
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectTrainRecordMenuId);
            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.BtnDelete))
                {
                    this.btnMenuDelete.Hidden = false;
                }
            }
        }
        #endregion
        #region 格式化字符串
        /// 
        /// 获取单位名称
        /// 
        /// 
        /// 
        protected string ConvertUnitName(object unitIds)
        {
            string unitName = string.Empty;
            if (unitIds != null)
            {
                List infos = unitIds.ToString().Split(',').ToList();
                if (infos.Count() > 0)
                {
                    foreach (var item in infos)
                    {
                        Model.Base_Unit unit = BLL.UnitService.GetUnitByUnitId(item);
                        if (unit != null)
                        {
                            unitName += unit.UnitName + ",";
                        }
                    }
                    if (!string.IsNullOrEmpty(unitName))
                    {
                        unitName = unitName.Substring(0, unitName.LastIndexOf(","));
                    }
                }
            }
            return unitName;
        }
        protected string ConvertUserName(object users)
        {
            string userName = string.Empty;
            if (users != null)
            {
                List infos = users.ToString().Split(',').ToList();
                if (infos.Count() > 0)
                {
                    foreach (var item in infos)
                    {
                        Model.Sys_User unit = BLL.UserService.GetUserByUserId(item);
                        if (unit != null)
                        {
                            userName += unit.UserName + ",";
                        }
                    }
                    if (!string.IsNullOrEmpty(userName))
                    {
                        userName = userName.Substring(0, userName.LastIndexOf(","));
                    }
                }
            }
            return userName;
        }
        #endregion
        #region 查询事件
        /// 
        /// 查询事件
        /// 
        /// 
        /// 
        protected void Text_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
      
    }
}