using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.QuantityManagement
{
    public partial class DrawingEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string DrawingId
        {
            get
            {
                return (string)ViewState["DrawingId"];
            }
            set
            {
                ViewState["DrawingId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                this.DrawingId = Request.Params["DrawingId"];
                BLL.CNProfessionalService.InitCNProfessionalDownList(drpMajor, true);
                var Drawing = BLL.DrawingService.GetDrawingById(this.DrawingId);
                if (Drawing != null)
                {
                    this.DrawingId = Drawing.DrawingId;
                    this.txtWorkSection.Text = Drawing.WorkSection;
                    this.txtDrawingNo.Text = Drawing.DrawingNo;
                    this.txtDrawingName.Text = Drawing.DrawingName;
                    this.drpMajor.SelectedValue = Drawing.Major;
                    this.txtAcceptDate.Text = Drawing.AcceptDate.HasValue ? string.Format("{0:yyyy-MM-dd}", Drawing.AcceptDate) : "";
                    if (Drawing.CompletionStatus == true)
                    {
                        this.cbCompletionStatus.Checked = true;
                    }
                    else
                    {
                        this.cbCompletionStatus.Checked = false;
                    }
                    if (Drawing.CompletionStatus2 == true)
                    {
                        this.cbCompletionStatus2.Checked = true;
                    }
                    else
                    {
                        this.cbCompletionStatus2.Checked = false;
                    }
                    this.txtDutyPerson.Text = Drawing.DutyPerson;
                    this.txtRemark.Text = Drawing.Remark;
                }
                else
                {
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpMajor.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择专业", MessageBoxIcon.Warning);
                return;
            }
            Model.QuantityManagement_Drawing newDrawing = new Model.QuantityManagement_Drawing();
            newDrawing.ProjectId = this.CurrUser.LoginProjectId;
            newDrawing.WorkSection = this.txtWorkSection.Text.Trim();
            newDrawing.DrawingNo = this.txtDrawingNo.Text.Trim();
            newDrawing.DrawingName = this.txtDrawingName.Text.Trim();
            newDrawing.Major = this.drpMajor.SelectedValue;
            if (!string.IsNullOrEmpty(this.txtAcceptDate.Text))
            {
                newDrawing.AcceptDate = Convert.ToDateTime(this.txtAcceptDate.Text);
            }
            if (cbCompletionStatus.Checked)
            {
                newDrawing.CompletionStatus = true;
            }
            else
            {
                newDrawing.CompletionStatus = false;
            }
            if (cbCompletionStatus2.Checked)
            {
                newDrawing.CompletionStatus2 = true;
            }
            else
            {
                newDrawing.CompletionStatus2 = false;
            }
            newDrawing.DutyPerson = this.txtDutyPerson.Text.Trim();
            newDrawing.Remark = this.txtRemark.Text.Trim();
            if (string.IsNullOrEmpty(this.DrawingId))
            {
                newDrawing.DrawingId = SQLHelper.GetNewID(typeof(Model.QuantityManagement_Drawing));
                newDrawing.CompileMan = this.CurrUser.UserId;
                newDrawing.CompileDate = DateTime.Now;
                //var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == newDrawing.DrawingId);
                //if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                //{
                //    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                //    return;
                //}
                BLL.DrawingService.AddDrawing(newDrawing);
            }
            else
            {
                newDrawing.DrawingId = this.DrawingId;
                //var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.DrawingId);
                //if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                //{
                //    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                //    return;
                //}
                var oldDrawing = Funs.DB.QuantityManagement_Drawing.Where(u => u.DrawingId == this.DrawingId).FirstOrDefault();
                if (oldDrawing == null)
                {
                    newDrawing.CompileMan = this.CurrUser.UserId;
                    newDrawing.CompileDate = DateTime.Now;
                    BLL.DrawingService.AddDrawing(newDrawing);
                }
                else
                {
                    newDrawing.CompileMan = oldDrawing.CompileMan;
                    newDrawing.CompileDate = oldDrawing.CompileDate;
                    BLL.DrawingService.UpdateDrawing(newDrawing);
                }
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(this.DrawingId))   //新增记录
            //{
            //    this.DrawingId = SQLHelper.GetNewID(typeof(Model.QuantityManagement_Drawing));
            //}
            //var oldDrawing = Funs.DB.QuantityManagement_Drawing.Where(u => u.DrawingId == this.DrawingId).FirstOrDefault();
            //if (oldDrawing == null || ((oldDrawing.CompileMan == CurrUser.UserId && oldDrawing.Status == BLL.Const.QuantityManagement_Compile) || (oldDrawing.CompileMan == CurrUser.UserId && oldDrawing.Status == BLL.Const.QuantityManagement_ReCompile)))
            //{
            //    PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/Drawing&menuId={1}", this.DrawingId, BLL.Const.DrawingMenuId)));
            //}
            //else
            //{
            //    PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=-1&toKeyId={0}&path=FileUpload/CQMS/Drawing&menuId={1}", this.DrawingId, BLL.Const.DrawingMenuId)));
            //}
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DrawingMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}