using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace FineUIPro.Web.Personal
{
    public partial class BusinessTripEdit : PageBase
    {
        /// 
        /// 定义项
        /// 
        public string BusinessTripId
        {
            get
            {
                return (string)ViewState["BusinessTripId"];
            }
            set
            {
                ViewState["BusinessTripId"] = value;
            }
        }
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                ProjectService.InitAllProjectDropDownList(this.drpProject, true);
                this.BusinessTripId = Request.QueryString["BusinessTripId"];
                if (!String.IsNullOrEmpty(this.BusinessTripId))
                {
                    var trip = BLL.Person_BusinessTripService.GetPersonBusinessTripById(this.BusinessTripId);
                    if (trip != null)
                    {
                        if (!string.IsNullOrEmpty(trip.Type))
                        {
                            this.rblType.SelectedValue = trip.Type;
                        }
                        if (this.rblType.SelectedValue == "1")
                        {
                            this.drpProject.Hidden = false;
                        }
                        else
                        {
                            this.drpProject.Hidden = true;
                        }
                        this.lbUserName.Text = BLL.UserService.GetUserNameByUserId(trip.UserId);
                        if (!string.IsNullOrEmpty(trip.ProjectId))
                        {
                            this.drpProject.SelectedValue = trip.ProjectId;
                        }
                        if (trip.ArriveDate != null)
                        {
                            this.txtArriveDate.Text = string.Format("{0:yyyy-MM-dd}", trip.ArriveDate);
                        }
                        if (trip.LeaveDate != null)
                        {
                            this.txtLeaveDate.Text = string.Format("{0:yyyy-MM-dd}", trip.LeaveDate);
                        }
                        txtArriveDate_TextChanged(null, null);
                    }
                }
                else
                {
                    this.lbUserName.Text = this.CurrUser.UserName;
                }
            }
        }
        /// 
        /// 保存数据
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpProject.SelectedValue == BLL.Const._Null && this.rblType.SelectedValue == "1")
            {
                Alert.ShowInParent("请选择项目!", MessageBoxIcon.Warning);
                return;
            }
            Model.Person_BusinessTrip newTrip = new Model.Person_BusinessTrip()
            {
                Type = this.rblType.SelectedValue,
                UserId = this.CurrUser.UserId,
                ProjectId = this.drpProject.SelectedValue,
                ArriveDate = Funs.GetNewDateTime(this.txtArriveDate.Text),
                LeaveDate = Funs.GetNewDateTime(this.txtLeaveDate.Text),
            };
            if (!string.IsNullOrEmpty(this.BusinessTripId))
            {
                newTrip.BusinessTripId = this.BusinessTripId;
                BLL.Person_BusinessTripService.UpdatePersonBusinessTrip(newTrip);
                BLL.LogService.AddSys_Log(this.CurrUser, null, newTrip.BusinessTripId, BLL.Const.BusinessTripMenuId, BLL.Const.BtnAdd);
            }
            else
            {
                newTrip.BusinessTripId = SQLHelper.GetNewID();
                BLL.Person_BusinessTripService.AddPersonBusinessTrip(newTrip);
                BLL.LogService.AddSys_Log(this.CurrUser, null, newTrip.BusinessTripId, BLL.Const.BusinessTripMenuId, BLL.Const.BtnModify);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblType.SelectedValue == "1")
            {
                this.drpProject.Hidden = false;
            }
            else
            {
                this.drpProject.Hidden = true;
            }
        }
        protected void txtArriveDate_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtArriveDate.Text.Trim()) && !string.IsNullOrEmpty(this.txtLeaveDate.Text.Trim()))
            {
                this.lbDays.Text = ((Convert.ToDateTime(this.txtLeaveDate.Text.Trim()) - Convert.ToDateTime(this.txtArriveDate.Text.Trim())).Days + 1).ToString();
            }
            else
            {
                this.lbDays.Text = string.Empty;
            }
        }
    }
}