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

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MStmp : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Plugin.Common.Rq("ac") == "Add")
                editBox(0);
            else
                GetList();//列表 
        }

        /// <summary>
        /// 添加/修改
        /// </summary>
        /// <param name="id"></param>
        private void editBox(int id)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfStmpId.Value = id.ToString();
            if (id > 0)
            {
                IList<Stat.Model.Stmp> list = Stat.BLL.Stmp.Get(id);
                if (list.Count > 0)
                {
                    this.txtStmp.Text = list[0].MailSmtp;
                    this.txtEName.Text = list[0].MailName;
                    this.txtEPass.Text = list[0].MailPass;
                    this.txtInfo.Text = list[0].MailContent;
                    this.rBtnState.Items.FindByValue(list[0].State.ToString()).Selected = true;
                }
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        private void GetList()
        {
            this.gvWebList.DataSource = Stat.BLL.Stmp.Get();
            this.gvWebList.DataKeyNames = new string[] { "id" };
            this.gvWebList.DataBind();

        }










        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            Stat.Model.Stmp model = new Stat.Model.Stmp();
            model.ID = Convert.ToInt32("0" + this.hdfStmpId.Value);
            model.MailSmtp = Plugin.Common.SafeRequest(this.txtStmp.Text);
            model.MailName = Plugin.Common.SafeRequest(this.txtEName.Text);
            model.MailPass = Plugin.Common.SafeRequest(this.txtEPass.Text);
            model.MailContent = Plugin.Common.SafeRequest(this.txtInfo.Text);
            model.State = Convert.ToInt32(this.rBtnState.SelectedValue);

            if (model.ID > 0)
            {
                if (Stat.BLL.Stmp.Update(model))
                    Plugin.Common.Alert("修改成功！", "?");
                else
                    Plugin.Common.Alert("修改失败！");
            }
            else
            {
                if (Stat.BLL.Stmp.Insert(model))
                      Plugin.Common.Alert("添加成功！", "?");
                else
                    Plugin.Common.Alert("添加失败！");
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        protected void gvWebList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                //给行加属性
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#66FF66';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';");

            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        protected void gvWebList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int keyid = int.Parse(this.gvWebList.DataKeys[e.RowIndex][0].ToString());
            if (Stat.BLL.Stmp.Delete(keyid))
                Plugin.Common.Alert("删除成功！", "?");
            else
                Plugin.Common.Alert("操作失败！");
        }
        /// <summary>
        /// 编辑
        /// </summary>
        protected void gvWebList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            int keyid = int.Parse(this.gvWebList.DataKeys[e.NewEditIndex][0].ToString());
            editBox(keyid);
        }
        /// <summary>
        /// 格式化样式
        /// </summary>
        protected string IsState(object o)
        {
            if (o.ToString() == "0")
            {
                return "<span style=\"color:#FF0000\">停止</span>";
            }
            else
            {
                return "正常";
            }
        }
    }
}
