using System;
using System.Linq;
using BLL;

namespace FineUIPro.Web.UserGuide
{
    public partial class UserGuideAndQAEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                //上传人
                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.File_UserGuideAndQA t = BLL.UserGuideAndQAService.GetUserGuideAndQAById(fileId);
                    if (t != null)
                    {
                        this.txtFileName.Text = t.FileName;
                        
                        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.File_UserGuideAndQA temp = new Model.File_UserGuideAndQA();
            if (txtFileName.Text == string.Empty)
            {
                ShowNotify("File Name cannot be empty!", 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.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.UserGuideAndQAService.UpdateUserGuideAndQA(temp);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify User guide and QA!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    temp.FileId = this.hdId.Text.Trim();
                }
                else
                {
                    temp.FileId = SQLHelper.GetNewID(typeof(Model.File_UserGuideAndQA));
                    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.UserGuideAndQAService.AddUserGuideAndQA(temp);



                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add User guide and QA!");
                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.File_UserGuideAndQA));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader2.aspx?type=0&toKeyId={0}&path=FileUpload/File_UserGuideAndQA/&menuId={1}", this.hdId.Text, BLL.Const.UserGuideAndQAMenuId)));
        }
        #endregion
    }
}