using System;
using System.Collections.Generic;
using System.Linq;
using BLL;

namespace FineUIPro.Web.UserGuide
{
    public partial class TemplatesEdit : 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 templateId = Request.Params["templateId"];
                if (!string.IsNullOrEmpty(templateId))
                {
                    Model.File_Template t = BLL.TemplatesService.GetTemplateById(templateId);
                    if (t != null)
                    {
                        this.txtTemplateName.Text = t.TemplateName;

                        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 = templateId;
                    }
                }
                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 templateId = Request.Params["templateId"];
            Model.File_Template temp = new Model.File_Template();
            if (txtTemplateName.Text == string.Empty)
            {
                ShowNotify("Template Name cannot be empty!", MessageBoxIcon.Warning);
                return;
            }
            else
            {
                temp.TemplateName = txtTemplateName.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(templateId))
            {
                temp.TemplateId = templateId;
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == templateId select x;
                if (att.Count() > 0)
                {
                    temp.AttachUrl = att.First().AttachUrl;
                }
                BLL.TemplatesService.UpdateTemplate(temp);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Template!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    temp.TemplateId = this.hdId.Text.Trim();
                }
                else
                {
                    temp.TemplateId = SQLHelper.GetNewID(typeof(Model.File_Template));
                    this.hdId.Text = temp.TemplateId;
                }
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == temp.TemplateId select x;
                if (att.Count() > 0)
                {
                    temp.AttachUrl = att.First().AttachUrl;
                }

                BLL.TemplatesService.AddTemplate(temp);



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