using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace FineUIPro.Web.PZHGL.ProjectData
{
    public partial class LayoutMapEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                //上传人
                UserService.InitUserDropDownList(drpUploadMan, CurrUser.LoginProjectId, false, Const.UnitId_CD);
                drpUploadMan.SelectedIndex = 0;
  
                string fileId = Request.Params["fileId"];
                if (!string.IsNullOrEmpty(fileId))
                {
                    Model.Common_FileManager t = BLL.FileManagerService.GetFileById(fileId);
                    if (t != null)
                    {
						this.txtFileName.Text = t.FileName;
						this.txtCode.Text = t.FileCode;

						if (!string.IsNullOrEmpty(t.UploadMan))
                        {
                            this.drpUploadMan.SelectedValue = t.UploadMan;
                        }
                        this.txtUploadDate.Text = t.UploadDate.HasValue ? string.Format("{0:yyyy-MM-dd}", t.UploadDate) : "";
                        this.txtRemark.Text = t.Remark;
                        hdId.Text = fileId;
                    }
                }
                else
                {
                    this.drpUploadMan.SelectedValue = this.CurrUser.UserId;
                    this.txtUploadDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }

        #region
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string fileId = Request.Params["fileId"];
            Model.Common_FileManager temp = new Model.Common_FileManager();
            if (txtFileName.Text == string.Empty)
            {
                ShowNotify("名称不能为空!", MessageBoxIcon.Warning);
                return;
            }
            else
            {
                temp.FileName = txtFileName.Text.Trim();
            }
            if (this.drpUploadMan.SelectedValue != BLL.Const._Null)
            {
                temp.UploadMan = this.drpUploadMan.SelectedValue;
            }
            temp.UploadDate = Funs.GetNewDateTime(this.txtUploadDate.Text.Trim());
            temp.ToMenu = "8";
            temp.ProjectId = this.CurrUser.LoginProjectId;

			temp.FileCode = this.txtCode.Text.Trim();
			temp.Remark = this.txtRemark.Text.Trim();
			if (!string.IsNullOrEmpty(fileId))
            {
                temp.FileId = fileId;
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == fileId select x;
                if (att.Count() > 0)
                {
                    temp.AttachUrl = att.First().AttachUrl;
                }
                BLL.FileManagerService.UpdateFile(temp);
                BLL.LogService.AddSys_Log(this.CurrUser, temp.FileName, temp.FileId, BLL.Const.PZHGL_LayoutMaptMenuId, BLL.Const.BtnModify);
                ShowNotify("保存成功!", MessageBoxIcon.Success);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    temp.FileId = this.hdId.Text.Trim();
                }
                else
                {
                    temp.FileId = SQLHelper.GetNewID(typeof(Model.Common_FileManager));
                    this.hdId.Text = temp.FileId;
                }
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == temp.FileId select x;
                if (att.Count() > 0)
                {
                    temp.AttachUrl = att.First().AttachUrl;
                }

                BLL.FileManagerService.AddFile(temp);
                BLL.LogService.AddSys_Log(this.CurrUser, temp.FileName, temp.FileId, BLL.Const.PZHGL_LayoutMaptMenuId, BLL.Const.BtnAdd);
                ShowNotify("保存成功!", 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.Common_FileManager));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/PZHGL/LschUrl/&menuId={1}", this.hdId.Text, BLL.Const.PZHGL_LayoutMaptMenuId)));
        }
        #endregion
    }
}