using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.SecuritySystem
{
    public partial class SafetyOrganization : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                ////权限按钮方法
                this.GetButtonPower();
                this.InitTreeMenu();
            }
        }
        #region 加载树
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            this.trSafetyOrganization.Nodes.Clear();
            TreeNode rootNode = new TreeNode
            {
                Text = "安全组织机构",
                NodeID = "0",
                Expanded = true
            };
            this.trSafetyOrganization.Nodes.Add(rootNode);
            BoundTree(rootNode.Nodes);
        }
        /// 
        /// 加载树
        /// 
        /// 
        /// 
        private void BoundTree(TreeNodeCollection nodes)
        {
            var unitLists = BLL.ProjectUnitService.GetProjectUnitListByProjectId(this.ProjectId);
            if (unitLists.Count() > 0)
            {
                if (BLL.ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.ProjectId, this.CurrUser.UnitId))
                {
                    unitLists = unitLists.Where(x => x.UnitId == this.CurrUser.UnitId).ToList();
                }
                TreeNode tn = null;
                foreach (var dr in unitLists)
                {
                    tn = new TreeNode();
                    var unitName = BLL.UnitService.GetUnitNameByUnitId(dr.UnitId);
                    if (unitName != null)
                    {
                        tn.Text = unitName;
                    }
                    tn.NodeID = dr.UnitId;
                    tn.EnableClickEvent = true;
                    var gunitType = BLL.ConstValue.GetConstByConstValueAndGroupId(dr.UnitType, BLL.ConstValue.Group_ProjectUnitType);
                    if (gunitType != null)
                    {
                        tn.ToolTip = gunitType.ConstText + ":" + unitName;
                    }
                    //tn.ToolTip = "编号:" + dr.SafetyOrganizationCode + ";
机构名称:" + dr.SafetyOrganizationName + ";
职责:" + dr.Duties + ";
组成文件:" + dr.BundleFile + ";
机构人员:" + dr.AgencyPersonnel;
                    nodes.Add(tn);
                }
            }
        }
        #endregion
        #region 绑定Grid
        /// 
        /// 绑定Grid
        /// 
        private void BindGrid()
        {
            string unitIdSelect = string.Empty;
            if (this.trSafetyOrganization.SelectedNode != null)
            {
                unitIdSelect = this.trSafetyOrganization.SelectedNode.NodeID;
            }
            string strSql = @"SELECT p.PersonId,p.CardNo,p.PersonName,p.ProjectId,p.UnitId,w.WorkPostName,p.Telephone,w.Remark"
                          + @" FROM SitePerson_Person AS P"
                          + @" LEFT JOIN  Base_WorkPost AS W ON P.WorkPostId =W.WorkPostId"
                          + @" WHERE w.PostType='1' AND p.ProjectId= '" + this.ProjectId + "' AND p.UnitId='" + unitIdSelect + "' and P.IsUsed=1 and P.OutTime is null";
            List listStr = new List();
            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();
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            //var buttonList = BLL.CommonService.GetAllButtonList(this.ProjectId, this.CurrUser.UserId, BLL.Const.ProjectSafetyOrganizationMenuId);
            //if (buttonList.Count() > 0)
            //{
            //    if (buttonList.Contains(BLL.Const.BtnAdd))
            //    {
            //        this.btnNewItem.Hidden = false;
            //    }
            //    if (buttonList.Contains(BLL.Const.BtnModify))
            //    {
            //        this.btnEditItem.Hidden = false;
            //        this.btnMenuEdit.Hidden = false;
            //    }
            //    if (buttonList.Contains(BLL.Const.BtnDelete))
            //    {
            //        this.btnDeleteItem.Hidden = false;
            //        this.btnMenuDelete.Hidden = false;
            //    }
            //}
        }
        #endregion
        #region GV 排序 翻页事件
        /// 
        /// Grid1排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 页码变化事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion
        #region GV 编辑 事件
        /// 
        /// 双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 编辑明细按钮
        /// 
        /// 
        /// 
        protected void btnEditItem_Click(object sender, EventArgs 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;
            }
            PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("SafetyOrganizationEdit.aspx?SafetyOrganizationId={0}", Grid1.SelectedRowID, "编辑 - ")));
        }
        /// 
        /// 增加明细
        /// 
        /// 
        /// 
        protected void btnNewItem_Click(object sender, EventArgs e)
        {
            if (this.trSafetyOrganization.SelectedNode != null)
            {
                if (this.trSafetyOrganization.SelectedNode.Nodes.Count == 0 && !string.IsNullOrEmpty(this.trSafetyOrganization.SelectedNode.NodeID) && this.trSafetyOrganization.SelectedNode.NodeID != "0")
                {                    
                    PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("SafetyOrganizationEdit.aspx?UnitId={0}", this.trSafetyOrganization.SelectedNode.NodeID, "编辑 - ")));
                }
                else
                {
                    ShowNotify("请选择末级节点!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 删除明细按钮
        /// 
        /// 
        /// 
        protected void btnDeleteItem_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    //var getV = BLL.SafetyOrganizationService.GetSafetyOrganizationById(rowID);
                    //if (getV != null)
                    //{
                    //    BLL.LogService.AddSys_Log(this.CurrUser, getV.Names, getV.SafetyOrganizationId, BLL.Const.ProjectSafetyOrganizationMenuId, BLL.Const.BtnDelete);
                    //    BLL.SafetyOrganizationService.DeleteSafetyOrganization(rowID);                       
                    //}                    
                }
                BindGrid();
                ShowNotify("删除数据成功!");
            }
        } 
        #endregion
        /// 
        /// 关闭窗口
        /// 
        /// 
        /// 
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void trSafetyOrganization_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            BindGrid();
        }
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("安全组织机构" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = 5000;
            this.BindGrid();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        #endregion
    }
}