using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TopWinCMS.BLL;
using TopWinCMS.Model;
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Admin.Soft
{
    public partial class Soft_Server : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();
        SoftServerManage MyClassBLL = new SoftServerManage();
        int ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ModelID = int.Parse(Request.QueryString["ModelID"]);
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkRight("DownLoadServer", true);

                Action = Request["Action"];

                GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);
                BindList();
            }
        }

        #region 当前状态
        private string Action
        {
            set
            {
                ViewState["Action"] = value;
            }
            get
            {
                if (ViewState["Action"] == null)
                {
                    return null;
                }
                else
                {
                    return ViewState["Action"].ToString();
                }
            }
        }
        #endregion

        #region 服务器ID
        private string SoftServerID
        {
            set
            {
                ViewState["SoftServerID"] = value;
            }
            get
            {
                if (ViewState["SoftServerID"] == null)
                {
                    return null;
                }
                else
                {
                    return ViewState["SoftServerID"].ToString();
                }
            }
        }
        #endregion

        public void BindList()
        {
            LB_Path.Text = Config.SiteName + "后台 → <a href=\"Soft_Server.aspx\">镜像服务器管理</a>";
            if (Action == null)
            {
                Panel1.Visible = true;
                Panel2.Visible = false;

                MyClassBLL.BindList(GV_List, ModelID);

            }
            else if (Action == "Add")
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                TB_ServerName.Text = "";
                TB_DownURL.Text = ""; TB_ServerHTML.Text = "";
                DDL_ServerGroup.Enabled = true;
                MyClassBLL.BindDDL(DDL_ServerGroup, "添加为服务器组", ModelID);

            }
            else if (Action == "Edit")
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                MyClassBLL.BindDDL(DDL_ServerGroup, "添加为服务器组", ModelID);
                SoftServerInfo model = MyClassBLL.EditLoad(SoftServerID);
                DDL_ServerGroup.SelectedValue = model.ID;
                DDL_ServerGroup.Enabled = false;
                TB_ServerName.Text = model.ServerName;
                TB_DownURL.Text = model.DownURL;
                TB_ServerHTML.Text = model.ServerHTML;
            }
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {


                TableCell myTableCell;
                //定义对“删除”单元格的引用
                myTableCell = e.Row.Cells[8];
                //定义对“删除”单元格中控件的引用
                LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
                //添加当单击时弹出确认对话框的自定义属性
                btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");

                myTableCell = e.Row.Cells[6];
                LinkButton LB_IsLock = (LinkButton)myTableCell.Controls[0];
                string strTemp = LB_IsLock.Text;

                bool flag;

                if (MyValidate.IsNumeric(strTemp) == true)
                {
                    int I = Convert.ToInt16(strTemp);
                    if (I == 1)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = Convert.ToBoolean(strTemp);
                }

                if (flag)
                {
                    LB_IsLock.Text = "锁定";
                    LB_IsLock.Attributes.Add("onclick", "return confirm('您确定要解锁？');");
                }
                else
                {
                    LB_IsLock.Text = "未锁定";
                    LB_IsLock.Attributes.Add("onclick", "return confirm('您确定要锁定？');");
                }


            }
        }



        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int Index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "IsLock")
            {
                string ID = GV_List.DataKeys[Index].Value.ToString();
                MyClassBLL.LockOrOpen(ID, UpdatePanel1);
            }

            if (e.CommandName == "EditSoftServer")
            {
                this.SoftServerID = GV_List.DataKeys[Index].Value.ToString();
                Action = "Edit";
            }
            if (e.CommandName == "DelSoftServer")
            {
                string ID = GV_List.DataKeys[Index].Value.ToString();
                MyClassBLL.Delete(ID);
            }
            BindList();
        }



        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Action = null;
            BindList();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Action = "Add";
            BindList();
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            if (DDL_ServerGroup.SelectedValue.Length > 0 && TB_ServerHTML.Text.Trim().Length == 0)
            {
                MsgBox.Alert("请输入下载名称", UpdatePanel1);
                return;
            }


            SoftServerInfo model = new SoftServerInfo();
            if (Action == "Add")
            {
                model.ID = "0";
            }
            else
            {
                model.ID = this.SoftServerID;
            }
            model.ModelID = ModelID.ToString();
            model.ParentID = DDL_ServerGroup.SelectedValue;
            model.ServerName = TB_ServerName.Text.Trim();
            model.DownURL = TB_DownURL.Text.Trim();
            model.ServerHTML = TB_ServerHTML.Text.Trim();

            MyClassBLL.Add(model);

            Action = null;
            BindList();
        }

        protected string Chang(object ServerName, object ParentID)
        {
            if (Convert.ToInt32(ParentID) != 0)
            {
                return "<IMG src=\"../images/tree/line.gif\" align=absMiddle>&nbsp;&nbsp;&nbsp;&nbsp;<IMG src=\"../images/tree/minusmiddle.gif\" align=absMiddle><IMG src=\"../images/tree/folder.gif\" align=absMiddle>" + ServerName.ToString();
            }
            else
            {
                return "<IMG src=\"../images/tree/plusmiddle.gif\" align=absMiddle><IMG src=\"../images/tree/folder.gif\" align=absMiddle>" + ServerName.ToString();
            }
        }
    }
}