using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class UsingPlanEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string UsingPlanId
        {
            get
            {
                return (string)ViewState["UsingPlanId"];
            }
            set
            {
                ViewState["UsingPlanId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.drpOrderTime.DataTextField = "Text";
                this.drpOrderTime.DataValueField = "Value";
                this.drpOrderTime.DataSource = BLL.DropListService.OrderTimeList();
                this.drpOrderTime.DataBind();
                Funs.FineUIPleaseSelect(this.drpOrderTime);
                WorkAreaService.InitWorkAreaByProjectId(this.drpWorkArea, this.CurrUser.LoginProjectId, true);
                ///材质
                BLL.Base_MaterialService.InitMaterialDropDownList(this.drpSheet, true, this.CurrUser.LoginProjectId);
                //使用位置
                var position = from x in Funs.DB.PW_IsoInfo
                               where x.ProjectId == this.CurrUser.LoginProjectId
                               orderby x.ISO_IsoNo
                               select x;
                this.drpUsePosition.DataValueField = "ISO_IsoNo";
                this.drpUsePosition.DataTextField = "ISO_IsoNo";
                this.drpUsePosition.DataSource = position;
                this.drpUsePosition.DataBind();
                Funs.FineUIPleaseSelect(this.drpUsePosition);
                this.txtOrderDate.MinDate = DateTime.Now;
                this.UsingPlanId = Request.Params["UsingPlanId"];
                if (!string.IsNullOrEmpty(this.UsingPlanId))
                {
                    Model.Weld_UsingPlan usingPlan = BLL.UsingPlanService.GetUsingPlanById(this.UsingPlanId);
                    if (usingPlan != null)
                    {
                        if (!string.IsNullOrEmpty(usingPlan.WeldId))
                        {
                            this.hdWeldId.Text = usingPlan.WeldId;
                            var weld = BLL.Base_ConsumablesService.GetConsumablesByConsumablesId(usingPlan.WeldId);
                            if (weld != null)
                            {
                                this.txtWeldCode.Text = weld.ConsumablesCode;
                                this.txtWeldName.Text = weld.ConsumablesName;
                                this.txtWeldSpec.Text = weld.SteelFormat;
                            }
                            if (!string.IsNullOrEmpty(usingPlan.UsingManOne))
                            {
                                this.hdUsingMan1.Text = usingPlan.UsingManOne;
                                Model.BS_Welder welder = BLL.WelderService.GetWelderById(this.hdUsingMan1.Text);
                                if (welder != null)
                                {
                                    this.hdUnitId.Text = welder.WED_Unit;
                                    this.txtUsingUnit.Text = BLL.UnitService.GetUnitNameByUnitId(welder.WED_Unit);
                                    this.hdTeamGroupId.Text = welder.TeamGroupId;
                                    this.txtTeamGroup.Text = BLL.TeamGroupService.GetTeamGroupNameByTeamGroupId(welder.TeamGroupId);
                                    this.txtUsingManOne.Text = welder.WED_Name;
                                }
                            }
                            if (!string.IsNullOrEmpty(usingPlan.WorkAreaId))
                            {
                                this.drpWorkArea.SelectedValue = usingPlan.WorkAreaId;
                                position = from x in Funs.DB.PW_IsoInfo
                                           where x.ProjectId == this.CurrUser.LoginProjectId && x.WorkAreaId == this.drpWorkArea.SelectedValue
                                           orderby x.ISO_IsoNo
                                           select x;
                                this.drpUsePosition.DataValueField = "ISO_IsoNo";
                                this.drpUsePosition.DataTextField = "ISO_IsoNo";
                                this.drpUsePosition.DataSource = position;
                                this.drpUsePosition.DataBind();
                                Funs.FineUIPleaseSelect(this.drpUsePosition);
                            }
                            if (!string.IsNullOrEmpty(usingPlan.UsePosition))
                            {
                                this.drpUsePosition.SelectedValue = usingPlan.UsePosition;
                            }
                            if (usingPlan.Amount.HasValue)
                            {
                                this.txtAmount.Text = usingPlan.Amount.ToString();
                            }
                            if (usingPlan.InPutDate.HasValue)
                            {
                                this.txtInPutDate.Text = string.Format("{0:yyyy-MM-dd}", usingPlan.InPutDate);
                            }
                            if (usingPlan.OrderDate.HasValue)
                            {
                                this.txtOrderDate.Text = string.Format("{0:yyyy-MM-dd}", usingPlan.OrderDate);
                            }
                            if (!string.IsNullOrEmpty(usingPlan.OrderTime))
                            {
                                this.drpOrderTime.SelectedValue = usingPlan.OrderTime;
                            }
                            if (usingPlan.CancelDate.HasValue)
                            {
                                this.txtCancelDate.Text = string.Format("{0:yyyy-MM-dd}", usingPlan.CancelDate);
                            }
                            if (usingPlan.IsSteelStru == true)
                            {
                                drpIsSteelStru.SelectedValue = "1";
                            }
                            else
                            {
                                drpIsSteelStru.SelectedValue = "0";
                            }
                            txtCancelResult.Text = usingPlan.CancelResult;
                            this.rblIsCancel.SelectedValue = (usingPlan.IsCancel == true ? "true" : "false");
                            if (!string.IsNullOrEmpty(usingPlan.STE_ID))
                            {
                                drpSheet.SelectedValueArray = usingPlan.STE_ID.Split(',');
                            }
                        }
                    }
                }
                else
                {
                    this.txtInPutDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    this.txtOrderDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData(false);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveData(true);
        }
        /// 
        /// 保存数据
        /// 
        private void SaveData(bool? isSubmit)
        {
            if (string.IsNullOrEmpty(this.hdWeldId.Text.Trim()))
            {
                Alert.ShowInTop("请选择焊材代号!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpWorkArea.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择施工区域!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUsePosition.SelectedValue == BLL.Const._Null && drpUsePosition.Text == "")
            {
                Alert.ShowInTop("请选择使用位置!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(this.hdUsingMan1.Text.Trim()))
            {
                Alert.ShowInTop("请选择领料人!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpSheet.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择焊件材质!", MessageBoxIcon.Warning);
                return;
            }
            if (drpOrderTime.SelectedValue == Const._Null)
            {
                Alert.ShowInTop("请选择预约时间!", MessageBoxIcon.Warning);
                return;
            }
            if (!string.IsNullOrEmpty(this.UsingPlanId))
            {
                var plan = BLL.UsingPlanService.GetUsingPlanById(this.UsingPlanId);
                if (plan.IsFinish == true)
                {
                    Alert.ShowInTop("该计划已完成,不能修改!", MessageBoxIcon.Warning);
                    return;
                }
            }
            Model.Weld_UsingPlan usingPlan = new Model.Weld_UsingPlan();
            if (!string.IsNullOrEmpty(this.hdWeldId.Text.Trim()))
            {
                usingPlan.WeldId = this.hdWeldId.Text.Trim();
            }
            usingPlan.ProjectId = this.CurrUser.LoginProjectId;
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null)
            {
                usingPlan.WorkAreaId = this.drpWorkArea.SelectedValue;
            }
            if (drpUsePosition.SelectedValue != null && drpUsePosition.SelectedValue != BLL.Const._Null)
            {
                usingPlan.UsePosition = this.drpUsePosition.SelectedValue;
            }
            else
            {
                if (drpUsePosition.Text != "")
                {
                    usingPlan.UsePosition = drpUsePosition.Text.Trim();
                }
            }
            usingPlan.UsingUnit = this.hdUnitId.Text;
            if (this.drpSheet.SelectedValue != BLL.Const._Null && this.drpSheet.SelectedValue != null)
            {
                List texts = new List();
                List values = new List();
                foreach (ListItem item in drpSheet.SelectedItemArray)
                {
                    texts.Add(item.Text);
                    values.Add(item.Value);
                }
                usingPlan.STE_ID = String.Join(",", values);
                usingPlan.STE_Name = String.Join(",", texts);
            }
            usingPlan.Amount = Funs.GetNewDecimal(this.txtAmount.Text.Trim());
            if (!string.IsNullOrEmpty(this.hdUsingMan1.Text.Trim()))
            {
                usingPlan.UsingManOne = this.hdUsingMan1.Text.Trim();
            }
            usingPlan.InPutDate = DateTime.Now;
            usingPlan.OrderDate = Funs.GetNewDateTime(this.txtOrderDate.Text.Trim());
            if (this.drpOrderTime.SelectedValue != BLL.Const._Null)
            {
                usingPlan.OrderTime = this.drpOrderTime.SelectedValue;
            }
            usingPlan.TeamGroupId = this.hdTeamGroupId.Text;
            usingPlan.CancelDate = Funs.GetNewDateTime(this.txtCancelDate.Text.Trim());
            usingPlan.IsCancel = Convert.ToBoolean(this.rblIsCancel.SelectedValue);
            usingPlan.CancelResult = this.txtCancelResult.Text.Trim();
            usingPlan.IsSubmit = isSubmit;
            usingPlan.InPutMan = CurrUser.UserId;
            usingPlan.Type = "1";
            if (drpIsSteelStru.SelectedValue == "1")
            {
                usingPlan.IsSteelStru = true;
            }
            else
            {
                usingPlan.IsSteelStru = false;
            }
            if (!string.IsNullOrEmpty(this.UsingPlanId))
            {
                var q = BLL.UsingPlanService.GetUsingPlanById(this.UsingPlanId);
                usingPlan.UsingPlanId = this.UsingPlanId;
                BLL.UsingPlanService.UpdateUsingPlan(usingPlan);
                BLL.LogService.AddSys_Log(this.CurrUser, usingPlan.InPutDate.ToString(), usingPlan.UsingPlanId, BLL.Const.CLGL_UsingPlanMenuId, "修改领料计划录入");
            }
            else
            {
                this.UsingPlanId = SQLHelper.GetNewID(typeof(Model.Weld_UsingPlan));
                usingPlan.UsingPlanId = this.UsingPlanId;
                BLL.UsingPlanService.AddUsingPlan(usingPlan);
                BLL.LogService.AddSys_Log(this.CurrUser, usingPlan.InPutDate.ToString(), usingPlan.UsingPlanId, BLL.Const.CLGL_UsingPlanMenuId, "添加领料计划录入");
                this.UsingPlanId = string.Empty;
                this.txtAmount.Text = string.Empty;
                this.txtWeldName.Text = string.Empty;
                this.txtWeldSpec.Text = string.Empty;
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 是否作废选择事件
        /// 
        /// 
        /// 
        protected void rblIsCancel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblIsCancel.SelectedValue == "true")
            {
                this.txtCancelDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            }
            else
            {
                this.txtCancelDate.Text = string.Empty;
            }
        }
        #endregion
        #region 查找焊材信息
        /// 
        /// 查找焊材信息
        /// 
        /// 
        /// 
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string window = String.Format("ShowPlanWeld.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.txtWeldCode.Text = weld.ConsumablesCode;
                    this.txtWeldName.Text = weld.ConsumablesName;
                    this.txtWeldSpec.Text = weld.SteelFormat;
                    //var weldType = BLL.WeldTypeService.GetWeldTypeById(weld.WeldTypeId);
                    //if (weldType != null)
                    //{
                    //    if (weldType.WeldTypeName == "焊条" || weldType.WeldTypeName == "焊丝")
                    //    {
                    //        this.txtAmount.Label = "数量(根)";
                    //    }
                    //    else
                    //    {
                    //        this.txtAmount.Label = "数量(Kg)";
                    //    }
                    //}
                }
                else
                {
                    this.txtWeldName.Text = string.Empty;
                    this.txtWeldSpec.Text = string.Empty;
                    txtWeldCode.Text = string.Empty;
                }
            }
            else
            {
                this.txtWeldName.Text = string.Empty;
                this.txtWeldSpec.Text = string.Empty;
                txtWeldCode.Text = string.Empty;
            }
        }
        #endregion
        #endregion
        #region 查找领料人
        /// 
        /// 查找领料人
        /// 
        /// 
        /// 
        protected void btnSelectUsingMan_Click(object sender, EventArgs e)
        {
            string window = String.Format("ShowUsingMan.aspx?projectId={0}", this.CurrUser.LoginProjectId, "编辑 - ");
            PageContext.RegisterStartupScript(Window2.GetSaveStateReference(hdUsingMan1.ClientID) + Window2.GetShowReference(window));
        }
        /// 
        /// 关闭领料人
        /// 
        /// 
        /// 
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            if (!string.IsNullOrEmpty(hdUsingMan1.Text))//第一领料人
            {
                Model.BS_Welder welder = BLL.WelderService.GetWelderById(this.hdUsingMan1.Text);
                if (welder != null)
                {
                    this.hdUnitId.Text = welder.WED_Unit;
                    this.txtUsingUnit.Text = BLL.UnitService.GetUnitNameByUnitId(welder.WED_Unit);
                    this.hdTeamGroupId.Text = welder.TeamGroupId;
                    this.txtTeamGroup.Text = BLL.TeamGroupService.GetTeamGroupNameByTeamGroupId(welder.TeamGroupId);
                    this.txtUsingManOne.Text = welder.WED_Name;
                }
                else
                {
                    this.txtUsingManOne.Text = string.Empty;
                }
            }
            else
            {
                this.txtUsingManOne.Text = string.Empty;
            }
        }
        #endregion
        protected void drpSheet_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (drpSheet.SelectedValueArray.Length > 1)
            {
                drpSheet.Items[0].Selected = false;
            }
            if (drpSheet.SelectedValueArray.Length == 0)
            {
                drpSheet.Items[0].Selected = true;
            }
        }
        #region 附件上传 暂不用
        /// 
        /// 上传附件资源
        /// 
        /// 
        /// 
        //protected void btnAttachUrl_Click(object sender, EventArgs e)
        //{
        //    string edit = "0"; // 表示能打开附件上传窗口,但不能上传附件
        //    if (this.drpProjectId.SelectedValue == Const._Null)
        //    {
        //        ShowNotify("请选择项目!");
        //    }
        //    else
        //    {
        //        PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/BaseInfo/Project&menuId={1}&edit={2}", this.drpProjectId.SelectedValue, Const.ProjectMenuId, edit)));
        //    }
        //}
        #endregion
        protected void drpWorkArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null)
            {
                var position = from x in Funs.DB.PW_IsoInfo
                               where x.ProjectId == this.CurrUser.LoginProjectId && x.WorkAreaId == this.drpWorkArea.SelectedValue
                               orderby x.ISO_IsoNo
                               select x;
                this.drpUsePosition.DataValueField = "ISO_IsoNo";
                this.drpUsePosition.DataTextField = "ISO_IsoNo";
                this.drpUsePosition.DataSource = position;
                this.drpUsePosition.DataBind();
                Funs.FineUIPleaseSelect(this.drpUsePosition);
            }
            else
            {
                var position = from x in Funs.DB.PW_IsoInfo
                               where x.ProjectId == this.CurrUser.LoginProjectId
                               orderby x.ISO_IsoNo
                               select x;
                this.drpUsePosition.DataValueField = "ISO_IsoNo";
                this.drpUsePosition.DataTextField = "ISO_IsoNo";
                this.drpUsePosition.DataSource = position;
                this.drpUsePosition.DataBind();
                Funs.FineUIPleaseSelect(this.drpUsePosition);
            }
            this.drpUsePosition.SelectedValue = BLL.Const._Null;
        }
    }
}