using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.SES
{
    public partial class OneTimeSignedContracts : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();

                //文件类型
                this.drpFileType.DataTextField = "Text";
                this.drpFileType.DataValueField = "Value";
                this.drpFileType.DataSource = BLL.DropListService.getFileTypeDropList();
                this.drpFileType.DataBind();
                Funs.FineUIPleaseSelect(this.drpFileType);
                //上传人
                BLL.Sys_UserService.InitUserDropDownList(this.drpUploadMan, true);
                if (CurrUser.Account == Const.Gly)
                {
                    drpUploadMan.Enabled = true;
                }
                else
                {
                    drpUploadMan.SelectedValue = CurrUser.UserId;
                    drpUploadMan.Enabled = false;
                }

                string fileId = Request.Params["fileId"];
                if (!string.IsNullOrEmpty(fileId))
                {
                    Model.EMC_OneTimeSignedContracts signedContracts = BLL.OneTimeSignedContractsService.GetOneTimeSignedContractsById(fileId);
                    if (signedContracts != null)
                    {
                        this.hdId.Text = signedContracts.FileId;
                        if (!string.IsNullOrEmpty(signedContracts.OneTimeContractsId))
                        {
                            var data = BLL.OneTimeContractsService.GetOneTimeContractsById(signedContracts.OneTimeContractsId);
                            if (data != null)
                            {
                                this.txtFO.Text = data.ItemNumber;
                            }
                        }
                        this.txtFileName.Text = signedContracts.FileName;
                        if (!string.IsNullOrEmpty(signedContracts.FileType))
                        {
                            this.drpFileType.SelectedValue = signedContracts.FileType;
                        }
                        this.txtFileTypeCode.Text = signedContracts.FileTypeCode;
                        if (!string.IsNullOrEmpty(signedContracts.UploadMan))
                        {
                            this.drpUploadMan.SelectedValue = signedContracts.UploadMan;
                        }
                        this.txtUploadDate.Text = signedContracts.UploadDate.HasValue ? string.Format("{0:yyyy-MM-dd}", signedContracts.UploadDate) : "";
                        this.txtRemark.Text = signedContracts.Remark;
                    }
                }
                else
                {
                    string id = Request.Params["oneTimeContractsId"];
                    if (!string.IsNullOrEmpty(id))
                    {
                        var data = BLL.OneTimeContractsService.GetOneTimeContractsById(id);
                        if (data != null)
                        {
                            this.txtFO.Text = data.ItemNumber;
                        }
                    }
                    this.drpUploadMan.SelectedValue = this.CurrUser.UserId;
                    this.txtUploadDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string oneTimeContractsId = Request.Params["oneTimeContractsId"];
            string fileId = Request.Params["fileId"];
            Model.EMC_OneTimeSignedContracts newSignedContracts = new Model.EMC_OneTimeSignedContracts();
            newSignedContracts.OneTimeContractsId = oneTimeContractsId;
            newSignedContracts.FileName = this.txtFileName.Text.Trim();
            if (this.drpFileType.SelectedValue != BLL.Const._Null)
            {
                newSignedContracts.FileType = this.drpFileType.SelectedValue.Trim();
            }
            newSignedContracts.FileTypeCode = this.txtFileTypeCode.Text.Trim();
            if (this.drpUploadMan.SelectedValue != BLL.Const._Null)
            {
                newSignedContracts.UploadMan = this.drpUploadMan.SelectedValue;
            }
            newSignedContracts.UploadDate = Funs.GetNewDateTime(this.txtUploadDate.Text.Trim());
            newSignedContracts.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(fileId))
            {
                newSignedContracts.FileId = fileId;

                var att = from x in Funs.DB.AttachFile where x.ToKeyId == fileId select x;
                if (att.Count() > 0)
                {
                    newSignedContracts.AttachUrl = att.First().AttachUrl;
                }

                BLL.OneTimeSignedContractsService.UpdateOneTimeSignedContracts(newSignedContracts);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Signed Contracts!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newSignedContracts.FileId = this.hdId.Text.Trim();
                }
                else
                {
                    newSignedContracts.FileId = SQLHelper.GetNewID(typeof(Model.EMC_OneTimeSignedContracts));
                    this.hdId.Text = newSignedContracts.FileId;
                }

                var att = from x in Funs.DB.AttachFile where x.ToKeyId == newSignedContracts.FileId select x;
                if (att.Count() > 0)
                {
                    newSignedContracts.AttachUrl = att.First().AttachUrl;
                }

                BLL.OneTimeSignedContractsService.AddOneTimeSignedContracts(newSignedContracts);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Signed Contracts!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 附件上传
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.EMC_OneTimeSignedContracts));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader2.aspx?type=0&toKeyId={0}&path=FileUpload/SES/OneTimeContracts/SignedContracts&menuId={1}", this.hdId.Text, BLL.Const.OneTimeContractsMenuId)));
        }
        #endregion
    }
}