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

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MWebConfig : System.Web.UI.Page
    {
        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("id");
            if (!IsPostBack)
            {
                if (id > 0) { editBox(1); }
                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();

            IList<Stat.Model.System> list = Stat.BLL.Systems.Get();
            if (list.Count > 0)
            {
                this.pnBtnUpLoad.Visible = false;
                this.pnSignImg.Visible = true;
                this.txtWebName.Text = list[0].WebName;
                this.txtWebUrl.Text = list[0].WebUrl;
                this.txtImgPath.Text = list[0].WebLogo;
                this.txtStat.Text = list[0].Stat;
                this.txtFoot.Text = list[0].WebFoot;
                this.rBtnIsReg.Items.FindByValue(list[0].IsReg.ToString()).Selected = true;
                this.rBtnState.Items.FindByValue(list[0].IsUse.ToString()).Selected = true;
                this.txtCopy.Text = list[0].WebCopy;
                this.txtShowID.Text = list[0].ShowID.ToString();
                //
                if (this.rBtnIsMD5.Items.FindByValue(list[0].IsUserPwdMD5 ? "1" : "0") != null)
                    this.rBtnIsMD5.Items.FindByValue(list[0].IsUserPwdMD5 ? "1" : "0").Selected = true;
                //

            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        private void GetList()
        {
            this.gvWebList.DataSource = Stat.BLL.Systems.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.System model = new Stat.Model.System();

            model.ID = Convert.ToInt32("0" + this.hdfId.Value);
            model.WebName = Plugin.Common.SafeRequest(this.txtWebName.Text);
            model.WebUrl = Plugin.Common.SafeRequest(this.txtWebUrl.Text);
            model.WebLogo = Plugin.Common.SafeRequest(this.txtImgPath.Text);
            model.Stat = Plugin.Common.SafeRequest(this.txtStat.Text);
            model.IsUse = Convert.ToInt32(this.rBtnState.SelectedValue);
            model.IsReg = Convert.ToInt32(this.rBtnIsReg.SelectedValue);
            model.WebFoot = this.txtFoot.Text;
            model.WebCopy = this.txtCopy.Text;
            model.ShowID = Convert.ToInt32("0" + this.txtShowID.Text);

            model.IsUserPwdMD5 = this.rBtnIsMD5.SelectedValue == "1" ? true : false;
            removeCache.RemoveSysCache();

            if (model.ID > 0)
            {
                if (Stat.BLL.Systems.Update(model))
                    Plugin.Common.Alert("操作成功！", "?");
                else
                    Plugin.Common.Alert("操作失败！");
            }
            else
            {
                if (Stat.BLL.Systems.Insert(model))
                    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='';");
                //给行中控件加属性
                ((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm('确认要删除吗？');");
            }

        }
        /// <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());
            if (Stat.BLL.Systems.Delete(keyid))
                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 IsReg(object o)
        {
            if (o.ToString() == "0")
            {
                return "运行";
            }
            else
            {
                return "开放";
            }
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpFile1_Click(object sender, EventArgs e)
        {
            this.btnUpFileImg.Enabled = false;
            string res = Plugin.UpFile.FileUpAndReturnGetNewFileName(this.Page, this.fupd, "ver");
            this.pnBtnUpLoad.Visible = false;
            if (res != null)
            {
                this.pnSignImg.Visible = true;
                this.txtImgPath.Text = "/Upfile/" + res;
            }
            else
            {
                this.pnSignImg.Visible = false;
                this.pnBtnUpLoad.Visible = true;
            }
            this.btnUpFileImg.Enabled = true;
        }
        /// <summary>
        /// 重新上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rebtn1_Click(object sender, EventArgs e)
        {
            this.pnSignImg.Visible = false;
            this.pnBtnUpLoad.Visible = true;
        }
    }
}
