using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.SES
{
    public partial class CTSalesContractManagement : 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();

                //文件类型
                BLL.FileTypeService.InitFileTypeDropDownList(this.drpFileType, true);
                //上传人
                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_CTSalesContractManagement contracts = BLL.CTSalesContractManagementService.GetCTSalesContractManagementById(fileId);
                    if (contracts != null)
                    {
                        this.hdId.Text = contracts.FileId;
                        if (!string.IsNullOrEmpty(contracts.CTSalesContractsId))
                        {
                            var data = BLL.CTSalesContractsService.GetCTSalesContractsById(contracts.CTSalesContractsId);
                            if (data != null)
                            {
                                this.txtFO.Text = data.ContractNumber;
                            }
                        }
                        this.txtFileName.Text = contracts.FileName;
                        if (!string.IsNullOrEmpty(contracts.FileTypeId))
                        {
                            this.drpFileType.SelectedValue = contracts.FileTypeId;
                        }
                        this.txtFileTypeCode.Text = contracts.FileTypeCode;
                        if (!string.IsNullOrEmpty(contracts.UploadMan))
                        {
                            this.drpUploadMan.SelectedValue = contracts.UploadMan;
                        }
                        this.txtUploadDate.Text = contracts.UploadDate.HasValue ? string.Format("{0:yyyy-MM-dd}", contracts.UploadDate) : "";
                        this.txtRemark.Text = contracts.Remark;
                    }
                }
                else
                {
                    string id = Request.Params["CTSalesContractsId"];
                    if (!string.IsNullOrEmpty(id))
                    {
                        var data = BLL.CTSalesContractsService.GetCTSalesContractsById(id);
                        if (data != null)
                        {
                            this.txtFO.Text = data.ContractNumber;
                        }
                    }
                    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 CTSalesContractsId = Request.Params["CTSalesContractsId"];
            string fileId = Request.Params["fileId"];
            Model.EMC_CTSalesContractManagement newContract = new Model.EMC_CTSalesContractManagement();
            newContract.CTSalesContractsId = CTSalesContractsId;
            newContract.FileName = this.txtFileName.Text.Trim();
            if (this.drpFileType.SelectedValue != BLL.Const._Null)
            {
                newContract.FileTypeId = this.drpFileType.SelectedValue;
            }
            newContract.FileTypeCode = this.txtFileTypeCode.Text.Trim();
            if (this.drpUploadMan.SelectedValue != BLL.Const._Null)
            {
                newContract.UploadMan = this.drpUploadMan.SelectedValue;
            }
            newContract.UploadDate = Funs.GetNewDateTime(this.txtUploadDate.Text.Trim());
            newContract.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(fileId))
            {
                newContract.FileId = fileId;
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == fileId select x;
                if (att.Count() > 0)
                {
                    newContract.AttachUrl = att.First().AttachUrl;
                }

                BLL.CTSalesContractManagementService.UpdateCTSalesContractManagement(newContract);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Contract Management!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newContract.FileId = this.hdId.Text.Trim();
                }
                else
                {
                    newContract.FileId = SQLHelper.GetNewID(typeof(Model.EMC_CTSalesContractManagement));
                    this.hdId.Text = newContract.FileId;
                }

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

                BLL.CTSalesContractManagementService.AddCTSalesContractManagement(newContract);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Contract Management!");
                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_CTSalesContractManagement));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader2.aspx?type=0&toKeyId={0}&path=FileUpload/SES/CTSalesContracts/ContractManagement&menuId={1}", this.hdId.Text, BLL.Const.CTSalesContractsMenuId)));
        }
        #endregion
    }
}