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

namespace Stat.web.Manager
{
    /// <summary>
    /// 
    /// </summary>
    public partial class GatherServer : System.Web.UI.Page
    {

        Model.GatherServer gatherModel = new Stat.Model.GatherServer();
        Model.PageInfo pageModel = new Stat.Model.PageInfo(100);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pageModel.PageIndex = 1;
            //
            if (Plugin.Common.Rq("ac") == "Add")
            {
                if (!IsPostBack)
                    editBox(0);
            }
            else
            {
                GetList();//列表 
            }
        }
        /// <summary>
        /// 添加/修改
        /// </summary>
        /// <param name="id"></param>
        private void editBox(int id)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfId.Value = id.ToString();
            if (id > 0)
            {
                gatherModel.ID = id;
                IList<Stat.Model.GatherServer> list = Stat.BLL.GatherServer.Get(gatherModel, ref pageModel);
                if (list.Count > 0)
                {
                    this.txtName.Text = list[0].ServerName;
                    this.txtUrl.Text = list[0].ServerDomain;
                    this.TextExp.Text = "";
                    this.txtSpeed.Text = list[0].Speed.ToString();
                    this.rBtnIsUse.Items.FindByValue(list[0].IsUse.ToString()).Selected = true;
                    this.rBtnIsOpen.Items.FindByValue(list[0].IsOpen.ToString()).Selected = true;
                }
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        private void GetList()
        {
            this.gvWebList.DataSource = Stat.BLL.GatherServer.Get(gatherModel, ref pageModel);
            this.gvWebList.DataKeyNames = new string[] { "id" };
            this.gvWebList.DataBind();

        }


        /// <summary>
        /// 保存
        /// </summary>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            Stat.Model.GatherServer model = new Stat.Model.GatherServer();

            model.ID = Convert.ToInt32("0" + this.hdfId.Value);
            model.ServerName = Plugin.Common.SafeRequest(this.txtName.Text);
            model.ServerDomain = Plugin.Common.SafeRequest(this.txtUrl.Text);
            model.Speed = Plugin.Common.ConvertToInt(this.txtSpeed.Text);
            model.IsUse = Convert.ToInt32(this.rBtnIsUse.SelectedValue);
            model.IsOpen = Convert.ToInt32(this.rBtnIsOpen.SelectedValue);

            if (model.ID > 0)
            {
                model.DbAction = "Update";
                if (Stat.BLL.GatherServer.InsertUpdateDelete(model) > 0)
                    Plugin.Common.Alert("修改成功！", "?");
                else
                    Plugin.Common.Alert("操作失败！");
            }
            else
            {
                model.DbAction = "Insert";
                if (Stat.BLL.GatherServer.InsertUpdateDelete(model) > 0)
                    Plugin.Common.Alert("添加成功！", "?");
                else
                    Plugin.Common.Alert("操作失败！");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWebList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int keyid = int.Parse(this.gvWebList.DataKeys[e.RowIndex][0].ToString());
            Model.GatherServer model = new Stat.Model.GatherServer();
            model.ID = keyid;
            model.DbAction = "Delete";
            if (Stat.BLL.GatherServer.InsertUpdateDelete(model) > 0)
                Plugin.Common.Alert("操作成功！", "?");
            else
                Plugin.Common.Alert("操作失败！");


        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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 IsUse(object o)
        {
            return "<a href=http://" + o + "/Status.ashx target=\"_blank\">查看</a>";
        }
        /// <summary>
        /// 格式化样式
        /// </summary>
        protected string IsOpen(object o)
        {
            if (o.ToString() == "0")
            {
                return "-";
            }
            else
            {
                return "<span style=\"color:#FF0000\">开放</span>";
            }
        }

    }
}
