using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.HSSE.SitePerson
{
    public partial class ReadWriteCard : PageBase
    {
        #region 定义项
        /// 
        /// 人员Id
        /// 
        public string PersonId
        {
            get
            {
                return (string)ViewState["PersonId"];
            }
            set
            {
                ViewState["PersonId"] = value;
            }
        }
        #endregion
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.PersonId = Request.Params["PersonId"];
                if (!string.IsNullOrEmpty(this.PersonId))
                {
                    var person = BLL.PersonService.GetPersonById(this.PersonId);
                    if (person != null)
                    {
                        this.txtPersonName.Text = person.PersonName;
                        this.txtUnitName.Text = BLL.UnitService.GetUnitNameByUnitId(person.UnitId);
                        this.txtCardNo.Text = person.CardNo;
                    }
                }
            }
            //this.SetDefaltButton(this.txtCardNo, this.TextBox2);
        }
        /// 
        /// 发卡
        /// 
        /// 
        /// 
        protected void btnSendCard_Click(object sender, EventArgs e)
        {
            int newPersonIndex = 1;
            int? maxPersonIndex = BLL.PersonService.GetMaxPersonIndex(this.CurrUser.LoginProjectId);
            if (maxPersonIndex != null)
            {
                newPersonIndex = Convert.ToInt32(maxPersonIndex) + 1;
                List personIndexs = BLL.PersonService.GetPersonIndexs(this.CurrUser.LoginProjectId);
                for (int i = 1; i <= maxPersonIndex; i++)
                {
                    bool result = false;
                    foreach (int? personIndex in personIndexs)
                    {
                        if (i == personIndex)
                        {
                            result = true;
                        }
                    }
                    if (!result)
                    {
                        newPersonIndex = i;
                    }
                }
            }
            else
            {
                newPersonIndex = 1;
            }
            //string cardNo = this.txtCardNo.Text.Trim() + this.TextBox2.Text.Trim();
            string cardNo = this.txtCardNo.Text.Trim();
            try
            {
                if (!String.IsNullOrEmpty(cardNo))
                {
                    BLL.PersonService.SaveSendCard(PersonId, cardNo, newPersonIndex);
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            catch
            {
                ShowNotify("发卡未成功!", MessageBoxIcon.Warning);
                return;
            }
        }
        #region 验证卡号是否存在
        /// 
        /// 验证卡号是否存在
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtCardNo.Text))
            {
                var q = Funs.DB.SitePerson_Person.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.CardNo == this.txtCardNo.Text.Trim() && (x.PersonId != this.PersonId || (this.PersonId == null && x.PersonId != null)));
                if (q != null)
                {
                    ShowNotify("输入的卡号已存在!", MessageBoxIcon.Warning);
                }
            }
        }
        #endregion
    }
}