using BLL;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WorkloadStatistics : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.drpCnProfession.DataValueField = "CnProfessionId";
                this.drpCnProfession.DataTextField = "CnProfessionName";
                this.drpCnProfession.DataSource = BLL.CnProfessionInitService.GetCnProfessionInitDropDownList();
                this.drpCnProfession.DataBind();
                Funs.FineUIPleaseSelect(drpCnProfession);
                Funs.FineUIPleaseSelect(drpUnitProject);
                Funs.FineUIPleaseSelect(drpWbsSet);
                this.txtMonths.Text = string.Format("{0:yyyy-MM}", DateTime.Now);
                //Funs.FineUIPleaseSelect(this.drpWeek);
                //InitWeek();
            }
        }
        #endregion
        //private void InitWeek()
        //{
        //    if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
        //    {
        //        this.Grid1.Columns[5].HeaderText = "本月计划完成量(WS)";
        //        this.Grid1.Columns[6].HeaderText = "本月实际完成量(WP)";
        //        this.Grid1.Columns[8].HeaderText = "本月已完工作实际费用-ACWP";
        //        this.Grid1.Columns[9].HeaderText = "本月计划工作预算费用-BCWS";
        //        this.Grid1.Columns[10].HeaderText = "本月已完工作预算费用-BCWP";
        //        this.drpWeek.Items.Clear();
        //        DateTime months = Convert.ToDateTime(this.txtMonths.Text.Trim() + "-01");
        //        string costControlId = BLL.CostControlDetailService.GetHasWeekPlanCostControlIdByProjectIdAndMonth(this.CurrUser.LoginProjectId, months);
        //        if (!string.IsNullOrEmpty(costControlId))
        //        {
        //            BLL.CostControlDetailService.InitWeekPlanList(this.drpWeek, costControlId, months, true);
        //            this.drpWeek.SelectedValue = BLL.Const._Null;
        //        }
        //        else
        //        {
        //            Funs.FineUIPleaseSelect(this.drpWeek);
        //            this.drpWeek.SelectedValue = BLL.Const._Null;
        //        }
        //    }
        //}
        //#region  月份选择事件
        ///// 
        ///// 月份选择事件
        ///// 
        ///// 
        ///// 
        //protected void txtMonths_TextChanged(object sender, EventArgs e)
        //{
        //    InitWeek();
        //}
        //#endregion
        #region 查询
        /// 
        /// 查询按钮
        /// 
        /// 
        /// 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                DateTime months = Convert.ToDateTime(this.txtMonths.Text.Trim());
                //if (this.rblStatisticsType.SelectedValue == "1")
                //{
                DataTable table = BLL.WorkloadStatisticsService.GetTreeDataTable(this.CurrUser.LoginProjectId, months, BLL.Const._Null, this.drpCnProfession.SelectedValue, this.drpUnitProject.SelectedValue, this.drpWbsSet.SelectedValue, this.txtEquipmentMaterialType.Text.Trim());
                Grid1.DataSource = table;
                Grid1.DataBind();
                //}
                //else
                //{
                //    if (this.drpCnProfession.SelectedValue != BLL.Const._Null)
                //    {
                //        DataTable table = BLL.WorkloadStatisticsService.GetTreeDataTable(this.CurrUser.LoginProjectId, months, this.drpCnProfession.SelectedValue, this.drpUnitProject.SelectedValue);
                //        Grid1.DataSource = table;
                //        Grid1.DataBind();
                //    }
                //    else
                //    {
                //        Alert.ShowInTop("请选择专业!", MessageBoxIcon.Warning);
                //    }
                //}
            }
            else
            {
                Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 推送数据
        /// 
        /// 推送数据按钮
        /// 
        /// 
        /// 
        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                DateTime month = Convert.ToDateTime(this.txtMonths.Text.Trim());
                //基准 = 施工所有计划费用
                //本月PV = 本月预算费用 / 基准
                //本月EV = 本月已完成工作的预算费用 / 基准
                //本月AC = 本月实际费用 / 基准
                //累计PV = 累计预算费用 / 基准
                //累计EV = 累计已完成工作的预算费用 / 基准
                //累计AC = 累计实际费用 / 基准
                var totalCostControl = (from x in Funs.DB.WBS_CostControl where x.ProjectId == this.CurrUser.LoginProjectId && x.TotalNum != null select x);
                double baseMoney = Convert.ToDouble(totalCostControl.Sum(x => (x.TotalNum ?? 0) * (x.PlanPrice ?? 0)));
                Model.Project_Installation installation = BLL.Project_InstallationService.GetProjectInstallationByProjectId(this.CurrUser.LoginProjectId);
                if (installation != null)
                {
                    var thisDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailViewByParentIdAndMonths(installation.InstallationId, month);
                    if (thisDetail != null)
                    {
                        var getPro = new
                        {
                            projId = 2370,
                            projName = "",
                            ppsId = 3,
                            ppsName = "施工",
                            performanceDate = this.txtMonths.Text.Trim() + "-25",
                            pv = Math.Round((thisDetail.ThisPlanValue ?? 0) / baseMoney, 4),
                            ev = Math.Round((thisDetail.ThisPlanCost ?? 0) / baseMoney, 4),
                            ac = Math.Round((thisDetail.ThisRealCost ?? 0) / baseMoney, 4),
                            totalPv = Math.Round((thisDetail.TotalPlanValue ?? 0) / baseMoney, 4),
                            totalEv = Math.Round((thisDetail.TotalPlanCost ?? 0) / baseMoney, 4),
                            totalAc = Math.Round((thisDetail.TotalRealCost ?? 0) / baseMoney, 4),
                            remark = ""
                        };
                        string contenttype = "application/json;charset=utf-8";
                        string parm = JsonConvert.SerializeObject(getPro);
                        var returndata = BLL.APIGetHttpService.ControlHttp(Funs.ControlApiUrl + "/Projects/SaveProjConstructionPerformance", "POST", contenttype, null, parm);
                    }
                }
            }
            else
            {
                Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 删除数据
        /// 
        /// 删除数据按钮
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                DateTime month = Convert.ToDateTime(this.txtMonths.Text.Trim());
                var getPro = new
                {
                    id = "9216a478-5f2f-4905-b7a0-08d99532e685"
                };
                string contenttype = "application/json;charset=utf-8";
                string parm = JsonConvert.SerializeObject(getPro);
                //var returndata = BLL.APIGetHttpService.ControlHttp(Funs.ControlApiUrl + "/Projects/DeleteProjConstructionPerformance", "DELETE", contenttype, null, parm);
                var returndata = BLL.APIGetHttpService.ControlHttp(Funs.ControlApiUrl + "/Projects/DeleteProjConstructionPerformance?id=9216a478-5f2f-4905-b7a0-08d99532e685", "DELETE", contenttype, null, null);
            }
            else
            {
                Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 查询数据
        /// 
        /// 查询数据按钮
        /// 
        /// 
        /// 
        protected void btnCheck_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                DateTime month = Convert.ToDateTime(this.txtMonths.Text.Trim());
                var getPro = new
                {
                    id = "9216a478-5f2f-4905-b7a0-08d99532e685"
                };
                string contenttype = "application/json;charset=utf-8";
                string parm = JsonConvert.SerializeObject(getPro);
                //var returndata = BLL.APIGetHttpService.ControlHttp(Funs.ControlApiUrl + "/Projects/DeleteProjConstructionPerformance", "DELETE", contenttype, null, parm);
                var returndata = BLL.APIGetHttpService.ControlHttp(Funs.ControlApiUrl + "/Projects/GetProjConstructionPerformanceList?id=793f2e98-32f5-4141-0f91-08d9975cc748", "GET", contenttype, null, null);
            }
            else
            {
                Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 统计类型选择事件
        /// 
        /// 统计类型选择事件
        /// 
        /// 
        /// 
        protected void rblStatisticsType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (this.rblStatisticsType.SelectedValue == "1")
            //{
            //    this.drpCnProfession.Hidden = true;
            //    this.drpUnitProject.Hidden = true;
            //    this.drpCnProfession.SelectedValue = BLL.Const._Null;
            //}
            //else if (this.rblStatisticsType.SelectedValue == "2")
            //{
            //    this.drpCnProfession.Hidden = false;
            //}
        }
        #endregion
        protected void drpCnProfession_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpCnProfession.SelectedValue != BLL.Const._Null)
            {
                this.drpUnitProject.DataValueField = "UnitProjectCode";
                this.drpUnitProject.DataTextField = "UnitProjectName";
                this.drpUnitProject.DataSource = BLL.UnitProjectInitService.GetUnitProjectInitsByCnProfessionId(this.drpCnProfession.SelectedValue);
                this.drpUnitProject.DataBind();
                Funs.FineUIPleaseSelect(drpUnitProject);
                this.drpUnitProject.SelectedValue = BLL.Const._Null;
                this.drpWbsSet.Items.Clear();
                Funs.FineUIPleaseSelect(drpWbsSet);
                this.drpWbsSet.SelectedValue = BLL.Const._Null;
            }
            else
            {
                this.drpUnitProject.Items.Clear();
                Funs.FineUIPleaseSelect(drpUnitProject);
                this.drpUnitProject.SelectedValue = BLL.Const._Null;
                this.drpWbsSet.Items.Clear();
                Funs.FineUIPleaseSelect(drpWbsSet);
                this.drpWbsSet.SelectedValue = BLL.Const._Null;
            }
        }
        protected void drpUnitProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpUnitProject.SelectedValue != BLL.Const._Null)
            {
                this.drpWbsSet.DataValueField = "WbsSetCode";
                this.drpWbsSet.DataTextField = "WbsSetName";
                this.drpWbsSet.DataSource = BLL.WbsSetInitService.GetWbsSetInitsByUnitProjectCode(this.drpUnitProject.SelectedValue);
                this.drpWbsSet.DataBind();
                Funs.FineUIPleaseSelect(drpWbsSet);
                this.drpWbsSet.SelectedValue = BLL.Const._Null;
            }
            else
            {
                this.drpWbsSet.Items.Clear();
                Funs.FineUIPleaseSelect(drpWbsSet);
                this.drpWbsSet.SelectedValue = BLL.Const._Null;
            }
        }
        #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 = Encoding.UTF8;
            this.Grid1.PageSize = Grid1.RecordCount;
            Response.Write(GetGridTableHtml2(Grid1));
            Response.End();
            DateTime months = Convert.ToDateTime(this.txtMonths.Text.Trim());
            DataTable table = BLL.WorkloadStatisticsService.GetTreeDataTable(this.CurrUser.LoginProjectId, months, BLL.Const._Null, this.drpCnProfession.SelectedValue, this.drpUnitProject.SelectedValue, this.drpWbsSet.SelectedValue, this.txtEquipmentMaterialType.Text.Trim());
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #endregion
        //protected void drpWeek_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (this.drpWeek.SelectedValue == BLL.Const._Null)
        //    {
        //        this.Grid1.Columns[5].HeaderText = "本月计划完成量(WS)";
        //        this.Grid1.Columns[6].HeaderText = "本月实际完成量(WP)";
        //        this.Grid1.Columns[8].HeaderText = "本月已完工作实际费用-ACWP";
        //        this.Grid1.Columns[9].HeaderText = "本月计划工作预算费用-BCWS";
        //        this.Grid1.Columns[10].HeaderText = "本月已完工作预算费用-BCWP";
        //    }
        //    else
        //    {
        //        this.Grid1.Columns[5].HeaderText = "本周计划完成量(WS)";
        //        this.Grid1.Columns[6].HeaderText = "本周实际完成量(WP)";
        //        this.Grid1.Columns[8].HeaderText = "本周已完工作实际费用-ACWP";
        //        this.Grid1.Columns[9].HeaderText = "本周计划工作预算费用-BCWS";
        //        this.Grid1.Columns[10].HeaderText = "本周已完工作预算费用-BCWP";
        //    }
        //}
    }
}