using System;
using System.Collections;
using System.Collections.Generic;

namespace Stat.web.v
{

    /// <summary>
    /// 
    /// </summary>
    public partial class WebsiteRole : System.Web.UI.Page
    {
        Stat.Security.ISecurity security = Stat.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)
        {
            int ruleid = Plugin.Common.Rq_Int("ruleid");
            if (ruleid > 0)
            { //修改
                if (!IsPostBack)
                {
                    EditBox(ruleid);
                }
            }
            else
            {//添加
                this.hdfWebid.Value = Plugin.Common.Rq_Int("wid").ToString();
            }

        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        private void EditBox(int id)
        {
            IList<Stat.Model.WebsiteStructure> list = Stat.BLL.WebsiteStructure.GetList(id);
            if (list.Count > 0)
            {
                if (!security.IsOperateWebSite(list[0].Webid)) { Response.Write("请不要手动修改URL参数！"); Response.End(); }
                /*---------------*/
                this.txtRuleName.Text = list[0].Name;
                this.txtRuleContent.Text = list[0].Rule;
                this.txtSort.Text = list[0].Sort.ToString();
                this.hdfRuleId.Value = list[0].id.ToString();
                this.hdfWebid.Value = list[0].Webid.ToString();

                this.rBtnIsUse.SelectedIndex = -1;
                this.rBtnIsUse.Items.FindByValue(list[0].IsUse.ToString()).Selected = true;

                this.rBtnType.SelectedIndex = -1;
                this.rBtnType.Items.FindByValue(list[0].Type.ToString()).Selected = true;
            }
            else
            {
                Response.Write("请不要手动修改URL参数！");
                Response.End();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveRule_Click(object sender, EventArgs e)
        {
            Stat.Model.WebsiteStructure model = new Stat.Model.WebsiteStructure();

            model.id = int.Parse("0" + this.hdfRuleId.Value);
            model.Webid = int.Parse("0" + this.hdfWebid.Value);
            model.Name = this.txtRuleName.Text.Trim();
            model.Rule = this.txtRuleContent.Text.Trim();
            model.Sort = Convert.ToInt32(this.txtSort.Text.Trim() == "" ? "0" : this.txtSort.Text.Trim());
            model.AddUserId = security.GetCurrentUserID();
            model.Type = this.rBtnType.SelectedValue == "1" ? 1 : 0;
            model.IsUse = this.rBtnIsUse.SelectedValue == "1" ? 1 : 0;
            if (model.id > 0)
                model.Data_Action = "Update";
            else
                model.Data_Action = "Insert";

            if (Stat.BLL.WebsiteStructure.WebsiteStructureInsertUpdateDelete(model) > 0)
            {
                //清除缓存
                removeCache.RemoveWebsiteCache(model.Webid);
                //清除缓存
                string gatherUrl = "http://" + BLL.WebSites.GetGatherServerUrl(model.Webid) + "/API/Clearsitecache.ashx?webid=" + model.Webid;
                Server.Execute(gatherUrl);
                //
                Plugin.Common.Alert("操作成功！", "WebsiteStructure.aspx?wid=" + model.Webid);
            }
            else
            {
                Plugin.Common.Alert("操作失败！");
            }
        }
    }
}

