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 TestRunPerformanceEdit : PageBase
    {
        /// 
        /// 定义项
        /// 
        public string TestRunPerformanceId
        {
            get
            {
                return (string)ViewState["TestRunPerformanceId"];
            }
            set
            {
                ViewState["TestRunPerformanceId"] = value;
            }
        }
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                ProjectService.InitAllProjectDropDownList2(this.drpProject, true);
                this.TestRunPerformanceId = Request.QueryString["TestRunPerformanceId"];
                TestRunPerformanceStandardService.InitTypeDropDownList(drpType, true);
                Funs.FineUIPleaseSelect(this.drpItem);
                Funs.FineUIPleaseSelect(this.drpUnit);
                if (!String.IsNullOrEmpty(this.TestRunPerformanceId))
                {
                    var performance = BLL.Person_TestRunPerformanceService.GetPersonTestRunPerformanceById(this.TestRunPerformanceId);
                    if (performance != null)
                    {
                        if (performance.Months != null)
                        {
                            this.txtMonths.Text = string.Format("{0:yyyy-MM-dd}", performance.Months);
                        }
                        this.lbUserName.Text = BLL.UserService.GetUserNameByUserId(performance.UserId);
                        if (!string.IsNullOrEmpty(performance.ProjectId))
                        {
                            this.drpProject.SelectedValue = performance.ProjectId;
                        }
                        this.txtJobContent.Text = performance.JobContent;
                        Model.Base_TestRunPerformanceStandard standard = BLL.TestRunPerformanceStandardService.GetPerformanceStandardById(performance.TestRunPerformanceStandardId);
                        if (standard != null)
                        {
                            this.drpType.SelectedValue = standard.Type;
                            TestRunPerformanceStandardService.InitItemDropDownList(drpItem, this.drpType.SelectedValue, true);
                            this.drpItem.SelectedValue = standard.Item;
                            if (drpItem.SelectedItem.Text.Contains("_回路") || drpItem.SelectedItem.Text.Contains("_台"))
                            {
                                this.txtNum.Hidden = false;
                                if (performance.Num != null)
                                {
                                    this.txtNum.Text = performance.Num.ToString();
                                }
                            }
                            TestRunPerformanceStandardService.InitUnitDropDownList(drpUnit, this.drpType.SelectedValue, this.drpItem.SelectedValue, true);
                            this.drpUnit.SelectedValue = standard.TestRunPerformanceStandardId;
                        }
                        if (performance.Days != null)
                        {
                            this.txtDays.Text = performance.Days.ToString();
                        }
                    }
                }
                else
                {
                    this.lbUserName.Text = this.CurrUser.UserName;
                }
            }
        }
        /// 
        /// 保存数据
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                Alert.ShowInParent("请选择月份!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpProject.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInParent("请选择工作地点!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpType.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInParent("请选择工作类别!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpItem.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInParent("请选择工作项!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnit.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInParent("请选择计量单位!", MessageBoxIcon.Warning);
                return;
            }
            if (this.txtDays.Text.Trim() == "0" || string.IsNullOrEmpty(this.txtDays.Text.Trim()))
            {
                Alert.ShowInParent("请输入标准工作日!", MessageBoxIcon.Warning);
                return;
            }
            Model.Person_TestRunPerformance newperformance = new Model.Person_TestRunPerformance()
            {
                Months = Funs.GetNewDateTimeOrNow(this.txtMonths.Text.Trim()),
                UserId = this.CurrUser.UserId,
                ProjectId = this.drpProject.SelectedValue,
                JobContent = this.txtJobContent.Text.Trim(),
                TestRunPerformanceStandardId = this.drpUnit.SelectedValue,
                Days = Funs.GetNewInt(this.txtDays.Text.Trim()),
                Num = Funs.GetNewInt(this.txtNum.Text.Trim()),
            };
            if (!string.IsNullOrEmpty(this.TestRunPerformanceId))
            {
                newperformance.TestRunPerformanceId = this.TestRunPerformanceId;
                BLL.Person_TestRunPerformanceService.UpdatePersonTestRunPerformance(newperformance);
                BLL.LogService.AddSys_Log(this.CurrUser, null, newperformance.TestRunPerformanceId, BLL.Const.TestRunPerformanceMenuId, BLL.Const.BtnAdd);
            }
            else
            {
                newperformance.TestRunPerformanceId = SQLHelper.GetNewID();
                BLL.Person_TestRunPerformanceService.AddPersonTestRunPerformance(newperformance);
                BLL.LogService.AddSys_Log(this.CurrUser, null, newperformance.TestRunPerformanceId, BLL.Const.TestRunPerformanceMenuId, BLL.Const.BtnModify);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpType.SelectedValue != BLL.Const._Null)
            {
                TestRunPerformanceStandardService.InitItemDropDownList(drpItem, this.drpType.SelectedValue, true);
                this.drpItem.SelectedIndex = 0;
                this.drpUnit.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpUnit);
                this.drpUnit.SelectedIndex = 0;
            }
            else
            {
                this.drpItem.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpItem);
                this.drpItem.SelectedIndex = 0;
                this.drpUnit.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpUnit);
                this.drpUnit.SelectedIndex = 0;
            }
            this.txtDays.Text = string.Empty;
            this.txtNum.Hidden = true;
        }
        protected void drpItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpItem.SelectedValue != BLL.Const._Null)
            {
                TestRunPerformanceStandardService.InitUnitDropDownList(drpUnit, this.drpType.SelectedValue, this.drpItem.SelectedValue, true);
                this.drpUnit.SelectedIndex = 0;
                if (drpItem.SelectedItem.Text.Contains("_回路") || drpItem.SelectedItem.Text.Contains("_台"))
                {
                    this.txtNum.Hidden = false;
                    this.txtNum.Text = "1";
                }
                else
                {
                    this.txtNum.Hidden = true;
                }
            }
            else
            {
                this.txtNum.Hidden = true;
                this.drpUnit.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpUnit);
                this.drpUnit.SelectedIndex = 0;
            }
            this.txtDays.Text = string.Empty;
        }
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                Model.Base_TestRunPerformanceStandard standard = BLL.TestRunPerformanceStandardService.GetPerformanceStandardById(this.drpUnit.SelectedValue);
                if (standard != null)
                {
                    if (standard.Days != null)
                    {
                        if (this.txtNum.Hidden == true)
                        {
                            this.txtDays.Text = standard.Days.ToString();
                        }
                        else
                        {
                            this.txtDays.Text = (standard.Days * Funs.GetNewIntOrZero(this.txtNum.Text.Trim())).ToString();
                        }
                        if (standard.Days > 0)
                        {
                            this.txtDays.Readonly = true;
                        }
                        else
                        {
                            this.txtDays.Readonly = false;
                        }
                    }
                }
            }
            else
            {
                this.txtDays.Text = string.Empty;
                this.txtDays.Readonly = true;
            }
        }
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string window = String.Format("ShowTestRunPerformanceStandard.aspx", "查找 - ");
            PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdItemsString.ClientID) + Window1.GetShowReference(window));
        }
        #region 关闭弹出窗口
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            if (!string.IsNullOrEmpty(hdItemsString.Text))
            {
                Model.Base_TestRunPerformanceStandard standard = BLL.TestRunPerformanceStandardService.GetPerformanceStandardById(hdItemsString.Text);
                if (standard != null)
                {
                    this.drpType.SelectedValue = standard.Type;
                    TestRunPerformanceStandardService.InitItemDropDownList(drpItem, this.drpType.SelectedValue, true);
                    this.drpItem.SelectedValue = standard.Item;
                    if (drpItem.SelectedItem.Text.Contains("_回路") || drpItem.SelectedItem.Text.Contains("_台"))
                    {
                        this.txtNum.Hidden = false;
                        this.txtNum.Text = "1";
                    }
                    else
                    {
                        this.txtNum.Hidden = true;
                    }
                    TestRunPerformanceStandardService.InitUnitDropDownList(drpUnit, this.drpType.SelectedValue, this.drpItem.SelectedValue, true);
                    this.drpUnit.SelectedValue = standard.TestRunPerformanceStandardId;
                    this.txtDays.Text = standard.Days.ToString();
                    if (standard.Days > 0)
                    {
                        this.txtDays.Readonly = true;
                    }
                    else
                    {
                        this.txtDays.Readonly = false;
                    }
                }
            }
        }
        #endregion
    }
}