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.Model.ApiInterface;
using JuSNS.Common;
using JuSNS.ApiInterface;

namespace JuSNS.Web.dev
{
    public partial class appedit : JuSNS.UI.Page.UserPage
    {
        protected AppInfo theappinfo = new AppInfo();
        protected int id = 0;
        protected string avalExt = "jpg,gif,ico,bmp,jpeg,png";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!JuSNS.ApiInterface.Developer.IsEnabled(this.UserID))
                {
                    PageError("您已经是开发者，但需要等待管理员审核", rooDir + "/dev", true);
                }
                initform();
            }
        }
        protected void initform()
        {
            int.TryParse(Request.QueryString["id"], out id);
            if (id == 0)
            {
                this.PageError("参数错误", "index.aspx");
            }
            theappinfo = App.GetAppInfo(id);
            if (theappinfo.IsLock == 0)
            {
                this.PageError("审核通过的应用程序不能修改。", "index.aspx");
            }
            this.DataBind();
        }
        /// <summary>
        /// 提交修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void submit_click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int.TryParse(Request.Form["hid"], out id);
                if (id == 0)
                {
                    this.PageError("参数错误", Request.UrlReferrer.ToString());
                }
                AppInfo info = App.GetAppInfo(id);
                info.AppID = id;
                info.AppName = Request.Form["appname"];
                info.Description = Request.Form["description"];
                info.Url = Request.Form["url"];
                info.ToAdmin = 0;
                info.Mobile = string.Empty;
                info.Tel = string.Empty;
                info.Email = string.Empty;
                string apptype = Request.Form["apptype"];
                info.Type = string.IsNullOrEmpty(apptype) ? 1 : int.Parse(apptype);
                string filename = string.Empty;
                if (this.icon.HasFile)
                {
                    Public.UpdateIcon(this.icon.PostedFile, avalExt, out filename);
                    info.Icon = filename;
                }
                if (this.micon.HasFile)
                {
                    Public.UpdateIcon(this.micon.PostedFile, avalExt, out filename);
                    info.MidIcon = filename;
                }
                if (this.bicon.HasFile)
                {
                    Public.UpdateIcon(this.bicon.PostedFile, avalExt, out filename);
                    info.BigIcon = filename;
                }
                info.UserID = this.UserID;
                JuSNS.ApiInterface.Developer.EditApp(info);
                PageRight("修改成功！", rooDir + "/dev");
            }
        }
    }
}
