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

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MWebList : System.Web.UI.Page
    {
        Stat.Cache.IConfig config = Stat.Cache.CacheFatory.CreateConfig();
        Model.PageInfo pageInfo = new Stat.Model.PageInfo();
        Model.SearchInfo searchInfo = new Stat.Model.SearchInfo();
        Stat.Security.ISecurity security = Security.DataFatory.CreateSecurity();
        Stat.Cache.IRemoveCache removeCache = Stat.Cache.CacheFatory.CreateRemoveCache();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            GetWebList();//列表 
        }
        /// <summary>
        /// 站点列表
        /// </summary>
        private void GetWebList()
        {
            FillToGvWebList();
        }
        /// <summary>
        /// 分页
        /// </summary>
        protected void FillToGvWebList()
        {
            /*----------------------------------------------------*/
            pageInfo.PageIndex = Plugin.Common.Rq_Int("page") < 1 ? 1 : Plugin.Common.Rq_Int("page");

            pageInfo.PageSize = 15;

            searchInfo.KeyWord = this.txtW.Text.Trim();

            /*----------------------------------------------------*/


            this.gvWebList.DataSource = Stat.BLL.WebsitePageIndex.GetInSearch(ref pageInfo, searchInfo);
            this.gvWebList.DataKeyNames = new string[] { "WebId" };
            this.gvWebList.DataBind();

            this.PageIndex.Text = JPageIndex.GetIndex.Get(pageInfo.RecordCount, pageInfo.PageIndex, pageInfo.PageSize, 0, 1, "");

        }
        /// <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='';");
                //给行中控件加属性
                ((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick", "return confirm('确认要删除吗？');");
            }
        }



        /// <summary>
        /// 
        /// </summary>
        protected void AddUser(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "AddUser")
            {
                this.pnWebList.Visible = false;
                this.pnAddUser.Visible = true;
                this.hdfWebid.Value = e.CommandArgument.ToString();
            }
        }

        ///<summary>
        /// 该用户是否可用
        ///</summary>
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (Convert.ToInt32(Stat.BLL.Users.CheckUserName(Plugin.Common.SafeRequest(this.txtUserName.Text.ToString()).Trim().ToString())) > 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        protected void btnSaveUser_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Stat.Model.Users model = new Stat.Model.Users();
                model.UserName = Plugin.Common.SafeRequest(this.txtUserName.Text.ToString()).Trim().ToString();
                model.UserPwd = this.txtUserPWD1.Text.ToString();
                model.UserPower = "," + this.hdfWebid.Value.ToString();
                model.UserEmail = "";
                model.UserQQ = "";
                model.IsPwdMD5 = config.IsUserPwdMd5();
                if (Stat.BLL.Users.Add(model))
                {
                    Plugin.Common.Alert("操作成功！", "?");
                }
                else
                {
                    Plugin.Common.Alert("操作失败！");
                }
            }
        }
        /// <summary>
        /// 删除站点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWebList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(this.gvWebList.DataKeys[e.RowIndex][0].ToString());
            if (Stat.BLL.WebSites.Delete(id))
            {
                Plugin.Common.Alert("操作成功！", "?");
            }
            else
            {
                Plugin.Common.Alert("操作失败！");
            }
        }
        /// <summary>
        /// 修改站点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            Stat.Model.WebSites model = new Stat.Model.WebSites();
            model.WebName = Plugin.Common.SafeRequest(this.txtWebName.Text.ToString());
            model.WebUrl = this.txtWebUrl.Text.ToString();
            model.WebId = Convert.ToInt32(this.hdfEditWebid.Value.ToString());
            model.OpenItem = Plugin.Common.Rq("website_power");
            model.ShowStype = Plugin.Common.Rq("ShowStype");

            if (Stat.BLL.WebSites.Edit(model))
            {
                removeCache.RemoveWebsiteCache(model.WebId);
                Plugin.Common.Alert("修改成功！", "?");
            }
            else
            {
                Plugin.Common.Alert("操作失败！");
            }
        }
        /// <summary>
        /// 修改命令
        /// </summary>
        protected void gvWebList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.pnWebList.Visible = false;
            this.pnAddUser.Visible = false;
            this.pnEditWeb.Visible = true;
            
            string keyid = this.gvWebList.DataKeys[e.NewEditIndex][0].ToString();
            IList<Stat.Model.WebSites> list = Stat.BLL.WebSites.GetWebSiteInfo(int.Parse(keyid));
            if (list.Count > 0)
            {
                this.txtWebName.Text = list[0].WebName;
                this.txtWebUrl.Text = list[0].WebUrl;
                this.hdfEditWebid.Value = list[0].WebId.ToString();
                this.ltrPowerList.Text = security.GetWebsitePowerList(list[0].OpenItem);               
            }
        }
        /// <summary>
        /// 绑定行后激发
        /// </summary>
        protected void gvWebList_RowDataBound1(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='';");
                ((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick", "return confirm('确认要删除吗？');");
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            FillToGvWebList();
        }
    }
}
