using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.Information
{
    public partial class SafetyBriefing : PageBase
    {
        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.ConstValue.InitConstValueDropDownList(this.drpMonth, ConstValue.Group_0009, false);
                BLL.ConstValue.InitConstValueDropDownList(this.drpYear, ConstValue.Group_0008, false);
                this.drpUnit.DataTextField = "UnitName";
                drpUnit.DataValueField = "UnitId";
                drpUnit.DataSource = BLL.UnitService.GetThisUnitDropDownList();
                drpUnit.DataBind();
                this.drpUnit.Readonly = true;
                ////取上个报表时间
                DateTime showDate = System.DateTime.Now.AddMonths(-1);
                drpMonth.SelectedValue = showDate.Month.ToString();
                drpYear.SelectedValue = showDate.Year.ToString();
                GetValue();
            }
        }
        private void SetEmpty()
        {
            this.SimpleForm1.Title = string.Empty;
            lbUnitName.Text = "填报企业:";
            lbCompileMan.Text = "编制人:";
            lbAuditor.Text = "审核人:";
            lbApprover.Text = "审批人:";
            this.txtMainWork.Text = string.Empty;
            txtHazardRecording.Text = string.Empty;
            txtSafetyAccident.Text = string.Empty;
            txtEPAccident.Text = string.Empty;
            txtOHAccident.Text = string.Empty;
            txtWorkPermitNum.Text = string.Empty;
            txtHiddenDangerDetection.Text = string.Empty;
            txtRectificationSituation.Text = string.Empty;
            txtCheckProblemsRectification.Text = string.Empty;
            txtPendingProject.Text = string.Empty;
            txtContractorManagement.Text = string.Empty;
            txtEnvironmentalEmissions.Text = string.Empty;
            txtNextMonthWorkPlan.Text = string.Empty;
        }
        #endregion
        #region 获取记录值
        private void GetValue()
        {
            int year = Funs.GetNewIntOrZero(drpYear.SelectedValue);
            int month = Funs.GetNewIntOrZero(drpMonth.SelectedValue);
            Model.Information_SafetyBriefing report = Funs.DB.Information_SafetyBriefing.FirstOrDefault(e => e.UnitId == drpUnit.SelectedValue && e.Month == month && e.YearId == year);
            if (report != null)
            {
                string upState = string.Empty;
                if (report.UpState == BLL.Const.UpState_3)
                {
                    upState = "(已上报)";
                }
                else
                {
                    upState = "(未上报)";
                }
                this.SimpleForm1.Title = "实业安全简报" + report.YearId + "年 " + report.Month + "月" + upState;
                lbUnitName.Text = "填报企业:" + BLL.UnitService.GetUnitNameByUnitId(report.UnitId);
                lbCompileMan.Text = "编制人:" + BLL.UserService.GetUserNameByUserId(report.CompileMan);
                lbAuditor.Text = "审核人:" + report.Auditor;
                lbApprover.Text = "审批人:" + report.Approver;
                this.txtMainWork.Text = report.MainWork;
                this.txtHazardRecording.Text = report.HazardRecording;
                this.txtSafetyAccident.Text = report.SafetyAccident;
                this.txtEPAccident.Text = report.EPAccident;
                this.txtOHAccident.Text = report.OHAccident;
                this.txtWorkPermitNum.Text = report.WorkPermitNum;
                this.txtHiddenDangerDetection.Text = report.HiddenDangerDetection;
                this.txtRectificationSituation.Text = report.RectificationSituation;
                this.txtCheckProblemsRectification.Text = report.CheckProblemsRectification;
                this.txtPendingProject.Text = report.PendingProject;
                this.txtContractorManagement.Text = report.ContractorManagement;
                this.txtEnvironmentalEmissions.Text = report.EnvironmentalEmissions;
                this.txtNextMonthWorkPlan.Text = report.NextMonthWorkPlan;
            }
            else
            {
                SetEmpty();
            }
            this.GetButtonPower();
        }
        #endregion
        #region 关闭弹出窗口
        /// 
        /// 关闭窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            GetValue();
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.SafetyBriefingMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNew.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnEdit.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnDelete.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnSaveUp))
                {
                    this.btnUpdata.Hidden = false;
                }
            }
            var getReport = BLL.SafetyBriefingService.GetSafetyBriefingByUnitIdAndYearAndMonth(drpUnit.SelectedValue, Funs.GetNewIntOrZero(drpYear.SelectedValue), Funs.GetNewIntOrZero(drpMonth.SelectedValue));
            if (getReport != null)
            {
                this.btnNew.Hidden = true;
            }
        }
        #endregion
        #region 增加、修改、上报、删除
        /// 
        /// 增加
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("SafetyBriefingEdit.aspx?UnitId={0}&&Year={1}&&Months={2}", this.CurrUser.UnitId, this.drpYear.SelectedValue, this.drpMonth.SelectedValue, "编辑 - ")));
        }
        /// 
        /// 修改
        /// 
        /// 
        /// 
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Model.Information_SafetyBriefing report = BLL.SafetyBriefingService.GetSafetyBriefingByUnitIdAndYearAndMonth(drpUnit.SelectedValue, Funs.GetNewIntOrZero(drpYear.SelectedValue), Funs.GetNewIntOrZero(drpMonth.SelectedValue));
            if (report != null)
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("SafetyBriefingEdit.aspx?safetyBriefingId={0}", report.SafetyBriefingId, "编辑 - ")));
            }
            else
            {
                ShowNotify("所选时间无报表记录!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 上报
        /// 
        /// 
        /// 
        protected void btnUpdata_Click(object sender, EventArgs e)
        {
            Model.Information_SafetyBriefing report = BLL.SafetyBriefingService.GetSafetyBriefingByUnitIdAndYearAndMonth(drpUnit.SelectedValue, Funs.GetNewIntOrZero(drpYear.SelectedValue), Funs.GetNewIntOrZero(drpMonth.SelectedValue));
            if (report != null)
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("SafetyBriefingEdit.aspx?safetyBriefingId={0}", report.SafetyBriefingId, "编辑 - ")));
            }
            else
            {
                ShowNotify("所选时间无报表记录!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Model.Information_SafetyBriefing report = BLL.SafetyBriefingService.GetSafetyBriefingByUnitIdAndYearAndMonth(drpUnit.SelectedValue, Funs.GetNewIntOrZero(drpYear.SelectedValue), Funs.GetNewIntOrZero(drpMonth.SelectedValue));
            if (report != null)
            {
                BLL.LogService.AddSys_Log(this.CurrUser, report.YearId.ToString() + "-" + report.Month.ToString(), report.SafetyBriefingId, BLL.Const.SafetyBriefingMenuId, BLL.Const.BtnDelete);
                BLL.SafetyBriefingService.DeleteSafetyBriefingById(report.SafetyBriefingId);
                SetEmpty();
                this.btnNew.Hidden = false;
                ShowNotify("删除数据成功!", MessageBoxIcon.Success);
            }
            else
            {
                ShowNotify("所选时间无报表记录!");
            }
        }
        #endregion
        
        #region 单位下拉选择事件
        /// 
        /// 单位下拉框联动事件
        /// 
        /// 
        /// 
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            var units = BLL.UnitService.GetUnitDropDownList();
            if (units != null && !string.IsNullOrEmpty(this.drpUnit.SelectedText))
            {
                var unit = units.FirstOrDefault(x => x.UnitName == this.drpUnit.SelectedText);
                if (unit != null)
                {
                    drpUnit.SelectedValue = unit.UnitId;
                }
            }
            GetValue();
        }
        #endregion
        #region 月份向前/向后
        /// 
        /// 前一个月
        /// 
        /// 
        /// 
        protected void BtnBulletLeft_Click(object sender, EventArgs e)
        {
            SetMonthChange("-");
        }
        /// 
        /// 后一个月
        /// 
        /// 
        /// 
        protected void BulletRight_Click(object sender, EventArgs e)
        {
            SetMonthChange("+");
        }
        /// 
        /// 月份加减变化
        /// 
        /// 
        private void SetMonthChange(string type)
        {
            DateTime? nowDate = Funs.GetNewDateTime(this.drpYear.SelectedValue + "-" + this.drpMonth.SelectedValue);
            if (nowDate.HasValue)
            {
                DateTime showDate = new DateTime();
                if (type == "+")
                {
                    showDate = nowDate.Value.AddMonths(1);
                }
                else
                {
                    showDate = nowDate.Value.AddMonths(-1);
                }
                this.drpYear.SelectedValue = showDate.Year.ToString();
                drpMonth.SelectedValue = showDate.Month.ToString();
                ///值变化
                GetValue();
            }
        }
        #endregion
        #region 查看未上报的项目
        /// 
        /// 查看未上报的项目
        /// 
        /// 
        /// 
        protected void btnView_Click(object sender, EventArgs e)
        {
            string info = string.Empty;
            DateTime date = Convert.ToDateTime(this.drpYear.SelectedValue + "-" + this.drpMonth.SelectedValue + "-01").AddDays(-1).AddMonths(1);
            var projects = (from x in Funs.DB.Base_Project
                            where (x.ProjectState == BLL.Const.ProjectState_1 || x.ProjectState == null)
                            && x.StartDate <= date
                            select x).ToList();
            foreach (var item in projects)
            {
                var report = Funs.DB.InformationProject_SafetyBriefing.FirstOrDefault(x => x.ProjectId == item.ProjectId && x.YearId == date.Year && x.Month == date.Month);
                if (report == null)
                {
                    info += item.ProjectCode + ":" + item.ProjectName + ",未填写报表;";
                }
            }
            if (!string.IsNullOrEmpty(info))
            {
                Alert.ShowInTop(info + "项目报表未上报。", MessageBoxIcon.Warning);
            }
            else
            {
                ShowNotify("项目报表已上报", MessageBoxIcon.Success);
            }
        }
        #endregion
    }
}