using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.TestRunPerformance
{
    public partial class TestRunMonthSummaryReportEdit : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string Id = Request.Params["TestRunMonthSummaryReportId"];
                Model.SGGLDB db = Funs.DB;
                var userIds = (from x in db.Person_TestRunMonthSummary select x.UserId).Distinct().ToList();
                var users = from x in db.Sys_User where userIds.Contains(x.UserId) select x;
                this.drpUser.DataTextField = "UserName";
                this.drpUser.DataValueField = "UserId";
                this.drpUser.DataSource = users;
                this.drpUser.DataBind();
                Funs.FineUIPleaseSelect(this.drpUser);
                this.drpUser.SelectedValue = this.CurrUser.UserId;
                Model.ZHGL_TestRunMonthSummaryReport report = BLL.TestRunMonthSummaryReportService.GetTestRunMonthSummaryReportById(Id);
                if (report != null)
                {
                    if (!string.IsNullOrEmpty(report.UserId))
                    {
                        this.drpUser.SelectedValue = report.UserId;
                    }
                    if (report.Year != null)
                    {
                        this.txtYear.Text = report.Year.ToString();
                    }
                    if (this.drpUser.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.txtYear.Text.Trim()))
                    {
                        BindGrid();
                    }
                }
            }
        }
        #endregion
        private void BindGrid()
        {
            Model.SGGLDB db = Funs.DB;
            DateTime startDate = Convert.ToDateTime(this.txtYear.Text + "-01" + "-01");
            DateTime endDate = startDate.AddYears(1);
            var projects = from x in db.Base_Project select x;
            var getTestRunMonthSummarys = (from x in db.Person_TestRunMonthSummary
                                           join y in db.Sys_User on x.UserId equals y.UserId
                                           where x.UserId == this.drpUser.SelectedValue && x.RaiseDate >= startDate && x.RaiseDate < endDate
                                           orderby x.ProjectId
                                           select new { x.TestRunMonthSummaryId, x.ProjectId, x.UserId, y.UserName, x.Major, x.ProcessName, x.RaiseDate, x.ProblemDescription, x.HandleMethod, x.ExperienceOrSuggestion }).ToList();
            if (getTestRunMonthSummarys.Count() > 0)
            {
                DataTable table = new DataTable();
                DateTime startMonth;
                List months = new List();
                startMonth = startDate;
                do
                {
                    months.Add(startMonth);
                    startMonth = startMonth.AddMonths(1);
                } while (startMonth < endDate);
                table.Columns.Add(new DataColumn("Id", typeof(String)));
                table.Columns.Add(new DataColumn("Code", typeof(String)));
                table.Columns.Add(new DataColumn("ProjectName", typeof(String)));
                table.Columns.Add(new DataColumn("Major", typeof(String)));
                table.Columns.Add(new DataColumn("ProcessName", typeof(String)));
                table.Columns.Add(new DataColumn("RaiseDate", typeof(String)));
                table.Columns.Add(new DataColumn("ProblemDescription", typeof(String)));
                table.Columns.Add(new DataColumn("HandleMethod", typeof(String)));
                table.Columns.Add(new DataColumn("ExperienceOrSuggestion", typeof(String)));
                DataRow row;
                int a = 1;
                for (int i = 0; i < months.Count; i++)
                {
                    row = table.NewRow();
                    row["Id"] = SQLHelper.GetNewID();
                    row["Code"] = GetNum(i + 1);
                    row["ProjectName"] = months[i].Year + "年" + months[i].Month + "月份开车技术总结";
                    DateTime monthEndDate = months[i].AddMonths(1);
                    var monthList = getTestRunMonthSummarys.Where(x => x.RaiseDate >= months[i] && x.RaiseDate < monthEndDate).OrderBy(x => x.RaiseDate);
                    table.Rows.Add(row);
                    a = 1;
                    string projectName = string.Empty;
                    foreach (var item in monthList)
                    {
                        row = table.NewRow();
                        row["Id"] = item.TestRunMonthSummaryId;
                        var project = projects.FirstOrDefault(x => x.ProjectId == item.ProjectId);
                        if (project != null)
                        {
                            if (string.IsNullOrEmpty(projectName) || projectName != project.ProjectName)
                            {
                                row["ProjectName"] = project.ProjectName;
                                projectName = row["ProjectName"].ToString();
                            }
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(projectName) || projectName != "本部")
                            {
                                row["ProjectName"] = "本部";
                                projectName = row["ProjectName"].ToString();
                            }
                        }
                        row["Major"] = item.Major;
                        row["ProcessName"] = item.ProcessName;
                        row["RaiseDate"] = item.RaiseDate;
                        row["ProblemDescription"] = item.ProblemDescription;
                        row["HandleMethod"] = item.HandleMethod;
                        row["ExperienceOrSuggestion"] = item.ExperienceOrSuggestion;
                        table.Rows.Add(row);
                        a++;
                    }
                }
                //rows = rows.Substring(0, rows.Length - 1);
                this.Grid1.DataSource = table;
                this.Grid1.DataBind();
            }
        }
        private string GetNum(int i)
        {
            string num = string.Empty;
            if (i == 1)
            {
                num = "一";
            }
            else if (i == 2)
            {
                num = "二";
            }
            else if (i == 3)
            {
                num = "三";
            }
            else if (i == 4)
            {
                num = "四";
            }
            else if (i == 5)
            {
                num = "五";
            }
            else if (i == 6)
            {
                num = "六";
            }
            else if (i == 7)
            {
                num = "七";
            }
            else if (i == 8)
            {
                num = "八";
            }
            else if (i == 9)
            {
                num = "九";
            }
            else if (i == 10)
            {
                num = "十";
            }
            else if (i == 11)
            {
                num = "十一";
            }
            else if (i == 12)
            {
                num = "十二";
            }
            return num;
        }
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUser.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择开车人员!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(this.txtYear.Text.Trim()))
            {
                Alert.ShowInTop("请选择年份!", MessageBoxIcon.Warning);
                return;
            }
            Save();
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        private void Save()
        {
            string TestRunMonthSummaryReportId = Request.Params["TestRunMonthSummaryReportId"];
            Model.ZHGL_TestRunMonthSummaryReport newItem = new Model.ZHGL_TestRunMonthSummaryReport();
            newItem.Year = Funs.GetNewInt(this.txtYear.Text.Trim());
            newItem.UserId = this.drpUser.SelectedValue;
            newItem.CompileMan = this.CurrUser.UserId;
            newItem.CompileDate = DateTime.Now;
            if (string.IsNullOrEmpty(TestRunMonthSummaryReportId))
            {
                newItem.TestRunMonthSummaryReportId = SQLHelper.GetNewID();
                BLL.TestRunMonthSummaryReportService.AddTestRunMonthSummaryReport(newItem);
                BLL.LogService.AddSys_Log(this.CurrUser, newItem.Year.ToString(), newItem.TestRunMonthSummaryReportId, BLL.Const.TestRunMonthSummaryReportMenuId, "增加开车月技术总结!");
            }
            else
            {
                newItem.TestRunMonthSummaryReportId = TestRunMonthSummaryReportId;
                BLL.TestRunMonthSummaryReportService.UpdateTestRunMonthSummaryReport(newItem);
                BLL.LogService.AddSys_Log(this.CurrUser, newItem.Year.ToString(), newItem.TestRunMonthSummaryReportId, BLL.Const.TestRunMonthSummaryReportMenuId, "修改开车月技术总结!");
            }
        }
        #endregion
        protected void drpUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpUser.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.txtYear.Text.Trim()))
            {
                BindGrid();
            }
            else
            {
                this.Grid1.DataSource = null;
                this.Grid1.DataBind();
            }
        }
        /// 
        /// 获取整改前图片(放于Img中)
        /// 
        /// 
        /// 
        protected string ConvertImageUrlByImage(object TestRunMonthSummaryId)
        {
            string url = string.Empty;
            string httpUrl = string.Empty;
            if (TestRunMonthSummaryId != null)
            {
                var file = BLL.AttachFileService.GetAttachFileByToKeyId(TestRunMonthSummaryId.ToString());
                if (file != null)
                {
                    url = BLL.UploadAttachmentService.ShowImage(Funs.SGGLUrl, file.AttachUrl);
                }
            }
            return url;
        }
        /// 
        /// 获取整改前图片(放于Img中)
        /// 
        /// 
        /// 
        protected string ConvertImageUrlByImage2(object TestRunMonthSummaryId)
        {
            string url = string.Empty;
            string httpUrl = string.Empty;
            if (TestRunMonthSummaryId != null)
            {
                var file = BLL.AttachFileService.GetAttachFileByToKeyId(TestRunMonthSummaryId.ToString() + "R");
                if (file != null)
                {
                    url = BLL.UploadAttachmentService.ShowImage(Funs.SGGLUrl, file.AttachUrl);
                }
            }
            return url;
        }
    }
}