using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class EMaterialRegistEditNew : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string EMaterialRegistId
        {
            get
            {
                return (string)ViewState["EMaterialRegistId"];
            }
            set
            {
                ViewState["EMaterialRegistId"] = value;
            }
        }
        private bool AppendToEnd = false;
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.EMaterialRegistId = Request.Params["EMaterialRegistId"];
                this.drpWME_ID.DataTextField = "ConsumablesName";
                this.drpWME_ID.DataValueField = "ConsumablesId";
                this.drpWME_ID.DataSource = (from x in Funs.DB.Base_Consumables orderby x.ConsumablesCode select x).ToList();
                this.drpWME_ID.DataBind();
                var units = UnitService.GetUnitByProjectIdUnitTypeList(this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2);
                this.dpUnit.DataTextField = "UnitName";
                this.dpUnit.DataValueField = "UnitId";
                this.dpUnit.DataSource = units;
                this.dpUnit.DataBind();
                Funs.FineUIPleaseSelect(dpUnit);
                this.cbJOTSch.DataTextField = "Text";
                this.cbJOTSch.DataValueField = "Value";
                this.cbJOTSch.DataSource = BLL.PW_JointInfoService.GetJOTSchList();
                this.cbJOTSch.DataBind();
                this.dpManufacturer.DataTextField = "ManufacturerName";
                this.dpManufacturer.DataValueField = "ManufacturerID";
                this.dpManufacturer.DataSource = Funs.DB.HJGL_EManufacturer.Where(x=>x.ProjectId==CurrUser.LoginProjectId).ToList();
                this.dpManufacturer.DataBind();
                Funs.FineUIPleaseSelect(dpManufacturer);
                if (!string.IsNullOrEmpty(this.EMaterialRegistId))
                {
                    var eMaterialRegist = BLL.HJGL_EMaterialRegistService.GetEMaterialRegistByID(this.EMaterialRegistId);
                    if (eMaterialRegist != null)
                    {
                        this.txtEMaterialRegistCode.Text = eMaterialRegist.EMaterialRegistCode;
                       // this.dpUnit.Text = eMaterialRegist.UnitName;
                        this.txtSystemCode.Text = eMaterialRegist.SystemCode;
                        this.txtPartACode.Text = eMaterialRegist.PartACode;
                        this.drpWME_ID.SelectedValue = eMaterialRegist.WMT_ID;
                        this.txtName.SelectedValue = eMaterialRegist.Name;
                        if (!string.IsNullOrEmpty(eMaterialRegist.SpecificationsModel))
                        {
                            this.dpSpecificationsModel.Values = eMaterialRegist.SpecificationsModel.Split(',');
                        }
                        this.txtMaterial.Text = eMaterialRegist.Material;
                        this.txtSpecialInstructions.Text = eMaterialRegist.SpecialInstructions;
                        this.dpUnit.SelectedValue = eMaterialRegist.Unit;
                        //this.txtUnit.Text = eMaterialRegist.Unit;
                        this.txtAmount.Text = eMaterialRegist.Amount;
                        this.txtFourLocation.Text = eMaterialRegist.FourLocation;
                        this.dpManufacturer.SelectedValue = eMaterialRegist.Manufacturer;
                        this.txtDesignUnit.Text = eMaterialRegist.DesignUnit;
                        this.txtRealArriveAmount.Text = eMaterialRegist.RealArriveAmount;
                        this.txtOutProve.Text = eMaterialRegist.OutProve;
                        this.txtOutPrices.Text = eMaterialRegist.OutPrices;
                        this.txtSupply.Text = eMaterialRegist.Supply;
                    }
                }
                else
                {
                }
            }
        }
        #endregion
        protected void dpUnit_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            string perfix = string.Empty;
            var unit = UnitService.GetUnitByUnitId(this.dpUnit.SelectedValue);
            perfix = unit.UnitCode + "-";
            this.txtSystemCode.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCode5ByProjectId", "dbo.HJGL_Batch_BatchTrust", "TrustBatchCode", this.CurrUser.LoginProjectId, perfix);           
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_EMaterialRegistMenuId, Const.BtnSave))
            {
                ShowNotify("您没有这个权限,请与管理员联系!");
                return;
            }
            if (string.IsNullOrEmpty(this.txtEMaterialRegistCode.Text))
            {
                ShowNotify("请输入材质证明单号!");
                return;
            }
            if (string.IsNullOrEmpty(this.txtSystemCode.Text))
            {
                ShowNotify("请输入系统编码!");
                return;
            }
            if (string.IsNullOrEmpty(this.txtPartACode.Text))
            {
                ShowNotify("请输入报审文件编号!");
                return;
            }
            if (string.IsNullOrEmpty(this.txtName.SelectedValue))
            {
                ShowNotify("请输入名称!");
                return;
            }
            
            if (string.IsNullOrEmpty(this.dpSpecificationsModel.Text))
            {
                ShowNotify("请输入规格型号!");
                return;
            }
            if (string.IsNullOrEmpty(this.txtMaterial.Text))
            {
                ShowNotify("请输入材质!");
                return;
            }
            //if (string.IsNullOrEmpty(this.txtUnit.Text))
            //{
            //    ShowNotify("请输入单位!");
            //    return;
            //}
            if (string.IsNullOrEmpty(this.txtAmount.Text))
            {
                ShowNotify("请输入到货数量!");
                return;
            }
             
            if (this.dpManufacturer.SelectedValue==Const._Null)
            {
                ShowNotify("请输入制造厂家!");
                return;
            }
            if (string.IsNullOrEmpty(this.EMaterialRegistId))
            {
                var temprm = Funs.DB.HJGL_EMaterialRegist.FirstOrDefault(x=>x.PartACode == this.txtPartACode.Text&&x.FourLocation == this.txtFourLocation.Text);
                if (temprm != null)
                {
                    ShowNotify("同一报验文件编号的焊材批号不能重复!");
                    return;
                }
            }
            else
            {
                var temprm = Funs.DB.HJGL_EMaterialRegist.FirstOrDefault(x => x.EMaterialRegistId != this.EMaterialRegistId && x.PartACode == this.txtPartACode.Text && x.FourLocation == this.txtFourLocation.Text);
                if (temprm != null)
                {
                    ShowNotify("同一报验文件编号的焊材批号不能重复!");
                    return;
                }
            }
            
            Model.HJGL_EMaterialRegist eMaterialRegist = new Model.HJGL_EMaterialRegist();
            eMaterialRegist.CompileDate = DateTime.Now;
            eMaterialRegist.EMaterialRegistDate = DateTime.Now;
            eMaterialRegist.ProjectId = this.CurrUser.LoginProjectId;
            eMaterialRegist.UnitName = this.dpUnit.SelectedText;
            eMaterialRegist.SystemCode = this.txtSystemCode.Text;
            eMaterialRegist.PartACode = this.txtPartACode.Text;
            eMaterialRegist.EMaterialRegistCode = this.txtEMaterialRegistCode.Text;
            eMaterialRegist.Name = this.txtName.SelectedText;
            if (this.dpSpecificationsModel.Values != null && this.dpSpecificationsModel.Values.Length > 0)
            {
                eMaterialRegist.SpecificationsModel = string.Join(",", this.dpSpecificationsModel.Values);
            }
            eMaterialRegist.Material = this.txtMaterial.Text;
            eMaterialRegist.SpecialInstructions = this.txtSpecialInstructions.Text;
            eMaterialRegist.Unit = this.dpUnit.SelectedValue;
            eMaterialRegist.Amount = this.txtAmount.Text;
            eMaterialRegist.FourLocation = this.txtFourLocation.Text;
            eMaterialRegist.Manufacturer = this.dpManufacturer.SelectedValue;
            eMaterialRegist.DesignUnit = this.txtDesignUnit.Text;
            eMaterialRegist.RealArriveAmount = this.txtRealArriveAmount.Text;
            eMaterialRegist.OutProve = this.txtOutProve.Text;
            eMaterialRegist.OutPrices = this.txtOutPrices.Text;
            eMaterialRegist.Supply = this.txtSupply.Text;
            eMaterialRegist.WMT_ID = this.drpWME_ID.SelectedValue;
            //修改
            if (!string.IsNullOrEmpty(this.EMaterialRegistId))
            {
                eMaterialRegist.EMaterialRegistId = this.EMaterialRegistId;
                var oldeMaterialRegist = Funs.DB.HJGL_EMaterialRegist.Where(x => x.EMaterialRegistId == this.EMaterialRegistId).FirstOrDefault();
                BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, oldeMaterialRegist.Unit, oldeMaterialRegist.WMT_ID, "", "", -decimal.Parse(oldeMaterialRegist.Amount));
                BLL.HJGL_EMaterialRegistService.UpdateEMaterialRegist(eMaterialRegist);
                BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, this.dpUnit.SelectedValue, eMaterialRegist.WMT_ID, "", "", decimal.Parse(eMaterialRegist.Amount));
                /// 删除到货明细
                //BLL.HJGL_EMaterialRegistService.DeleteEMaterialRegistItem(this.CurrUser.LoginProjectId, this.dpUnit.SelectedValue, this.EMaterialRegistId);
                //BLL.Sys_LogService.AddLog(BLL.Const.System_7, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "修改材料到货登记及验收记录!");
            }
            else  //添加
            {
                eMaterialRegist.EMaterialRegistId = SQLHelper.GetNewID(typeof(Model.HJGL_EMaterialRegist));
                eMaterialRegist.CompileMan = this.CurrUser.UserId;
                this.EMaterialRegistId = eMaterialRegist.EMaterialRegistId;
                BLL.HJGL_EMaterialRegistService.AddEMaterialRegist(eMaterialRegist);
                BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, this.dpUnit.SelectedValue, eMaterialRegist.WMT_ID, "", "", decimal.Parse(eMaterialRegist.Amount));
                //BLL.Sys_LogService.AddLog(BLL.Const.System_7, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "添加材料到货登记及验收记录!");
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    }
}