using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;

namespace FineUIPro.Web.common.ProjectSet
{
    public partial class ProjectSysSet : PageBase
    {
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();


                string strSql = @"select * from WxProjectGpsAre  where  ";
                List<SqlParameter> listStr = new List<SqlParameter>();
                strSql += "   ProjectId = @ProjectId";
                listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId)); 
                
                SqlParameter[] parameter = listStr.ToArray();
                DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
                Grid1.RecordCount = tb.Rows.Count;
                tb = GetFilteredTable(Grid1.FilteredData, tb); 

                Grid1.DataSource = tb;
                Grid1.DataBind();
                btnNew.OnClientClick = WindowArea.GetShowReference("ProjectGpsAreEdit.aspx") + "return false;";

                Show(this.CurrUser.LoginProjectId);
            }
        }
        /// <summary>
        /// 关闭弹出窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WindowArea_Close(object sender, WindowCloseEventArgs e)
        {
            string strSql = @"select * from WxProjectGpsAre  where  ";
            List<SqlParameter> listStr = new List<SqlParameter>();
            strSql += "   ProjectId = @ProjectId";
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));

            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);

            Grid1.DataSource = tb;
            Grid1.DataBind();
        }
 

        #region 判断按钮权限
        /// <summary>
        /// 判断按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.CQMSSysSetMenuId, Const.BtnSave))
            {
                this.btnSave.Hidden = false;
            }
        }
        #endregion

        #region   提交按钮
        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string projectId = this.CurrUser.LoginProjectId;
            if (string.IsNullOrEmpty(projectId))
            {
                ShowNotify("请选择项目!", MessageBoxIcon.Warning);
                return;
            }
            #region 通用
            ///通用
            var getProject = ProjectService.GetProjectByProjectId(projectId);
            if (getProject != null)
            {
                getProject.IsFace = this.ckbIsFace.Checked;
                Funs.DB.SubmitChanges();
            }
            #endregion

            #region 焊接
            Model.Project_Sys_Set newSet1 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "1",
                IsAuto = ckbDayReport.Checked,
                SetName = ckbDayReport.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet1);

            Model.Project_Sys_Set newSet6 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "6",
                IsAuto = ckbDayReportDataIn.Checked,
                SetName = ckbDayReportDataIn.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet6);

            Model.Project_Sys_Set newSet2 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "2",
                IsAuto = ckbPoint.Checked,
                SetName = ckbPoint.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet2);

            Model.Project_Sys_Set newSet3 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "3",
                SetName = robStandard.Label
            };
            if (robStandard.SelectedValue == "1")
            {
                newSet3.IsAuto = true;
                newSet3.SetValue = null;
            }
            else if (robStandard.SelectedValue == "2")
            {
                newSet3.IsAuto = false;
                newSet3.SetValue = null;
            }
            else
            {
                newSet3.IsAuto = null;
                newSet3.SetValue = robStandard.SelectedValue;
            }
            Project_SysSetService.SaveProjectSysSet(newSet3);

            Model.Project_Sys_Set newSet4 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "4",
                IsAuto = ckbSupervisor.Checked,
                SetName = ckbSupervisor.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet4);

            Model.Project_Sys_Set newSet5 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "5",
                IsAuto = ckbFilmNum.Checked,
                SetName = ckbFilmNum.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet5);

            Model.Project_Sys_Set newSet7 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "7",
                IsAuto = ckbTrust.Checked,
                SetName = ckbTrust.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet7);

            Model.Project_Sys_Set newSet8 = new Model.Project_Sys_Set
            {
                ProjectId = projectId,
                SetId = "8",
                IsAuto = cbPDMS.Checked,
                SetName = cbPDMS.Label
            };
            Project_SysSetService.SaveProjectSysSet(newSet8);
          
            //this.Show(projectId);
            #endregion

            #region 质量

            Model.Project_Sys_Set CheckEquipmentDay = BLL.Project_SysSetService.GetSysSetBySetName("检试验设备到期提醒天数", this.CurrUser.LoginProjectId);
            if (CheckEquipmentDay != null)
            {

                if (!string.IsNullOrEmpty(this.txtRemindDay.Text.Trim()))
                {
                    CheckEquipmentDay.SetValue = this.txtRemindDay.Text.Trim();
                    BLL.Project_SysSetService.UpdateSet(CheckEquipmentDay);
                }

            }
            else
            {
                if (!string.IsNullOrEmpty(this.txtRemindDay.Text.Trim()))
                {
                    Model.Project_Sys_Set newCheckEquipmentDay = new Model.Project_Sys_Set();
                    newCheckEquipmentDay.SetId = SQLHelper.GetNewID(typeof(Model.Project_Sys_Set));

                    newCheckEquipmentDay.ProjectId = this.CurrUser.LoginProjectId;
                    newCheckEquipmentDay.SetName = "检试验设备到期提醒天数";
                    newCheckEquipmentDay.SetValue = this.txtRemindDay.Text.Trim();
                    BLL.Project_SysSetService.AddSet(newCheckEquipmentDay);
                }
            }
            Model.Project_Sys_Set CheckMonthStartDay = BLL.Project_SysSetService.GetSysSetBySetName("月报开始日期", this.CurrUser.LoginProjectId);
            if (CheckMonthStartDay != null)
            {
                if (!string.IsNullOrEmpty(this.txtStarTime.Text.Trim()))
                {
                    CheckMonthStartDay.SetValue = this.txtStarTime.Text.Trim();
                    BLL.Project_SysSetService.UpdateSet(CheckMonthStartDay);
                }

            }
            else
            {
                if (!string.IsNullOrEmpty(this.txtStarTime.Text.Trim()))
                {
                    Model.Project_Sys_Set newCheckEquipmentDay = new Model.Project_Sys_Set();
                    newCheckEquipmentDay.SetId = SQLHelper.GetNewID(typeof(Model.Project_Sys_Set));
                    newCheckEquipmentDay.ProjectId = this.CurrUser.LoginProjectId;
                    newCheckEquipmentDay.SetName = "月报开始日期";
                    newCheckEquipmentDay.SetValue = this.txtStarTime.Text.Trim();
                    BLL.Project_SysSetService.AddSet(newCheckEquipmentDay);
                }
            }
            Model.Project_Sys_Set CheckMonthEndDay = BLL.Project_SysSetService.GetSysSetBySetName("月报结束日期", this.CurrUser.LoginProjectId);
            if (CheckMonthEndDay != null)
            {
                if (!string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
                {
                    CheckMonthStartDay.SetValue = this.txtEndTime.Text.Trim();
                    BLL.Project_SysSetService.UpdateSet(CheckMonthStartDay);
                }

            }
            else
            {
                if (!string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
                {
                    Model.Project_Sys_Set newCheckEquipmentDay = new Model.Project_Sys_Set();
                    newCheckEquipmentDay.SetId = SQLHelper.GetNewID(typeof(Model.Project_Sys_Set));
                    newCheckEquipmentDay.ProjectId = this.CurrUser.LoginProjectId;
                    newCheckEquipmentDay.SetName = "月报结束日期";
                    newCheckEquipmentDay.SetValue = this.txtEndTime.Text.Trim();
                    BLL.Project_SysSetService.AddSet(newCheckEquipmentDay);
                }
            }

            #endregion

            #region 进度
            Model.Project_Sys_Set weightsSet = BLL.Project_SysSetService.GetSysSetBySetId("21", this.CurrUser.LoginProjectId);
            if (weightsSet != null)
            {
                if (!string.IsNullOrEmpty(this.rbWeightsSet.SelectedValue))
                {
                    weightsSet.SetValue = this.rbWeightsSet.SelectedValue;
                    BLL.Project_SysSetService.UpdateSet(weightsSet);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(this.rbWeightsSet.SelectedValue))
                {
                    Model.Project_Sys_Set newWeightsSet = new Model.Project_Sys_Set();
                    newWeightsSet.SetId = "21";
                    newWeightsSet.ProjectId = this.CurrUser.LoginProjectId;
                    newWeightsSet.SetName = "进度测量标志(权重)批量审核设置";
                    newWeightsSet.SetValue = this.rbWeightsSet.SelectedValue;
                    BLL.Project_SysSetService.AddSet(newWeightsSet);
                }
            }

            Model.Project_Sys_Set completeAndRealSet = BLL.Project_SysSetService.GetSysSetBySetId("22", this.CurrUser.LoginProjectId);
            if (completeAndRealSet != null)
            {
                if (!string.IsNullOrEmpty(this.nbCompleteAndReal.Text.Trim()))
                {
                    completeAndRealSet.SetValue = this.nbCompleteAndReal.Text.Trim();
                    BLL.Project_SysSetService.UpdateSet(completeAndRealSet);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(this.nbCompleteAndReal.Text.Trim()))
                {
                    Model.Project_Sys_Set newCompleteAndRealSet = new Model.Project_Sys_Set();
                    newCompleteAndRealSet.SetId = "22";
                    newCompleteAndRealSet.ProjectId = this.CurrUser.LoginProjectId;
                    newCompleteAndRealSet.SetName = "完成值/实耗值允许补录月份数";
                    newCompleteAndRealSet.SetValue = this.nbCompleteAndReal.Text.Trim();
                    BLL.Project_SysSetService.AddSet(newCompleteAndRealSet);
                }
            }

            #endregion

            //BLL.Sys_LogService.AddLog(BLL.Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "提交项目环境设置");
            Alert.ShowInTop("提交成功!", MessageBoxIcon.Success);
        }
        #endregion

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string itemId = Grid1.DataKeys[e.RowIndex][0].ToString();

            if (e.CommandName == "MapPoint")
            {

                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ProjectSetMap.aspx?GpsAreId={0}&value=0", itemId, "查看 - ")));
            }
            else if (e.CommandName == "Edit")
            {
                PageContext.RegisterStartupScript(WindowArea.GetShowReference("ProjectGpsAreEdit.aspx?GpsAreId=" + itemId));

            }
            else if(e.CommandName == "Del") 
            {
                ProjectSetMapService.DelProjectGpsAre(itemId);
                string strSql = @"select * from WxProjectGpsAre  where  ";
                List<SqlParameter> listStr = new List<SqlParameter>();
                strSql += "   ProjectId = @ProjectId";
                listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));

                SqlParameter[] parameter = listStr.ToArray();
                DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
                Grid1.RecordCount = tb.Rows.Count;
                tb = GetFilteredTable(Grid1.FilteredData, tb);

                Grid1.DataSource = tb;
                Grid1.DataBind();
            }

        }

        #region 页面呈现
        /// <summary>
        /// 
        /// </summary>
        private void Show(string projectId)
        {
            ///通用
            var getProject = ProjectService.GetProjectByProjectId(projectId);
            if (getProject != null && getProject.IsFace == true)
            {
                this.ckbIsFace.Checked = true;
            }

            var q = from x in Funs.DB.Project_Sys_Set where x.ProjectId == projectId select x;
            if (q.Count() > 0)
            {
                foreach (var s in q)
                {
                    if (s.SetId == "1")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbDayReport.Checked = true;
                        }
                        else
                        {
                            this.ckbDayReport.Checked = false;
                        }
                    }

                    if (s.SetId == "2")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbPoint.Checked = true;
                        }
                        else
                        {
                            this.ckbPoint.Checked = false;
                        }
                    }

                    if (s.SetId == "3")
                    {
                        if (s.IsAuto == true)
                        {
                            this.robStandard.SelectedValue = "1";
                        }
                        if (s.IsAuto == false)
                        {
                            this.robStandard.SelectedValue = "2";
                        }
                        if (s.SetValue == "3")
                        {
                            this.robStandard.SelectedValue = "3";
                        }
                        if (s.SetValue == "4")
                        {
                            this.robStandard.SelectedValue = "4";
                        }
                    }

                    if (s.SetId == "4")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbSupervisor.Checked = true;
                        }
                        else
                        {
                            this.ckbSupervisor.Checked = false;
                        }
                    }

                    if (s.SetId == "5")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbFilmNum.Checked = true;
                        }
                        else
                        {
                            this.ckbFilmNum.Checked = false;
                        }
                    }

                    if (s.SetId == "6")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbDayReportDataIn.Checked = true;
                        }
                        else
                        {
                            this.ckbDayReportDataIn.Checked = false;
                        }
                    }

                    if (s.SetId == "7")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbTrust.Checked = true;
                        }
                        else
                        {
                            this.ckbTrust.Checked = false;
                        }
                    }
                    if (s.SetId == "8")
                    {
                        if (s.IsAuto == true)
                        {
                            this.cbPDMS.Checked = true;
                        }
                        else
                        {
                            this.cbPDMS.Checked = false;
                        }
                    }
                }
            }

            ///质量页面呈现
            Model.Project_Sys_Set CheckEquipmentDay = BLL.Project_SysSetService.GetSysSetBySetName("检试验设备到期提醒天数", this.CurrUser.LoginProjectId);
            if (CheckEquipmentDay != null)
            {
                this.txtRemindDay.Text = CheckEquipmentDay.SetValue;
            }
            Model.Project_Sys_Set CheckMonthStartDay = BLL.Project_SysSetService.GetSysSetBySetName("月报开始日期", this.CurrUser.LoginProjectId);
            if (CheckMonthStartDay != null)
            {
                this.txtStarTime.Text = CheckMonthStartDay.SetValue;
            }
            else
            {
                this.txtStarTime.Text = "25";
            }
            Model.Project_Sys_Set CheckMonthEndDay = BLL.Project_SysSetService.GetSysSetBySetName("月报结束日期", this.CurrUser.LoginProjectId);
            if (CheckMonthEndDay != null)
            {
                this.txtEndTime.Text = CheckMonthEndDay.SetValue;
            }
            else
            {
                this.txtEndTime.Text = "24";
            }

            //进度页面呈现
            Model.Project_Sys_Set weightsSet = BLL.Project_SysSetService.GetSysSetBySetId("21", this.CurrUser.LoginProjectId);
            if (weightsSet != null)
            {
                this.rbWeightsSet.SelectedValue = weightsSet.SetValue;
            }
            else
            {
                this.rbWeightsSet.SelectedValue = "";
            }

            Model.Project_Sys_Set completeAndRealSet = BLL.Project_SysSetService.GetSysSetBySetId("22", this.CurrUser.LoginProjectId);
            if (completeAndRealSet != null)
            {
                this.nbCompleteAndReal.Text = completeAndRealSet.SetValue;
            }
            else
            {
                this.nbCompleteAndReal.Text = "";
            }
        }
        #endregion

        protected void btnSetMap_Click(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
            { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ProjectSetMap.aspx?projectId={0}&value=0", this.CurrUser.LoginProjectId, "查看 - "))); }
        }
    }
}