using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.PersonManage
{
    public partial class PersonManageEdit : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                BLL.UnitService.InitUnitDropDownList(this.drpUnitId, this.CurrUser.LoginProjectId, true);
                Funs.FineUIPleaseSelect(drpTeamGroupId);
                var unit = BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId);
                if (unit == null || unit.UnitId == BLL.Const.UnitId_CD)
                {
                    drpUnitId.Enabled = true;
                }
                else
                {
                    drpUnitId.SelectedValue = this.CurrUser.UnitId;
                    drpUnitId.Enabled = false;
                }
                this.cbWeldMethod.DataTextField = "WeldingMethodCode";
                this.cbWeldMethod.DataValueField = "WeldingMethodId";
                this.cbWeldMethod.DataSource = BLL.WeldMethodItemService.GetWeldMethodList();
                this.cbWeldMethod.DataBind();
                this.cbSteType.DataTextField = "Text";
                this.cbSteType.DataValueField = "Value";
                this.cbSteType.DataSource = BLL.DropListService.HJGL_GetSteTypeList();
                this.cbSteType.DataBind();
                string welderId = Request.Params["welderId"];
                if (!string.IsNullOrEmpty(welderId))
                {
                    this.hdWelderId.Text = welderId;
                    Model.BS_Welder welder = BLL.WelderService.GetWelderById(this.hdWelderId.Text);
                    if (welder != null)
                    {
                        if (!string.IsNullOrEmpty(welder.AttachUrl))
                        {
                            imgPhoto.ImageUrl = "~/" + welder.AttachUrl;
                        }
                        if (!string.IsNullOrEmpty(welder.WED_Unit))
                        {
                            this.drpUnitId.SelectedValue = welder.WED_Unit;
                            this.drpTeamGroupId.Items.Clear();
                            BLL.TeamGroupService.InitTeamGroupProjectUnitDropDownList(this.drpTeamGroupId, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, true);
                        }
                        if (!string.IsNullOrEmpty(welder.TeamGroupId))
                        {
                            this.drpTeamGroupId.SelectedValue = welder.TeamGroupId;
                        }
                        this.txtWED_Name.Text = welder.WED_Name;
                        this.txtWED_Code.Text = welder.WED_Code;
                        this.txtBirthday.Text = welder.WED_Birthday.HasValue ? string.Format("{0:yyyy-MM-dd}", welder.WED_Birthday) : "";
                        if (!string.IsNullOrEmpty(welder.WED_TYPE))
                        {
                            this.rbtype.SelectedValue = welder.WED_TYPE;
                        }
                        if (!string.IsNullOrEmpty(welder.WED_Sex))
                        {
                            this.rblSex.SelectedValue = welder.WED_Sex.Trim();
                        }
                        this.txtWorkCode.Text = welder.WED_WorkCode;
                        this.txtLimitDate.Text = welder.LimitDate.HasValue ? string.Format("{0:yyyy-MM-dd}", welder.LimitDate) : "";
                        this.txtClass.Text = welder.WED_Class;
                        if (welder.WED_IfOnGuard.HasValue)
                        {
                            if (welder.WED_IfOnGuard.Value == true)
                            {
                                this.cbIfOnGuard.Checked = true;
                            }
                            else
                            {
                                this.cbIfOnGuard.Checked = false;
                            }
                        }
                        this.txtIdentityCard.Text = welder.IdentityCard;
                        if (!string.IsNullOrEmpty(welder.WED_Remark))
                            this.txtRemak.Text = welder.WED_Remark;
                        if (!string.IsNullOrEmpty(welder.SE_EquipmentID))
                            this.txtSE_EquipmentID.Text = welder.SE_EquipmentID;
                        var items = from x in Funs.DB.BS_WeldMethodItem where x.WED_ID == welder.WED_ID select x.WME_ID;
                        this.dpWeldMethod.Values = items.ToArray();
                        if (!string.IsNullOrEmpty(welder.JOT_Sch))
                            this.dpJOTSch.SelectedValue = welder.JOT_Sch;
                        if (!string.IsNullOrEmpty(welder.Steels))
                        {
                            this.drpSteType.Values = welder.Steels.Split(',');
                        }
                    }
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdWelderId.Text))
            {
                this.hdWelderId.Text = SQLHelper.GetNewID(typeof(Model.BS_Welder));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/HJGL/PersonManage/&menuId={1}", this.hdWelderId.Text, BLL.Const.HJGL_WelderManageMenuId)));
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 单位下拉选择事件
        /// 
        /// 
        /// 
        protected void drpUnitId_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpTeamGroupId.Items.Clear();
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                BLL.TeamGroupService.InitTeamGroupProjectUnitDropDownList(this.drpTeamGroupId, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, true);
                this.drpTeamGroupId.SelectedIndex = 0;
            }
            else
            {
                Funs.FineUIPleaseSelect(drpTeamGroupId);
                this.drpTeamGroupId.SelectedIndex = 0;
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string welderId = Request.Params["welderId"];
            Model.BS_Welder welder = new Model.BS_Welder();
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                welder.WED_Unit = this.drpUnitId.SelectedValue;
            }
            if (this.drpTeamGroupId.SelectedValue != BLL.Const._Null)
            {
                welder.TeamGroupId = this.drpTeamGroupId.SelectedValue;
            }
            welder.WED_Code = this.txtWED_Code.Text.Trim();
            welder.WED_Name = this.txtWED_Name.Text.Trim();
            welder.WED_Sex = this.rblSex.SelectedValue;
            welder.WED_TYPE = this.rbtype.SelectedValue;
            welder.WED_Birthday = Funs.GetNewDateTime(this.txtBirthday.Text.Trim());
            welder.LimitDate = Funs.GetNewDateTime(this.txtLimitDate.Text.Trim());
            welder.WED_WorkCode = this.txtWorkCode.Text.Trim();
            welder.WED_Class = this.txtClass.Text.Trim();
            if (this.cbIfOnGuard.Checked)
            {
                welder.WED_IfOnGuard = true;
            }
            else
            {
                welder.WED_IfOnGuard = false;
            }
            welder.IdentityCard = this.txtIdentityCard.Text.Trim();
            welder.SE_EquipmentID = this.txtSE_EquipmentID.Text.Trim();
            welder.WED_Remark = this.txtRemak.Text.Trim();
            welder.ProjectId = this.CurrUser.LoginProjectId;
            welder.WederType = "1";//人员信息
            welder.JOT_Sch =   this.dpJOTSch.SelectedValue;
            welder.Steels = string.Join(",", this.drpSteType.Values);
            if (!string.IsNullOrEmpty(imgPhoto.ImageUrl) && imgPhoto.ImageUrl != "~/res/images/blank_150.png")
            {
                welder.AttachUrl = imgPhoto.ImageUrl.Replace("../../", "").Replace("~/", "");
            }
            else
            {
                welder.AttachUrl = null;
            }
            if (!string.IsNullOrEmpty(welderId))
            {
                welder.WED_ID = welderId;
                var wed = BLL.PersonManageService.GetBSWelderByTeamWEDID(welderId);
                string name = wed.WED_Name;
                string wedCode = wed.WED_Code;
                if (name != this.txtWED_Name.Text.Trim())
                {
                    if (BLL.WelderService.IsExistWEDName(this.txtWED_Name.Text.Trim(), this.CurrUser.LoginProjectId, "1"))
                    {
                        Alert.ShowInTop("焊工姓名已存在!", MessageBoxIcon.Warning);
                        return;
                    }
                }
                if (wedCode != this.txtWED_Code.Text.Trim())
                {
                    if (BLL.WelderService.IsExistWEDCode(this.txtWED_Code.Text.Trim(), this.CurrUser.LoginProjectId, "1"))
                    {
                        Alert.ShowInTop("焊工代号已存在!", MessageBoxIcon.Warning);
                        return;
                    }
                }
                BLL.WelderService.UpdateBSWelder(welder);
            }
            else
            {
                if (BLL.WelderService.IsExistWEDName(this.txtWED_Name.Text.Trim(), this.CurrUser.LoginProjectId, "1"))
                {
                    Alert.ShowInTop("焊工姓名已存在!", MessageBoxIcon.Warning);
                    return;
                }
                if (BLL.WelderService.IsExistWEDCode(this.txtWED_Code.Text.Trim(), this.CurrUser.LoginProjectId, "1"))
                {
                    Alert.ShowInTop("焊工代号已存在!", MessageBoxIcon.Warning);
                    return;
                }
                if (!string.IsNullOrEmpty(this.hdWelderId.Text.Trim()))
                {
                    welder.WED_ID = this.hdWelderId.Text;
                }
                else
                {
                    this.hdWelderId.Text = SQLHelper.GetNewID(typeof(Model.BS_Welder));
                    welder.WED_ID = this.hdWelderId.Text;
                }
                BLL.WelderService.AddBSWelder(welder);
            }
            Model.SitePerson_Person person = new Model.SitePerson_Person();
            person.PersonId = welder.WED_ID;
            person.IdentityCard = welder.IdentityCard;
            person.PhotoUrl = welder.AttachUrl;
            ///上传人脸库          
            if (!string.IsNullOrEmpty(person.PhotoUrl))
            {
                Alert.ShowInParent(APIPersonService.PersonFace(person), MessageBoxIcon.Warning);
            }
            //保存焊接方法
            BLL.WeldMethodItemService.DeleteWeldMethodItem(welder.WED_ID);
            if (this.dpWeldMethod.Values != null && this.dpWeldMethod.Values.Length > 0)
            {
                foreach (var item in this.dpWeldMethod.Values)
                {
                    Model.BS_WeldMethodItem weldMethodItem = new Model.BS_WeldMethodItem();
                    weldMethodItem.WeldMethodItemId = SQLHelper.GetNewID(typeof(Model.BS_WeldMethodItem));
                    weldMethodItem.WED_ID = welder.WED_ID;
                    weldMethodItem.WME_ID = item;
                    BLL.WeldMethodItemService.AddWeldMethodItem(weldMethodItem);
                }
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region  上传照片
        /// 
        /// 上传照片
        /// 
        /// 
        /// 
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                if (!ValidateFileType(fileName))
                {
                    ShowNotify("无效的文件类型!");
                    return;
                }
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                string url = "~/FileUpload/PersonBaseInfo/" + DateTime.Now.Year + "-" + DateTime.Now.Month + "/";
                filePhoto.SaveAs(Server.MapPath(url + fileName));
                imgPhoto.ImageUrl = url + fileName;
                // 清空文件上传组件
                filePhoto.Reset();
            }
        }
        #endregion
    }
}