using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
namespace FineUIPro.Web.ZHGL.Information
{
    public partial class AnalyseResource : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            // 表头过滤
            FilterDataRowItem = FilterDataRowItemImplement;
            if (!IsPostBack)
            {
                if (this.CurrUser != null && this.CurrUser.PageSize.HasValue)
                {
                    Grid1.PageSize = this.CurrUser.PageSize.Value;
                }
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                ///判断是否集团公司
                if (!BLL.CommonService.IsMainUnitOrAdmin(this.CurrUser.UserId))
                {
                    this.drpUnit.Enabled = false;
                }
                BLL.UnitService.InitUnitDropDownList(this.drpUnit, this.CurrUser.LoginProjectId, false);
                this.drpUnit.SelectedValue = this.CurrUser.UnitId; ///当前人单位 
                this.txtStarTime.Text = string.Format("{0:yyyy-MM-dd}", System.DateTime.Now.AddMonths(-1));
                this.txtEndTime.Text = string.Format("{0:yyyy-MM-dd}", System.DateTime.Now);
                // 绑定表格
                //BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            List unitValues = new List();
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                foreach (ListItem item in this.drpUnit.SelectedItemArray)
                {
                    unitValues.Add(item.Value);
                }
            }
            List resourceCollections = BLL.AnalyseResourceService.GetListResourceCollection(unitValues, this.txtUserName.Text.Trim(), Funs.GetNewDateTime(this.txtStarTime.Text.Trim()), Funs.GetNewDateTime(this.txtEndTime.Text.Trim()));
            DataTable tb = this.GetPagedDataTable(Grid1, resourceCollections);
            // 2.获取当前分页数据
            //var table = this.GetPagedDataTable(Grid1, tb1);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            this.OutputSummaryData(resourceCollections);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        /// 
        /// 计算合计
        /// 
        private void OutputSummaryData(List resourceCollections)
        {
            int TotalCount = 0;
            int TotalUsedCount = 0;
            string TotalUsedRate = "0%";
            int LawRegulationCount = 0;
            int HSSEStandardListCount = 0;
            int RulesRegulationsCount = 0;
            int ManageRuleCount = 0;
            int TrainDBCount = 0;
            int TrainTestDBCount = 0;
            int AccidentCaseCount = 0;
            int KnowledgeDBCount = 0;
            int HazardListCount = 0;
            int RectifyCount = 0;
            int HAZOPCount = 0;
            int AppraiseCount = 0;
            int ExpertCount = 0;
            int EmergencyCount = 0;
            int SpecialSchemeCount = 0;
            foreach (var item in resourceCollections)
            {
                TotalCount += item.TotalCount;
                TotalUsedCount += item.TotalUsedCount;
                LawRegulationCount += item.LawRegulationCount;
                HSSEStandardListCount += item.HSSEStandardListCount;
                RulesRegulationsCount += item.RulesRegulationsCount;
                ManageRuleCount += item.ManageRuleCount;
                TrainDBCount += item.TrainDBCount;
                TrainTestDBCount += item.TrainTestDBCount;
                AccidentCaseCount += item.AccidentCaseCount;
                KnowledgeDBCount += item.KnowledgeDBCount;
                HazardListCount += item.HazardListCount;
                RectifyCount += item.RectifyCount;
                HAZOPCount += item.HAZOPCount;
                AppraiseCount += item.AppraiseCount;
                ExpertCount += item.ExpertCount;
                EmergencyCount += item.EmergencyCount;
                SpecialSchemeCount += item.SpecialSchemeCount;
            }
            string rate = string.Empty;
            if (TotalCount > 0)
            {
                decimal totalUsedRate = Convert.ToDecimal(TotalUsedCount) / Convert.ToDecimal(TotalCount);
                totalUsedRate = Math.Round(totalUsedRate * 100, 2, MidpointRounding.AwayFromZero);
                if (totalUsedRate == 1)
                {
                    rate = "100.00";
                }
                else
                {
                    rate = totalUsedRate.ToString();
                }
            }
            else
            {
                rate = "0";
            }
            TotalUsedRate = rate + "%";
            JObject summary = new JObject();
            summary.Add("UnitName", "合计:");
            summary.Add("TotalCount", TotalCount);
            summary.Add("TotalUsedCount", TotalUsedCount);
            summary.Add("TotalUsedRate", TotalUsedRate);
            summary.Add("LawRegulationCount", LawRegulationCount);
            summary.Add("HSSEStandardListCount", HSSEStandardListCount);
            summary.Add("RulesRegulationsCount", RulesRegulationsCount);
            summary.Add("ManageRuleCount", ManageRuleCount);
            summary.Add("TrainDBCount", TrainDBCount);
            summary.Add("TrainTestDBCount", TrainTestDBCount);
            summary.Add("AccidentCaseCount", AccidentCaseCount);
            summary.Add("KnowledgeDBCount", KnowledgeDBCount);
            summary.Add("HazardListCount", HazardListCount);
            summary.Add("RectifyCount", RectifyCount);
            summary.Add("HAZOPCount", HAZOPCount);
            summary.Add("AppraiseCount", AppraiseCount);
            summary.Add("ExpertCount", ExpertCount);
            summary.Add("EmergencyCount", EmergencyCount);
            summary.Add("SpecialSchemeCount", SpecialSchemeCount);
            Grid1.SummaryData = summary;
        }
        #endregion
        #region Grid行点击前事件
        /// 
        /// 行点击事件 考虑单据查看数据
        /// 
        /// 
        /// 
        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
        }
        #endregion
        #region 分页 排序 过滤
        #region 根据表头信息过滤列表数据
        /// 
        /// 过滤表头
        /// 
        /// 
        /// 
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 根据表头信息过滤列表数据
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private bool FilterDataRowItemImplement(object sourceObj, string fillteredOperator, object fillteredObj, string column)
        {
            bool valid = false;
            if (column == "UnitName")
            {
                string sourceValue = sourceObj.ToString();
                string fillteredValue = fillteredObj.ToString();
                if ((fillteredOperator == "equal" && sourceValue == fillteredValue) || fillteredOperator == "contain" && sourceValue.Contains(fillteredValue))
                {
                    valid = true;
                }
            }
            if (column == "UserName")
            {
                string sourceValue = sourceObj.ToString();
                string fillteredValue = fillteredObj.ToString();
                if ((fillteredOperator == "equal" && sourceValue == fillteredValue) || fillteredOperator == "contain" && sourceValue.Contains(fillteredValue))
                {
                    valid = true;
                }
            }
            return valid;
        }
        #endregion
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private bool GetButtonPower(string button)
        {
            return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.AnalyseResourceMenuId, button);
        }
        #endregion
        #region 清空
        /// 
        /// 清空下拉框
        /// 
        /// 
        /// 
        protected void drpUnit_ClearIconClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.CurrUser.UnitId))
            {
                this.drpUnit.SelectedValue = this.CurrUser.UnitId;
            }
            else
            {
                this.drpUnit.SelectedIndex = 0;
            }
        }
        #endregion
        #region 统计
        /// 
        /// 统计
        /// 
        /// 
        /// 
        protected void BtnAnalyse_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
    }
}