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.ZHGL.Person
{
    public partial class BranchPersonEdit : PageBase
    {
        #region 定义项
        /// 
        /// 用户主键
        /// 
        public string CompanyBranchPersonId
        {
            get
            {
                return (string)ViewState["CompanyBranchPersonId"];
            }
            set
            {
                ViewState["CompanyBranchPersonId"] = value;
            }
        }
        /// 
        /// 单位主键
        /// 
        public string UnitId
        {
            get
            {
                return (string)ViewState["UnitId"];
            }
            set
            {
                ViewState["UnitId"] = value;
            }
        }
        #endregion
        /// 
        /// 用户编辑页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                string type = Request.Params["type"];
                BLL.UnitService.InitNoThisAllUnitDownList(this.drpUnit, true);
                if (this.CurrUser.UnitId != null && this.CurrUser.UnitId != BLL.Const.UnitId_CWCEC)
                {
                    this.drpUnit.Enabled = false;
                    this.drpUnit.SelectedValue = this.CurrUser.UnitId;
                }
                ///权限
                this.GetButtonPower();
                this.CompanyBranchPersonId = Request.Params["CompanyBranchPersonId"];
                WorkPostService.InitWorkPostDropDownList(this.drpWorkPost, true);
                if (!string.IsNullOrEmpty(this.CompanyBranchPersonId))
                {
                    var CompanyBranchPerson = BLL.CompanyBranchPersonService.GetCompanyBranchPersonById(this.CompanyBranchPersonId);
                    if (CompanyBranchPerson != null)
                    {
                        this.hdCompanyBranchPersonId.Text = this.CompanyBranchPersonId;
                        if (!string.IsNullOrEmpty(CompanyBranchPerson.UnitId))
                        {
                            this.drpUnit.SelectedValue = CompanyBranchPerson.UnitId;
                        }
                        this.txtPersonName.Text = CompanyBranchPerson.PersonName;
                        if (!string.IsNullOrEmpty(CompanyBranchPerson.Sex))
                        {
                            this.rblSex.SelectedValue = CompanyBranchPerson.Sex;
                        }
                        this.txtIdentityCard.Text = CompanyBranchPerson.IdentityCard;
                        if (!string.IsNullOrEmpty(CompanyBranchPerson.WorkPostId))
                        {
                            this.drpWorkPost.SelectedValue = CompanyBranchPerson.WorkPostId;
                        }
                        this.txtTelephone.Text = CompanyBranchPerson.Telephone;
                        this.txtAddress.Text = CompanyBranchPerson.Address;
                        if (CompanyBranchPerson.IsOnJob == true)
                        {
                            this.rblIsOnJob.SelectedValue = "True";
                        }
                        else
                        {
                            this.rblIsOnJob.SelectedValue = "False";
                        }
                        this.txtRemark.Text = CompanyBranchPerson.Remark;
                    }
                }
            }
        }
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnit.SelectedValue == Const._Null)
            {
                Alert.ShowInParent("请选择单位!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpWorkPost.SelectedValue == Const._Null)
            {
                Alert.ShowInParent("请选择岗位!", MessageBoxIcon.Warning);
                return;
            }
            if (!string.IsNullOrEmpty(this.txtIdentityCard.Text) && BLL.CompanyBranchPersonService.IsExistPersonIdentityCard(this.CompanyBranchPersonId, this.txtIdentityCard.Text.Trim()) == true)
            {
                Alert.ShowInParent("输入的身份证号码已存在!", MessageBoxIcon.Warning);
                return;
            }
            Model.Person_CompanyBranchPerson newCompanyBranchPerson = new Model.Person_CompanyBranchPerson
            {
                PersonName = this.txtPersonName.Text.Trim(),
                Sex = this.rblSex.SelectedValue,
                IdentityCard = this.txtIdentityCard.Text.Trim(),
                Telephone = this.txtTelephone.Text.Trim(),
                Address = this.txtAddress.Text.Trim(),
                IsOnJob = Convert.ToBoolean(this.rblIsOnJob.SelectedValue),
                Remark = this.txtRemark.Text.Trim()
            };
            if (this.drpUnit.SelectedValue != Const._Null)
            {
                newCompanyBranchPerson.UnitId = this.drpUnit.SelectedValue;
            }
            if (this.drpWorkPost.SelectedValue != Const._Null)
            {
                newCompanyBranchPerson.WorkPostId = this.drpWorkPost.SelectedValue;
            }
            if (string.IsNullOrEmpty(this.CompanyBranchPersonId))
            {
                if (string.IsNullOrEmpty(this.hdCompanyBranchPersonId.Text))
                {
                    newCompanyBranchPerson.CompanyBranchPersonId = SQLHelper.GetNewID(typeof(Model.Person_CompanyBranchPerson));
                }
                else
                {
                    newCompanyBranchPerson.CompanyBranchPersonId = this.hdCompanyBranchPersonId.Text;
                }
                CompanyBranchPersonService.AddCompanyBranchPerson(newCompanyBranchPerson);
                LogService.AddSys_Log(this.CurrUser, newCompanyBranchPerson.PersonName, newCompanyBranchPerson.CompanyBranchPersonId, BLL.Const.BranchPersonMenuId, BLL.Const.BtnAdd);
            }
            else
            {
                newCompanyBranchPerson.CompanyBranchPersonId = this.CompanyBranchPersonId;
                CompanyBranchPersonService.UpdateCompanyBranchPerson(newCompanyBranchPerson);
                LogService.AddSys_Log(this.CurrUser, newCompanyBranchPerson.PersonName, newCompanyBranchPerson.CompanyBranchPersonId, BLL.Const.BranchPersonMenuId, 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.BranchPersonMenuId);
                if (buttonList.Count() > 0)
                {
                    if (buttonList.Contains(BLL.Const.BtnSave))
                    {
                        this.btnSave.Hidden = false;
                    }
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdCompanyBranchPersonId.Text))
            {
                this.hdCompanyBranchPersonId.Text = SQLHelper.GetNewID();
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/BranchPersonAttachUrl&menuId={1}", this.hdCompanyBranchPersonId.Text, BLL.Const.BranchPersonMenuId)));
        }
        #endregion
    }
}