using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace FineUIPro.Web.HSSE.SitePerson
{
    public partial class PersonInfoEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string CheckingId
        {
            get
            {
                return (string)ViewState["CheckingId"];
            }
            set
            {
                ViewState["CheckingId"] = value;
            }
        }
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.ProjectId = this.CurrUser.LoginProjectId;
                ///区域下拉框
                BLL.UnitWorkService.InitUnitWorkDownList(this.drpWorkArea, this.ProjectId, true);
                UnitService.InitUnitDropDownList(this.drpUnit, this.CurrUser.LoginProjectId, true);
                if (ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.CurrUser.LoginProjectId, this.CurrUser.UnitId))
                {
                    this.drpUnit.SelectedValue = this.CurrUser.UnitId;
                    this.drpUnit.Enabled = false;
                }
                BindGrid(string.Empty);
                this.CheckingId = Request.Params["CheckingId"];
                if (!string.IsNullOrEmpty(Request.Params["type"]))
                {
                    this.btnSave.Hidden = true;
                }
                if (!string.IsNullOrEmpty(this.CheckingId))
                {
                    Model.SitePerson_Checking personInfo = BLL.SitePerson_CheckingService.GetPersonInfoByCheckingId(this.CheckingId);
                    if (personInfo != null)
                    {
                        this.ProjectId = personInfo.ProjectId;
                        if (!string.IsNullOrEmpty(personInfo.PersonId))
                        {
                            BindGrid(personInfo.PersonId);
                            this.drpPersonId.Value = personInfo.PersonId;
                        }
                        this.txtIdCard.Text = personInfo.IdentityCard;
                        if (!string.IsNullOrEmpty(personInfo.WorkAreaId))
                        {
                            this.drpWorkArea.SelectedValueArray = personInfo.WorkAreaId.Split(',');
                        }
                        this.txtWorkArea.Text = personInfo.WorkAreaName;
                        this.txtAddress.Text = personInfo.Address;
                        if (personInfo.IntoOutTime != null)
                        {
                            this.txtTime.Text = string.Format("{0:yyyy-MM-dd}", personInfo.IntoOutTime);
                            this.txtTime2.Text = string.Format("{0:HH:mm:ss}", personInfo.IntoOutTime);
                        }
                        this.drpType.SelectedValue = personInfo.IntoOut;
                    }
                }
                else
                {
                    this.txtTime.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    this.txtTime2.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
                }
            }
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        ///  人员下拉框选择
        /// 
        /// 
        /// 
        protected void drpPersonId_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.drpPersonId.Value))
            {
                string[] personIds = this.drpPersonId.Value.Split(',');
                string idCards = string.Empty;
                string workAreaIds = string.Empty;
                string workAreaNames = string.Empty;
                foreach (var item in personIds)
                {
                    var person = BLL.PersonService.GetPersonById(item.Trim());
                    if (person != null)
                    {
                        idCards += person.IdentityCard + ",";
                        workAreaIds += person.WorkAreaId;                   
                    }
                }
                if (!string.IsNullOrEmpty(idCards))
                {
                    this.txtIdCard.Text = idCards.Substring(0, idCards.LastIndexOf(','));
                }
                if (!string.IsNullOrEmpty(workAreaIds))
                {
                    this.drpWorkArea.SelectedValueArray = workAreaIds.Split(',');
                    this.txtWorkArea.Text = UnitWorkService.GetUnitWorkName(workAreaIds);
                }              
            }
        }
        #endregion
        #region 区域选择框事件
        /// 
        /// 区域选择框事件
        /// 
        /// 
        /// 
        protected void drpWorkArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectName = string.Empty;
            foreach (var item in this.drpWorkArea.SelectedValueArray)
            {
                if (item != Const._Null)
                {
                    string text = this.drpWorkArea.Items.FindByValue(item).Text;
                    if (string.IsNullOrEmpty(selectName))
                    {
                        selectName = text;
                    }
                    else
                    {
                        selectName += ","+ text;
                    }
                }
            }
            this.txtWorkArea.Text = selectName;
            this.drpWorkArea.SelectedValueArray = Funs.RemoveDropDownListNull(this.drpWorkArea.SelectedValueArray);
        }
        #endregion
        #region 绑定数据
        /// 
        /// 下拉框查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid(string.Empty);
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid(string personId)
        {
            string strSql = @"SELECT Person.PersonId,Person.CardNo,Person.PersonName,Person.IdentityCard,Person.UnitId,Person.WorkPostId,Unit.UnitName,WorkPost.WorkPostName "
                + @" FROM SitePerson_Person AS Person "
                + @" LEFT JOIN Base_Unit AS Unit ON Unit.UnitId = Person.UnitId "
                + @" LEFT JOIN Base_WorkPost AS WorkPost ON WorkPost.WorkPostId = Person.WorkPostId  " 
                +@" WHERE IsUsed =1 AND (OutTime IS NULL OR OutTime > GETDATE()) AND ProjectId='" + this.ProjectId + "'";
            List listStr = new List();
            //if (!string.IsNullOrEmpty(this.txtCardNo.Text.Trim()))
            //{
            //    strSql += " AND Person.CardNo LIKE @CardNo";
            //    listStr.Add(new SqlParameter("@CardNo", "%" + this.txtCardNo.Text.Trim() + "%"));
            //}
            if (this.drpUnit.SelectedValue != Const._Null)
            {
                strSql += " AND Person.UnitId = @UnitId";
                listStr.Add(new SqlParameter("@UnitId", this.drpUnit.SelectedValue));
            }
            if (!string.IsNullOrEmpty(this.txtPersonName.Text.Trim()))
            {
                strSql += " AND Person.PersonName LIKE @PersonName";
                listStr.Add(new SqlParameter("@PersonName", "%" + this.txtPersonName.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtIdentityCard.Text.Trim()))
            {
                strSql += " AND Person.IdentityCard LIKE @IdentityCard";
                listStr.Add(new SqlParameter("@IdentityCard", "%" + this.txtIdentityCard.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(personId))
            {
                strSql += " AND Person.PersonId = @PersonId";
                listStr.Add(new SqlParameter("@PersonId", personId));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;       
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.drpPersonId.Value))
            {
                ShowNotify("请选择人员!", MessageBoxIcon.Warning);
                return;
            }
            string[] personIds = this.drpPersonId.Value.Split(',');
            foreach (var item in personIds)
            {
                Model.SitePerson_Checking personInfo = new Model.SitePerson_Checking
                {
                    ProjectId = this.ProjectId,
                    PersonId = item.Trim()
                };
                var person = BLL.PersonService.GetPersonById(personInfo.PersonId);
                if (person != null)
                {
                    personInfo.IdentityCard = person.IdentityCard;
                    personInfo.CardNo = person.CardNo;
                    personInfo.PersonName = person.PersonName;
                    personInfo.UnitId = person.UnitId;
                    if (!string.IsNullOrEmpty(personInfo.UnitId))
                    {
                        personInfo.UnitName = UnitService.GetUnitNameByUnitId(person.UnitId);
                    }
                    if (!string.IsNullOrEmpty(person.WorkAreaId))
                    {
                        personInfo.WorkAreaId = person.WorkAreaId;
                        personInfo.WorkAreaName = BLL.UnitWorkService.GetUnitWorkName(person.WorkAreaId);
                    }                    
                }
                personInfo.Address = this.txtAddress.Text.Trim();
                string date = string.IsNullOrEmpty(this.txtTime.Text) ? string.Format("{0:yyyy-MM-dd}", DateTime.Now) : this.txtTime.Text.Trim();
                string time = string.IsNullOrEmpty(this.txtTime2.Text) ? string.Format("{0:HH:mm:ss}", DateTime.Now) : this.txtTime2.Text.Trim();
                personInfo.IntoOutTime = Funs.GetNewDateTimeOrNow(date + " " + time);
                personInfo.IntoOut = this.drpType.SelectedValue.Trim();
                if (!string.IsNullOrEmpty(CheckingId))
                {
                    personInfo.CheckingId = CheckingId;
                    BLL.SitePerson_CheckingService.UpdatePersonInfo(personInfo);
                    BLL.LogService.AddSys_Log(this.CurrUser, personInfo.CardNo, personInfo.CheckingId, BLL.Const.PersonalInfoMenuId, BLL.Const.BtnModify);
                }
                else
                {
                    //this.CheckingId = SQLHelper.GetNewID();
                    personInfo.CheckingId = SQLHelper.GetNewID();
                    BLL.SitePerson_CheckingService.AddPersonInfo(personInfo);
                    BLL.LogService.AddSys_Log(this.CurrUser, personInfo.CardNo, personInfo.CheckingId, BLL.Const.PersonalInfoMenuId, BLL.Const.BtnDelete);
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region rbl选择事件
        /// 
        /// 自动、手动选择事件
        /// 
        /// 
        /// 
        protected void rblCheck_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblCheck.SelectedValue == "自动")
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("PersonAutoDetail.aspx", "编辑 - ")));
            }
        }
        #endregion
    }
}