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 JuSNS.Home;
using JuSNS.UI.Control;
using JuSNS.ApiInterface;

namespace JuSNS.Web.dev
{
    public partial class index : JuSNS.UI.Page.UserPage
    {
        protected bool isDeveloper = false;
        protected bool isEnabled = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            isDeveloper = Developer.IsDeveloper(UserID);
            isEnabled = Developer.IsEnabled(UserID);
            if (isDeveloper)
            {
                bind();
            }
            string req = Request.QueryString["req"];
            if (req == "1")
            {
                int isDev=0;
                Developer.RequestDeveloper(UserID, out isDev);
                if (isDev > 0)
                {
                    PageError("你已经是开发者了，无须申请。", rooDir + "/dev", true);
                }
                else
                {
                    string isdev = JuSNS.Common.Public.GetXMLValue("isDev");
                    if (isdev == "0")
                    {
                        PageRight("你已经成为了本平台的应用程序开发者了！但需要管理员审核", rooDir + "/dev");
                    }
                    else
                    {
                        PageRight("你已经成为了本平台的应用程序开发者了！", rooDir + "/dev/appnew.aspx");
                    }
                }
            }
            string type = Request.QueryString["type"];
            if (!string.IsNullOrEmpty(type))
            {
                int id = 0;
                int.TryParse(Request.QueryString["id"], out id);
                switch (type.ToLower())
                {
                    case "setkey":
                        Developer.ResetAppKey(id);
                        break;
                }
                Response.Redirect("index.aspx");
            }
            this.DataBind();
        }
        protected void bind()
        {
            string pagecode = "DevAppList_aspx";
            JuSNS.Model.SqlConditionInfo sci = new JuSNS.Model.SqlConditionInfo("@UserID", this.UserID, TypeCode.Int32);
            int ReCount, PgCount;
            int PageIndex = 1;
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            DataTable dt = UtilPage.GetPage(pagecode, PageIndex, PAGESZ, out ReCount, out PgCount, sci);
            this.rpt.DataSource = dt;
            this.rpt.DataBind();
            string PageUrl = Request.FilePath;
            if (PgCount > 1)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
        }


        protected string vState(object state)
        {
            int n = 0;
            try
            {
                n = Convert.ToInt32(state);
            }
            catch { }
            if (n == 1)
            {
                return "等待审核";
            }
            else
            {
                return "已审核";
            }
        }

        protected string vStateable(object toAdmin,object state,object UserID,object AppID)
        {
            int n = 0;
            try
            {
                n = Convert.ToInt32(state);
            }
            catch { }
            string aSTR = string.Empty;
            if (n == 1)
            {
                if (toAdmin.ToString() == "1")
                {
                    aSTR += "<input type=\"button\" value=\"已提交\" class=\"f-button\"  disabled=\"disabled\"  /> &nbsp; ";
                }
                else
                {
                    aSTR += "<input type=\"button\" value=\"提交给管理员\" class=\"f-button\" onclick=\"toadmin('" + AppID.ToString() + "');\" /> &nbsp; ";
                }
                aSTR += "<input type=\"button\" value=\"删除\" class=\"f-button\" onclick=\"deleteApp('" + UserID.ToString() + "','" + AppID.ToString() + "');\" /> &nbsp; ";
                aSTR += "<input type=\"button\" class=\"f-button\" value=\"编辑\" onclick=\"location.href='appedit.aspx?id=" + AppID.ToString() + "';\" />";
                return aSTR;
            }
            else
            {
                aSTR = "<input type=\"button\" value=\"删除\" disabled=\"disabled\" title=\"审核后不能删除\" class=\"f-button\"  /> &nbsp; ";
                aSTR += "<input type=\"button\" class=\"f-button\" disabled=\"disabled\" title=\"审核后不能编辑\"  value=\"编辑\" />";
                return aSTR;
            }
        }
        protected string getAppKey(object AppID)
        {
            string AppKey=Developer.GetAppKey(Convert.ToInt32(AppID));
            if (string.IsNullOrEmpty(AppKey))
            {
                return "未生成密钥";
            }
            return AppKey;
        }
    }
}
