using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.QuantityManagement
{
    public partial class DayInputEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string DayInputId
        {
            get
            {
                return (string)ViewState["DayInputId"];
            }
            set
            {
                ViewState["DayInputId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                this.DayInputId = Request.Params["DayInputId"];
                BLL.DrawingService.InitDrawingChangeDropDownList(drpDrawingNo, this.CurrUser.LoginProjectId, true);
                BLL.TeamGroupService.InitTeamGroupProjectDropDownList(drpWorkTeam, this.CurrUser.LoginProjectId, true);
                var DayInput = BLL.DayInputService.GetDayInputById(this.DayInputId);
                if (DayInput != null)
                {
                    this.hdDayInputId.Text = this.DayInputId;
                    Model.QuantityManagement_Base b = BLL.BaseService.GetBaseById(DayInput.BaseId);
                    if (b != null)
                    {
                        Model.QuantityManagement_Drawing drawing = BLL.DrawingService.GetDrawingById(b.DrawingId);
                        this.DayInputId = DayInput.DayInputId;
                        if (drawing != null)
                        {
                            this.txtWorkSection.Text = drawing.WorkSection;
                            this.drpDrawingNo.SelectedValue = b.DrawingId;
                            this.txtDrawingName.Text = drawing.DrawingName;
                        }
                        BLL.BaseService.InitPartDropDownList(this.drpPart, b.DrawingId, true);
                        this.drpPart.SelectedValue = b.Part;
                        BLL.BaseService.InitProjectContentDropDownList(this.drpProjectContent, b.DrawingId, b.Part, true);
                        this.drpProjectContent.SelectedValue = b.BaseId;
                        this.txtUnit.Text = b.Unit;
                        decimal usedAmount = (from x in Funs.DB.QuantityManagement_DayInput where x.BaseId == DayInput.BaseId select x.DayAmount ?? 0).Sum();
                        if (b.Amount != null)
                        {
                            this.txtAmount.Text = (b.Amount - usedAmount).ToString();
                            this.txtDayAmount.MaxValue = Convert.ToDouble(b.Amount - usedAmount);
                        }
                        if (!string.IsNullOrEmpty(DayInput.WorkTeam))
                        {
                            this.drpWorkTeam.SelectedValue = DayInput.WorkTeam;
                        }
                        if (DayInput.Date != null)
                        {
                            this.txtDate.Text = string.Format("{0:yyyy-MM-dd}", DayInput.Date);
                        }
                        if (DayInput.DayAmount != null)
                        {
                            this.txtDayAmount.Text = DayInput.DayAmount.ToString();
                        }
                    }
                }
                else
                {
                    this.txtDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion
        protected void drpDrawingNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Model.QuantityManagement_Drawing drawing = BLL.DrawingService.GetDrawingById(this.drpDrawingNo.SelectedValue);
            Model.QuantityManagement_Change change = BLL.ChangeService.GetChangeById(this.drpDrawingNo.SelectedValue);
            if (drawing != null)
            {
                this.txtWorkSection.Text = drawing.WorkSection;
                this.txtDrawingName.Text = drawing.DrawingName;
                BLL.BaseService.InitPartDropDownList(this.drpPart, drawing.DrawingId, true);
                this.drpPart.SelectedValue = BLL.Const._Null;
            }
            else if (change != null)
            {
                this.txtWorkSection.Text = change.WorkSection;
                this.txtDrawingName.Text = change.ChangeName;
                BLL.BaseService.InitPartDropDownList(this.drpPart, change.ChangeId, true);
                this.drpPart.SelectedValue = BLL.Const._Null;
            }
            else 
            {
                this.txtWorkSection.Text = string.Empty;
                this.txtDrawingName.Text = string.Empty;
                this.drpPart.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpPart);
                this.drpPart.SelectedValue = BLL.Const._Null;
            }
            this.drpProjectContent.Items.Clear();
            Funs.FineUIPleaseSelect(this.drpProjectContent);
            this.drpProjectContent.SelectedValue = BLL.Const._Null;
            this.txtUnit.Text = string.Empty;
            this.txtAmount.Text = string.Empty;
            this.drpWorkTeam.SelectedValue = BLL.Const._Null;
        }
        protected void drpPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpPart.SelectedValue != BLL.Const._Null)
            {
                BLL.BaseService.InitProjectContentDropDownList(this.drpProjectContent, this.drpDrawingNo.SelectedValue, this.drpPart.SelectedValue, true);
                this.drpProjectContent.SelectedValue = BLL.Const._Null;
            }
            else
            {
                this.drpProjectContent.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpProjectContent);
                this.drpProjectContent.SelectedValue = BLL.Const._Null;
            }
            this.txtUnit.Text = string.Empty;
            this.txtAmount.Text = string.Empty;
            this.drpWorkTeam.SelectedValue = BLL.Const._Null;
        }
        protected void drpProjectContent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpProjectContent.SelectedValue != BLL.Const._Null)
            {
                Model.QuantityManagement_Base b = BLL.BaseService.GetBaseById(this.drpProjectContent.SelectedValue);
                if (b != null)
                {
                    this.txtUnit.Text = b.Unit;
                    decimal usedAmount = (from x in Funs.DB.QuantityManagement_DayInput where x.BaseId == this.drpProjectContent.SelectedValue select x).ToList().Sum(x => x.DayAmount ?? 0);
                    if (b.Amount != null)
                    {
                        this.txtAmount.Text = (b.Amount - usedAmount).ToString();
                        this.txtDayAmount.MaxValue = Convert.ToDouble(b.Amount - usedAmount);
                    }
                    this.drpWorkTeam.SelectedValue = b.WorkTeam;
                }
            }
            else
            {
                this.drpProjectContent.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpProjectContent);
                this.drpProjectContent.SelectedValue = BLL.Const._Null;
                this.txtUnit.Text = string.Empty;
                this.txtAmount.Text = string.Empty;
                this.drpWorkTeam.SelectedValue = BLL.Const._Null;
            }
        }
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpDrawingNo.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择图号", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpPart.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择部位", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpProjectContent.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择项目内容", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpWorkTeam.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择实际作业队", MessageBoxIcon.Warning);
                return;
            }
            Model.QuantityManagement_DayInput newDayInput = new Model.QuantityManagement_DayInput();
            newDayInput.ProjectId = this.CurrUser.LoginProjectId;
            newDayInput.BaseId = this.drpProjectContent.SelectedValue;
            newDayInput.Date = Funs.GetNewDateTimeOrNow(this.txtDate.Text.Trim());
            newDayInput.DayAmount = Funs.GetNewDecimal(this.txtDayAmount.Text.Trim());
            newDayInput.WorkTeam = this.drpWorkTeam.SelectedValue;
            if (string.IsNullOrEmpty(this.DayInputId))
            {
                if (string.IsNullOrEmpty(this.hdDayInputId.Text))
                {
                    newDayInput.DayInputId = SQLHelper.GetNewID(typeof(Model.QuantityManagement_DayInput));
                }
                else
                {
                    newDayInput.DayInputId = this.hdDayInputId.Text;
                }
                newDayInput.CompileMan = this.CurrUser.UserId;
                newDayInput.CompileDate = DateTime.Now;
                //var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == newDayInput.DayInputId);
                //if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                //{
                //    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                //    return;
                //}
                BLL.DayInputService.AddDayInput(newDayInput);
            }
            else
            {
                newDayInput.DayInputId = this.DayInputId;
                //var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.DayInputId);
                //if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                //{
                //    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                //    return;
                //}
                var oldDayInput = Funs.DB.QuantityManagement_DayInput.Where(u => u.DayInputId == this.DayInputId).FirstOrDefault();
                if (oldDayInput == null)
                {
                    newDayInput.CompileMan = this.CurrUser.UserId;
                    newDayInput.CompileDate = DateTime.Now;
                    BLL.DayInputService.AddDayInput(newDayInput);
                }
                else
                {
                    newDayInput.CompileMan = oldDayInput.CompileMan;
                    newDayInput.CompileDate = oldDayInput.CompileDate;
                    BLL.DayInputService.UpdateDayInput(newDayInput);
                }
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DayInputMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdDayInputId.Text))   //新增记录
            {
                this.hdDayInputId.Text = SQLHelper.GetNewID();
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/JDGL/DayInput&menuId={1}", this.hdDayInputId.Text, BLL.Const.DayInputMenuId)));
        }
        #endregion
    }
}