using System;
using BLL;
using Model;

namespace FineUIPro.Web.common.BaseInfo
{
    public partial class ProjectEdit : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                string projectId = Request.QueryString["ProjectId"];

                if (!String.IsNullOrEmpty(projectId))
                {
                    var q = BLL.Base_ProjectService.GetProjectByProjectId(projectId);
                    if (q != null)
                    {
                        txtProjectCode.Text = q.ProjectCode;
                        txtProjectName.Text = q.ProjectName;
                        txtEnProjectName.Text = q.EnProjectName;
                        txtShortName.Text = q.ShortName;
                        txtProjectAddress.Text = q.ProjectAddress;

                        if (q.StartDate.HasValue)
                        {
                            txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", q.StartDate);
                        }
                        if (q.EndDate.HasValue)
                        {
                            txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", q.EndDate);
                        }
                       
                        this.txtProjectPrincipal.Text = q.ProjectPrincipal;
                        this.txtConstructionPrincipal.Text = q.ConstructionPrincipal;
                        txtRemark.Text = q.Remark;

                        if (q.IsClosed.HasValue)
                        {
                            this.ckbIsClosed.Checked = q.IsClosed.Value;
                        }

                        
                    }
                }
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.ProjectMenuId, Const.BtnSave))
            {
                Model.Base_Project project = new Base_Project();
                string projectId = Request.QueryString["ProjectId"];

                project.ProjectCode = txtProjectCode.Text.Trim();
                project.ProjectName = txtProjectName.Text.Trim();
                project.EnProjectName = txtEnProjectName.Text.Trim();
                project.ShortName = txtShortName.Text.Trim();
                project.ProjectAddress = txtProjectAddress.Text.Trim();
                if (!string.IsNullOrEmpty(txtStartDate.Text.Trim()))
                {
                    project.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                }
                if (!string.IsNullOrEmpty(txtEndDate.Text.Trim()))
                {
                    project.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                }
               
                project.ProjectPrincipal = this.txtProjectPrincipal.Text.Trim();
                project.ConstructionPrincipal = this.txtConstructionPrincipal.Text.Trim();
                project.IsClosed = ckbIsClosed.Checked;
                project.Remark = txtRemark.Text.Trim();
                project.ProjectArea = this.CurrUser.LoginProjectArea;

                if (String.IsNullOrEmpty(projectId))
                {
                    string newProjectId = SQLHelper.GetNewID(typeof(Model.Base_Project));
                    project.ProjectId = newProjectId;
                    if (!BLL.Base_ProjectService.IsExistProjectCode(newProjectId, txtProjectCode.Text.Trim()))
                    {
                        BLL.Base_ProjectService.AddProject(project);

                        // 给新项目拷贝一份系统环境设置
                        string setSql = "insert into dbo.Sys_Set (SetId,SetName,IsAuto,SetValue,ProjectId) select SetId,SetName,IsAuto,SetValue,'" + newProjectId + "'  from dbo.Sys_Set where ProjectId='0'";
                        BLL.SQLHelper.ExecutSql(setSql);

                        if (this.CurrUser.UserId != BLL.Const.GlyId)
                        {
                            // 给新项目项目单位插入当前用户单位
                            Model.Project_Unit newUnit = new Model.Project_Unit();
                            newUnit.ProjectId = newProjectId;
                            newUnit.UnitId = this.CurrUser.UnitId;
                            newUnit.InTime = System.DateTime.Now;
                            BLL.Project_UnitService.AddProject_Unit(newUnit);

                            // 给新项目项目用户插入当前用户
                            Model.Project_User newUser = new Model.Project_User();
                            newUser.ProjectId = newProjectId;
                            newUser.UserId = this.CurrUser.UserId;
                            newUser.IsPost = true;

                            BLL.Project_UserService.AddProject_User(newUser);
                        }

                        BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.ProjectMenuId, Const.BtnAdd, newProjectId);
                    }
                    else
                    {
                        Alert.ShowInParent(Resources.Lan.ProjectCodeExists, MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    if (!BLL.Base_ProjectService.IsExistProjectCode(projectId, txtProjectCode.Text.Trim()))
                    {
                        Model.Base_Project oldProject = BLL.Base_ProjectService.GetProjectByProjectId(projectId);
                        project.ProjectId = projectId;
                        BLL.Base_ProjectService.UpdateProject(project);
                        BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.ProjectMenuId, Const.BtnModify, projectId);
                    }
                    else
                    {
                        Alert.ShowInParent(Resources.Lan.ProjectCodeExists, MessageBoxIcon.Warning);
                        return;
                    }
                }
                // 关闭本窗体,然后回发父窗体
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion
    }
}