using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
namespace FineUIPro.Web.Party
{
    public partial class PartyerEdit : PageBase
    {
        #region 定义项
        /// 
        /// 用户主键
        /// 
        public string PartyerId
        {
            get
            {
                return (string)ViewState["PartyerId"];
            }
            set
            {
                ViewState["PartyerId"] = value;
            }
        }
        /// 
        /// 签名附件路径
        /// 
        public string SignatureUrl
        {
            get
            {
                return (string)ViewState["SignatureUrl"];
            }
            set
            {
                ViewState["SignatureUrl"] = value;
            }
        }
        #endregion
        /// 
        /// 用户编辑页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                ///权限
                this.GetButtonPower();
                this.PartyerId = Request.Params["partyerId"];
                if (!string.IsNullOrEmpty(this.PartyerId))
                {
                    var partyer = BLL.PartyerService.GetPartyerById(this.PartyerId);
                    if (partyer != null)
                    {
                        this.txtName.Text = partyer.Name;
                        this.txtSex.Text = partyer.Sex;
                        if (partyer.BirthDate != null)
                        {
                            this.txtBirthDate.Text = string.Format("{0:yyyy-MM}",partyer.BirthDate);
                        }
                        this.txtEducation.Text = partyer.Education;
                        this.txtNation.Text = partyer.Nation;
                        if (partyer.JoinPartyDate != null)
                        {
                            this.txtJoinPartyDate.Text = string.Format("{0:yyyy-MM-dd}", partyer.JoinPartyDate);
                        }
                        if (partyer.JoinPostDate != null)
                        {
                            this.txtJoinPostDate.Text = string.Format("{0:yyyy-MM-dd}", partyer.JoinPostDate);
                        }
                        this.txtPost.Text = partyer.Post;
                        this.txtPhone.Text = partyer.Phone;
                        if (partyer.PartyRelationInDate != null)
                        {
                            this.txtPartyRelationInDate.Text = string.Format("{0:yyyy-MM-dd}", partyer.PartyRelationInDate);
                        }
                        if (partyer.PartyRelationOutDate != null)
                        {
                            this.txtPartyRelationOutDate.Text = string.Format("{0:yyyy-MM-dd}", partyer.PartyRelationOutDate);
                        }
                    }
                }
            }
        }
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.Party_Partyer newPartyer = new Model.Party_Partyer
            {
                Name = this.txtName.Text.Trim(),
                Sex = this.txtSex.Text.Trim(),
                Education = this.txtEducation.Text.Trim(),
                Nation = this.txtNation.Text.Trim(),
                Post = this.txtPost.Text.Trim(),
                Phone=this.txtPhone.Text.Trim()
            };
            newPartyer.BirthDate = Funs.GetNewDateTime(this.txtBirthDate.Text.Trim()+"-01");
            newPartyer.JoinPartyDate = Funs.GetNewDateTime(this.txtJoinPartyDate.Text.Trim());
            newPartyer.JoinPostDate = Funs.GetNewDateTime(this.txtJoinPostDate.Text.Trim());
            newPartyer.PartyRelationInDate = Funs.GetNewDateTime(this.txtPartyRelationInDate.Text.Trim());
            newPartyer.PartyRelationOutDate = Funs.GetNewDateTime(this.txtPartyRelationOutDate.Text.Trim());
            if (string.IsNullOrEmpty(this.PartyerId))
            {
                newPartyer.PartyerId = SQLHelper.GetNewID();
                PartyerService.AddPartyer(newPartyer);
                LogService.AddSys_Log(this.CurrUser, newPartyer.Name, newPartyer.PartyerId, BLL.Const.PartyerMenuId, BLL.Const.BtnAdd);
            }
            else
            {
                newPartyer.PartyerId = this.PartyerId;
                PartyerService.UpdatePartyer(newPartyer);
                LogService.AddSys_Log(this.CurrUser, newPartyer.Name, newPartyer.PartyerId, BLL.Const.PartyerMenuId, BLL.Const.BtnModify);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
            {
                this.btnSave.Hidden = false;
            }
            else
            {
                var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.PartyerMenuId);
                if (buttonList.Count() > 0)
                {
                    if (buttonList.Contains(BLL.Const.BtnSave))
                    {
                        this.btnSave.Hidden = false;
                    }
                }
            }
        }
        #endregion
    }
}