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.Personal
{
    public partial class PersonalInfo : PageBase
    {
        #region 定义变量
        /// 
        /// 照片附件路径
        /// 
        public string PhotoAttachUrl
        {
            get
            {
                return (string)ViewState["PhotoAttachUrl"];
            }
            set
            {
                ViewState["PhotoAttachUrl"] = value;
            }
        }
        #endregion
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /// Tab1加载页面方法
                this.Tab1LoadData();
                if (this.CurrUser.UserId == Const.hfnbdId)
                {
                    this.btnCustomQuery.Hidden = false;
                    this.lbSystemVersion.Hidden = false;
                    this.lbSystemVersion.Text = Funs.SystemVersion;
                    this.btnPassRefresh.Hidden = false;
                }
                if (this.CurrUser.UserId == Const.sysglyId)
                {
                    this.lbSystemVersion.Hidden = false;
                    this.lbSystemVersion.Text = Funs.SystemVersion;
                    this.btnPassRefresh.Hidden = false;
                }
            }
        }
        #region Tab1
        /// 
        /// Tab1加载页面方法
        /// 
        private void Tab1LoadData()
        {
            var user = Funs.DB.Sys_User.FirstOrDefault(x => x.UserId == this.CurrUser.UserId);
            if (user != null)
            {
                this.txtUserName.Text = user.UserName;
                this.txtUserCode.Text = user.UserCode;
                var sexVules = BLL.ConstValue.drpConstItemList(ConstValue.Group_0002).FirstOrDefault(x => x.ConstValue == user.Sex);
                if (sexVules != null)
                {
                    this.drpSex.Text = sexVules.ConstText;
                }
                this.dpBirthDay.Text = string.Format("{0:yyyy-MM-dd}", user.BirthDay);
                var MarriageVules = BLL.ConstValue.drpConstItemList(ConstValue.Group_0003).FirstOrDefault(x => x.ConstValue == user.Marriage);
                if (MarriageVules != null)
                {
                    this.drpMarriage.Text = MarriageVules.ConstText;
                }
                var NationVules = BLL.ConstValue.drpConstItemList(ConstValue.Group_0005).FirstOrDefault(x => x.ConstValue == user.Nation);
                if (NationVules != null)
                {
                    this.drpNation.Text = NationVules.ConstText;
                }
                var units = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitId == user.UnitId);
                if (units != null)
                {
                    this.drpUnit.Text = units.UnitName;
                }
                this.txtAccount.Text = user.Account;
                this.txtIdentityCard.Text = user.IdentityCard;
                this.txtEmail.Text = user.Email;
                this.txtTelephone.Text = user.Telephone;
                var EducationVules = BLL.ConstValue.drpConstItemList(ConstValue.Group_0004).FirstOrDefault(x => x.ConstValue == user.Education);
                if (EducationVules != null)
                {
                    this.drpEducation.Text = EducationVules.ConstText;
                }
                this.txtHometown.Text = user.Hometown;
                //var position = BLL.PositionService.GetPositionById(user.PositionId);
                //if (position != null)
                //{
                //    this.drpPosition.Text = position.PositionName;
                //}
                this.txtPerformance.Text = user.Performance;
                if (!string.IsNullOrEmpty(user.PhotoUrl))
                {
                    this.PhotoAttachUrl = user.PhotoUrl;
                    this.Image1.ImageUrl = "~/" + this.PhotoAttachUrl;
                }
            }
        }
        #endregion
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var q = Funs.DB.Sys_User.FirstOrDefault(x => x.Account == this.txtAccount.Text.Trim() && (x.UserId != this.CurrUser.UserId || (this.CurrUser.UserId == null && x.UserId != null)));
            if (q != null)
            {
                Alert.ShowInParent("输入的账号已存在!", MessageBoxIcon.Warning);
                return;
            }
            if (!string.IsNullOrEmpty(this.txtUserCode.Text))
            {
                var q2 = Funs.DB.Sys_User.FirstOrDefault(x => x.UserCode == this.txtUserCode.Text.Trim() && (x.UserId != this.CurrUser.UserId || (this.CurrUser.UserId == null && x.UserId != null)));
                if (q2 != null)
                {
                    Alert.ShowInParent("输入的编号已存在!", MessageBoxIcon.Warning);
                    return;
                }
            }
            if (!string.IsNullOrEmpty(this.txtIdentityCard.Text) && BLL.UserService.IsExistUserIdentityCard(this.CurrUser.UserId, this.txtIdentityCard.Text.Trim()) == true)
            {
                Alert.ShowInParent("输入的身份证号码已存在!", MessageBoxIcon.Warning);
                return;
            }
            Model.Sys_User newUser = new Model.Sys_User
            {
                UserCode = this.txtUserCode.Text.Trim(),
                UserName = this.txtUserName.Text.Trim(),
                Account = this.txtAccount.Text.Trim(),
                IdentityCard = this.txtIdentityCard.Text.Trim(),
                Email = this.txtEmail.Text.Trim(),
                Telephone = this.txtTelephone.Text.Trim(),
            };
            newUser.UserId = this.CurrUser.UserId;
            UserService.UpdateUserInfo(newUser);
            Alert.ShowInParent("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        protected void btnCustomQuery_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("../SysManage/CustomQuery.aspx"), "查询"));
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void btnPassRefresh_Click(object sender, EventArgs e)
        {
            var getUsers = from x in Funs.DB.Sys_User
                           where x.RawPassword == null
                           select x;
            foreach (var item in getUsers)
            {
                string password = Funs.getInitialPassword(item.UnitId, item.IdentityCard);
                UserService.UpdatePassword(item.UserId, password);
            }
            var getPersons = from x in Funs.DB.SitePerson_Person
                             where  x.RawPassword == null
                             select x;
            foreach (var item in getPersons)
            {
                string password = Funs.getInitialPassword(item.UnitId, item.IdentityCard);
                PersonService.UpdateSitePersonPassword(item.PersonId, password);
            }
            ShowNotify("重置成功!", MessageBoxIcon.Success);
        }
    }
}