using System;
using System.Linq;
using BLL;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class StockInEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string StockInId
        {
            get
            {
                return (string)ViewState["StockInId"];
            }
            set
            {
                ViewState["StockInId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                BLL.UserService.InitUsersDropDownList(this.drpMaterialman, this.CurrUser.LoginProjectId, true, null);
                BLL.UserService.InitUserProjectIdRoleIdDropDownList(this.drpMaterialman, this.CurrUser.LoginProjectId,Const.SubWeldingEngineer+","+ Const.WeldingSystemManager + ","+Const.HJEngineer, true);
                //if (CurrUser.Account != Const.Gly)
                //{
                //    BLL.UnitStoreService.InitUnitStoreDropDownList(drpUnitStore, CurrUser.UnitId, true);
                //}
                //else
                //{
                BLL.UnitStoreService.InitUnitStoreDropDownList(drpUnitStore, null, true);
                //}
                //this.drpSupplier.DataTextField = "Text";
                //this.drpSupplier.DataValueField = "Value";
                //this.drpSupplier.DataSource = BLL.SupplierService.GetSupplierList();
                //this.drpSupplier.DataBind();
                //Funs.FineUIPleaseSelect(this.drpSupplier);
                this.StockInId = Request.Params["StockInId"];
                if (!string.IsNullOrEmpty(this.StockInId))
                {
                    Model.Weld_StockIn stockIn = BLL.StockInService.GetStockInById(this.StockInId);
                    if (stockIn != null)
                    {
                        if (!string.IsNullOrEmpty(stockIn.WeldId))
                        {
                            this.hdWeldId.Text = stockIn.WeldId;
                            var weld = BLL.Base_ConsumablesService.GetConsumablesByConsumablesId(stockIn.WeldId);
                            if (weld != null)
                            {
                                //this.txtWeldCode.Text = weld.WeldCode;
                                this.txtWeldName.Text = weld.ConsumablesName;
                                this.txtWeldSpec.Text = weld.SteelFormat;
                                this.txtWeldUnit.Text = weld.WeldUnit;
                                var t = BLL.DropListService.HJGL_ConsumablesTypeList().FirstOrDefault(x => x.Value == weld.ConsumablesType);
                                if (t != null)
                                {
                                    this.txtWeldType.Text = t.Text;
                                }
                            }
                            if (stockIn.Amount.HasValue)
                            {
                                this.txtAmount.Text = stockIn.Amount.ToString();
                            }
                            if (stockIn.Weight.HasValue)
                            {
                                this.txtWeight.Text = stockIn.Weight.ToString();
                            }
                            if (!string.IsNullOrEmpty(stockIn.Materialman))
                            {
                                this.drpMaterialman.SelectedValue = stockIn.Materialman;
                            }
                            if (stockIn.InStockDate.HasValue)
                            {
                                this.txtInStockDate.Text = string.Format("{0:yyyy-MM-dd}", stockIn.InStockDate);
                            }
                            if (stockIn.ReviewDate.HasValue)
                            {
                                this.txtReviewDate.Text = string.Format("{0:yyyy-MM-dd}", stockIn.ReviewDate);
                            }
                            //this.rblIsReview.SelectedValue = (stockIn.IsReview == true ? "true" : "false");
                            this.txtHeartNo.Text = stockIn.HeartNo;
                            //if (stockIn.IsReview == true)
                            //{
                            //    this.txtHeartNo.Hidden = false;
                            //}
                            //else
                            //{
                            //    this.txtHeartNo.Hidden = true;
                            //}
                            this.txtWarrantybook.Text = stockIn.Warrantybook;
                            this.txtMaterialCertificateNumber.Text = stockIn.MaterialCertificateNumber;
                            if (!string.IsNullOrEmpty(stockIn.UnitStoreId))
                            {
                                this.drpUnitStore.SelectedValue = stockIn.UnitStoreId;
                            }
                        }
                    }
                }
                else
                {
                    this.drpMaterialman.SelectedValue = this.CurrUser.UserId;
                    this.txtInStockDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    this.txtHeartNo.Hidden = false;
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.hdWeldId.Text == string.Empty)
            {
                Alert.ShowInTop("请选择焊材牌号!", MessageBoxIcon.Warning);
                return;
            }
            if (drpUnitStore.SelectedValue == Const._Null)
            {
                Alert.ShowInTop("请选择单位仓库!", MessageBoxIcon.Warning);
                return;
            }
            if (drpMaterialman.SelectedValue == Const._Null)
            {
                Alert.ShowInTop("请选择材料员!", MessageBoxIcon.Warning);
                return;
            }
            SaveData();
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存数据
        /// 
        private void SaveData()
        {
            Model.Weld_StockIn stockIn = new Model.Weld_StockIn();
            if (!string.IsNullOrEmpty(this.hdWeldId.Text.Trim()))
            {
                stockIn.WeldId = this.hdWeldId.Text.Trim();
            }
            stockIn.ProjectId = this.CurrUser.LoginProjectId;
            stockIn.Amount = Funs.GetNewDecimal(this.txtAmount.Text.Trim());
            stockIn.Weight = Funs.GetNewDecimal(this.txtWeight.Text.Trim());
            //stockIn.IsReview = Convert.ToBoolean(this.rblIsReview.SelectedValue);
            if (this.drpMaterialman.SelectedValue != BLL.Const._Null)
            {
                stockIn.Materialman = this.drpMaterialman.SelectedValue;
            }
            stockIn.InStockDate = Funs.GetNewDateTime(this.txtInStockDate.Text.Trim());
            stockIn.ReviewDate = Funs.GetNewDateTime(this.txtReviewDate.Text.Trim());
            stockIn.Warrantybook = this.txtWarrantybook.Text.Trim();
            //stockIn.Number = txtNumber.Text.Trim();
            stockIn.HeartNo = txtHeartNo.Text.Trim();
            stockIn.MaterialCertificateNumber = txtMaterialCertificateNumber.Text.Trim();
            if (this.drpUnitStore.SelectedValue != BLL.Const._Null)
            {
                stockIn.UnitStoreId = this.drpUnitStore.SelectedValue;
            }
            stockIn.Flag = "1";//入库
            if (!string.IsNullOrEmpty(this.StockInId))
            {
                stockIn.StockInId = this.StockInId;
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == this.StockInId select x;
                if (att.Count() > 0)
                {
                    stockIn.AttachUrl = att.First().AttachUrl;
                }
                var mat = from x in Funs.DB.Weld_UsingMat where x.StockInId == this.StockInId select x;
                if (mat.Count() > 0)
                {
                    // 焊材已有领用,不更新数量
                    BLL.StockInService.UpdateStockIn1(stockIn);
                }
                else
                {
                    BLL.StockInService.UpdateStockIn(stockIn);
                }
                BLL.LogService.AddSys_Log(this.CurrUser, stockIn.HeartNo, stockIn.StockInId, BLL.Const.CLGL_StockInMenuId, "修改焊材入库录入");
            }
            else
            {
                this.StockInId = SQLHelper.GetNewID(typeof(Model.Weld_StockIn));
                stockIn.StockInId = this.StockInId;
                BLL.StockInService.AddStockIn(stockIn);
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == this.StockInId select x;
                if (att.Count() > 0)
                {
                    BLL.StockInService.UpdateAttachFile(this.StockInId, att.First().AttachUrl);
                }
                BLL.LogService.AddSys_Log(this.CurrUser, stockIn.HeartNo, stockIn.StockInId, BLL.Const.CLGL_StockInMenuId, "添加焊材入库录入");
            }
        }
        #endregion        
        #region 附件上传
        /// 
        /// 上传附件资源
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            string edit = "0"; // 表示能打开附件上传窗口,但不能上传附件
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.CLGL_StockInMenuId, BLL.Const.BtnSave))
            {
                if (string.IsNullOrEmpty(this.StockInId))
                {
                    if (this.hdWeldId.Text == string.Empty)
                    {
                        Alert.ShowInTop("请选择焊材牌号!", MessageBoxIcon.Warning);
                        return;
                    }
                    SaveData();
                }
                edit = "1";
                PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/WeldMat/StockIn&menuId={1}&edit={2}", this.StockInId, Const.CLGL_StockInMenuId, edit)));
            }
        }
        #endregion
        #region 查找焊材信息
        /// 
        /// 查找焊材信息
        /// 
        /// 
        /// 
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string window = String.Format("ShowWeldInfo.aspx", "编辑 - ");
            PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdWeldId.ClientID) + Window1.GetShowReference(window));
        }
        #region 弹出编辑窗口关闭事件
        /// 
        /// 弹出编辑窗体关闭事件
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hdWeldId.Text))
            {
                var weld = BLL.Base_ConsumablesService.GetConsumablesByConsumablesId(this.hdWeldId.Text);
                if (weld != null)
                {
                    this.txtWeldName.Text = weld.ConsumablesName;
                    this.txtWeldSpec.Text = weld.SteelFormat;
                    this.txtWeldUnit.Text = weld.WeldUnit;
                    var t = BLL.DropListService.HJGL_ConsumablesTypeList().FirstOrDefault(x => x.Value == weld.ConsumablesType);
                    if (t != null)
                    {
                        this.txtWeldType.Text = t.Text;
                    }
                }
            }
        }
        #endregion
        #endregion
        #region 输入数量自动带出重量
        /// 
        /// 输入数量自动带出重量
        /// 
        /// 
        /// 
        protected void txtWeight_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtWeight.Text.Trim()))
            {
                if (!string.IsNullOrEmpty(this.hdWeldId.Text.Trim()))
                {
                    var weld = BLL.Base_ConsumablesService.GetConsumablesByConsumablesId(this.hdWeldId.Text.Trim());
                    if (weld != null)
                    {
                        if (weld.ConvertValue.HasValue)
                        {
                            decimal? amountCov = Funs.GetNewDecimal(this.txtWeight.Text.Trim()) / weld.ConvertValue;
                            int amount = Convert.ToInt32(Math.Floor(amountCov.Value));
                            this.txtAmount.Text = amount.ToString();
                        }
                        else
                        {
                            this.txtAmount.Text = this.txtWeight.Text.Trim();
                        }
                    }
                }
            }
        }
        #endregion
    }
}