using System;
using System.Linq;
using BLL;

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

        /// <summary>
        /// 树加载
        /// </summary>
        private void InitTreeMenu()
        {
            this.trWBS.Nodes.Clear();
            this.trWBS.ShowBorder = false;
            this.trWBS.ShowHeader = false;
            this.trWBS.EnableIcons = true;
            this.trWBS.AutoScroll = true;
            this.trWBS.EnableSingleClickExpand = true;
            
            var projects = from x in Funs.DB.Base_Project orderby x.ProjectCode select x;
            if (projects.Count() > 0)
            {
                TreeNode root = null;
                foreach (var q in projects)
                {
                    root = new TreeNode();
                    root.Text = "[" + q.ProjectCode + "]" + q.ProjectName;
                    root.NodeID = q.ProjectId;
                    root.EnableClickEvent = true;
                    this.trWBS.Nodes.Add(root);
                }
            }
        }
        #endregion

        #region   Tree点击事件
        /// <summary>
        /// Tree点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trWBS_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            this.ckbDayReport.Checked = false;
            this.ckbPoint.Checked = false;
            this.robStandard.SelectedValue = null;
            string projectId = this.trWBS.SelectedNodeID;
            Show(projectId);

        }
        #endregion

        #region   提交按钮
        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_SysSetMenuId, Const.BtnSave))
            {
                string projectId = this.trWBS.SelectedNodeID;
                if (projectId == "0" || string.IsNullOrEmpty(projectId))
                {
                    ShowNotify(Resources.Lan.PleaseSelectProject, MessageBoxIcon.Warning);
                    return;
                }

                Model.Sys_Set dayReport = BLL.Sys_SetService.GetSetById("1", projectId);
                Model.Sys_Set point = BLL.Sys_SetService.GetSetById("2", projectId);
                Model.Sys_Set trust = BLL.Sys_SetService.GetSetById("3", projectId);
                Model.Sys_Set checkDay = BLL.Sys_SetService.GetSetById("4", projectId);

                if (checkDay != null)
                {
                    checkDay.SetValue = txtCheckDay.Text.Trim();
                    BLL.Sys_SetService.UpdateSet(checkDay);
                }
                else
                {
                    Model.Sys_Set newcheckDay = new Model.Sys_Set();
                    newcheckDay.SetId = "4";
                    newcheckDay.ProjectId = projectId;
                    newcheckDay.SetName = "检测周期";
                    newcheckDay.SetValue = txtCheckDay.Text.Trim();
                    BLL.Sys_SetService.AddSet(newcheckDay);
                }

                #region 焊接日报号
                if (dayReport != null)
                {
                    if (ckbDayReport.Checked)
                    {
                        dayReport.IsAuto = true;
                    }
                    else
                    {
                        dayReport.IsAuto = false;
                    }
                    BLL.Sys_SetService.UpdateSet(dayReport);
                }
                else
                {
                    Model.Sys_Set newDayReport = new Model.Sys_Set();
                    newDayReport.SetId = "1";
                    newDayReport.ProjectId = projectId;
                    newDayReport.SetName = Resources.Lan.WeldingReportCode;
                    if (ckbDayReport.Checked)
                    {
                        newDayReport.IsAuto = true;
                    }
                    else
                    {
                        newDayReport.IsAuto = false;
                    }
                    BLL.Sys_SetService.AddSet(newDayReport);
                }
                #endregion

                
                #region 点口编号
                if (point != null)
                {
                    if (ckbPoint.Checked)
                    {
                        point.IsAuto = true;
                    }
                    else
                    {
                        point.IsAuto = false;
                    }
                    BLL.Sys_SetService.UpdateSet(point);
                }
                else
                {
                    Model.Sys_Set newPoint = new Model.Sys_Set();
                    newPoint.SetId = "2";
                    newPoint.ProjectId = projectId;
                    newPoint.SetName = Resources.Lan.PointCode;
                    if (ckbPoint.Checked)
                    {
                        newPoint.IsAuto = true;
                    }
                    else
                    {
                        newPoint.IsAuto = false;
                    }
                    BLL.Sys_SetService.AddSet(newPoint);
                }
                #endregion

                #region 无损检测委托单
                if (trust != null)
                {
                    if (robStandard.SelectedValue == "1")
                    {
                        trust.IsAuto = true;
                        trust.SetValue = null;
                    }
                    else if (robStandard.SelectedValue == "2")
                    {
                        trust.IsAuto = false;
                        trust.SetValue = null;
                    }
                    else
                    {
                        trust.IsAuto = null;
                        trust.SetValue = robStandard.SelectedValue;
                    }
                    BLL.Sys_SetService.UpdateSet(trust);
                }
                else
                {
                    Model.Sys_Set newTrust = new Model.Sys_Set();
                    newTrust.ProjectId = projectId;
                    newTrust.SetId = "3";
                    newTrust.SetName = Resources.Lan.NDTRequestSheet;
                    if (robStandard.SelectedValue == "1")
                    {
                        newTrust.IsAuto = true;
                        newTrust.SetValue = null;
                    }
                    else if (robStandard.SelectedValue == "2")
                    {
                        newTrust.IsAuto = false;
                        newTrust.SetValue = null;
                    }
                    else
                    {
                        newTrust.IsAuto = null;
                        newTrust.SetValue = robStandard.SelectedValue;
                    }
                    BLL.Sys_SetService.AddSet(newTrust);
                }
                #endregion
                this.Show(projectId);

                BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_SysSetMenuId, Const.BtnSave, "");
                ShowNotify(Resources.Lan.SaveSuccessfully, MessageBoxIcon.Success);
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region 页面呈现
        /// <summary>
        /// 
        /// </summary>
        private void Show(string projectId)
        {
            var q = from x in BLL.Funs.DB.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;
                        }
                    }
                    else if (s.SetId == "2")
                    {
                        if (s.IsAuto == true)
                        {
                            this.ckbPoint.Checked = true;
                        }
                        else
                        {
                            this.ckbPoint.Checked = false;
                        }
                    }
                    else if (s.SetId == "3")
                    {
                        if (s.IsAuto == true)
                        {
                            this.robStandard.SelectedValue = "1";
                        }
                        if (s.IsAuto == false)
                        {
                            this.robStandard.SelectedValue = "2";
                        }
                    }
                    else if (s.SetId == "4")
                    {
                        txtCheckDay.Text = s.SetValue;
                    }
                }
            }
        }
        #endregion
    }
}