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

namespace Stat.web.v
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Website1 : 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 id = Plugin.Common.Rq_Int("wid");
            if (id > 0)
            {//修改

                if (!IsPostBack)
                {
                    EditBox(id);
                }
            }
            else
            {//添加
                this.ltrPowerList.Text = security.GetWebsitePowerList("");
            }
            //  this.ltrShowTypeList.Text += Html.Systems.GetShowStype();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        private void EditBox(int id)
        {
            Stat.Model.WebSites model = Stat.BLL.WebSites.GetWebSiteInfo(id);
            if (model != null)
            {
                this.txtWebname.Text = model.WebName;
                this.txtWebUrl.Text = model.WebUrl;
                this.hdfWebid.Value = model.WebId.ToString();
                this.ltrPowerList.Text = security.GetWebsitePowerList(model.OpenItem);

            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveWebsite2_Click(object sender, EventArgs e)
        {
            Stat.Model.WebSites model = new Stat.Model.WebSites();

            model.WebId = int.Parse("0" + this.hdfWebid.Value);
            model.WebName = Plugin.Common.SafeRequest(this.txtWebname.Text.ToString());
            model.WebUrl = this.txtWebUrl.Text.ToString();
            model.UserId = Convert.ToInt32(security.GetCurrentUserID());
            model.OpenItem = Plugin.Common.Rq("website_power");
            //   model.IsReFresh = this.rBtnIsRefresh.SelectedValue == "1" ? true : false;
            //   model.IsCodeLock = this.rBtnCodeLock.SelectedValue == "1" ? true : false;
            model.ShowStype = Plugin.Common.Rq("ShowStype");
            if (model.WebId > 0)
            {
                if (Stat.BLL.WebSites.Edit(model))
                {
                    removeCache.RemoveWebsiteCache(model.WebId);
                    Plugin.Common.Alert("修改成功！", "WebsiteList.aspx");
                }
                else
                {
                    Plugin.Common.Alert("操作失败！");
                }
            }
            else
            {
                //先判断是否有 开放的 采集服务器没有？
                model.GatherServerID = BLL.GatherServer.Get_IsOpen_GathServerID();
                //
                if (model.GatherServerID > 0)
                {
                    if (Stat.BLL.WebSites.Add(model))
                    {
                        Plugin.Common.Alert("添加成功！");
                        security.ReloadCurrentUser();
                        Response.Redirect("WebsiteList.aspx", true);
                    }
                    else
                    {
                        Plugin.Common.Alert("操作失败！");
                    }
                }
                else
                {
                    Plugin.Common.Alert("因为没有可开放的统计服务器，无法添加站点！");
                }
            }
        }
    }
}
