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

namespace JuSNS.Web.system.app
{
    public partial class index : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "app";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                string actype = Request.QueryString["type"];
                int id=0;
                string value=Request.QueryString["value"];
                int.TryParse(Request.QueryString["id"], out id);

                if (!string.IsNullOrEmpty(actype))
                {
                    switch (actype.ToLower())
                    {
                        case "del":
                            App.Delete(id);
                            break;
                        case "lock":
                            if (string.IsNullOrEmpty(value) || value == "0")
                            {
                                App.UnLock(id);
                            }
                            else
                            {
                                App.Lock(id);
                            }
                            break;
                    }
                }


                bind();

                bindAppType();
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        protected void bind()
        {
            string pagecode = "manageAppList_aspx";

            JuSNS.Model.SqlConditionInfo sci = null;
            string key = Request.QueryString["key"];
            string ctype = Request.QueryString["c"];
            if (!string.IsNullOrEmpty(ctype) && ctype != "0")
            {
                sci = new JuSNS.Model.SqlConditionInfo("@ctype", Convert.ToInt32(ctype), TypeCode.Int32);
                pagecode = "manageTypeAppList_aspx";
            }

            if (!string.IsNullOrEmpty(key))
            {
                sci = new JuSNS.Model.SqlConditionInfo("@likekey", key, TypeCode.String);
                pagecode = "manageSearchAppList_aspx";
            }
            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+"?c=";
            if (!string.IsNullOrEmpty(ctype))
            {
                PageUrl += ctype;
            }
            if (key != null)
            {
                PageUrl += "&key=" + key;
            }
            
            if (PgCount > 1)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
                this.DivPage.Visible = true;
            }
        }
        protected string lockState(object islock,object toadmin)
        {
            string lockSTR = string.Empty;
            int n = Convert.ToInt32(islock);
            lockSTR = "正常";
            if (n == 1)
            {
                lockSTR= "锁定";
            }
            string adminSTR = "未提交";
            if (toadmin.ToString() == "1")
            {
                adminSTR = "<span style=\"color:Red;\">已提交</span>";
            }
            return lockSTR + "(" + adminSTR + ")";
        }

        protected string iSState(object appid, object toadmin)
        {
            string adminSTR = "|<span title=\"用户提交后才能审核\">审核</span>";
            if (toadmin.ToString() == "1")
            {
                adminSTR = "|<a href=\"javascript:Lock('"+appid.ToString()+"',0)\"><span style=\"color:Red;\">审核</span></a>";
            }
            return adminSTR;
        }
        /// <summary>
        /// 绑定应用类别
        /// </summary>
        protected void bindAppType()
        {
            string type = Request.QueryString["c"];
            DataTable dt = new DataTable();
            dt.Columns.Add("type", typeof(int));
            dt.Columns.Add("typestr", typeof(string));
            dt.Columns.Add("typeclass", typeof(string));
            DataRow dr = dt.NewRow();
            dr["typeclass"] = "";
            if (string.IsNullOrEmpty(type)||type=="0")
            {
                dr["typeclass"] = "current";
            }
            dr["type"] = 0;
            dr["typestr"] = "全部";
            dt.Rows.Add(dr);
            foreach (KeyValuePair<int, string> kv in JuSNS.Config.UserApiConfig.AppType)
            {
                dr = dt.NewRow();
                dr["typeclass"] = "";
                if (type == kv.Key.ToString())
                {
                    dr["typeclass"] = "current";
                }
                dr["type"] = kv.Key;
                dr["typestr"] = kv.Value;
                dt.Rows.Add(dr);
            }
            this.apptypelist.DataSource = dt;
            this.apptypelist.DataBind();            
        }
    }
}
