using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EKETEAM.Data;
using EKETEAM.FrameWork;
using EKETEAM.UserControl;
using LitJson;

namespace eFrameWork.AppWebAPI
{
    public partial class Model : System.Web.UI.Page
    {
        public string AppItem = eParameters.Request("AppItem");
        public string ModelID = eParameters.Request("modelid");
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            eUser user;
            eModel model;           
            if (Request.Headers["auth"] == null) eResult.Message(new { success = 1, errcode = "1", message = "未携带ToKen(auth)!" });
            string auth = Request.Headers["auth"].ToString();
            if (auth.Length == 0) eResult.Message(new { success = 1, errcode = "1", message = "未携带ToKen(auth)!" });


            //eBase.AppendLog("Page_Load");
            DataRow[] appRows = eBase.a_eke_sysApplicationItems.Select("ApplicationItemID='" + AppItem + "'");
            if (appRows.Length == 0) eResult.Message(new { success = 1, errcode = "1", message = "参数错误(Appitem)!" }); 
            if(ModelID.Length==0) ModelID = appRows[0]["ModelID"].ToString();

            if (auth == "anonymous")
            {
                user = new eUser("", "00000000-0000-0000-0000-000000000000");
                user.Expires = DateTime.Now.AddDays(1);
                model = new eModel(AppItem, ModelID, user);
                model.Power["add"] = model.AnonymousAdd;
                model.Power["list"] = model.AnonymousList;
                model.Power["view"] = model.AnonymousView;
                model.Power["del"] = model.AnonymousDelete;
                model.Power["edit"] = model.AnonymousEdit;
            }
            else
            {
                eToken token = new eToken(auth);
                user = new eUser(token);
                model = new eModel(AppItem, ModelID, user);
            }
            model.Ajax = true;
            model.clientMode = "webapi";
            //eBase.AppendLog(model.Action);
            if (Request.ContentType.ToLower().IndexOf("/json") > -1 && 1==2)
            {
                model.WebAPISave();
            }
            else
            {
                switch (model.Action)
                {
                    case "":
                        model.WebAPIList();
                        break;
                    case "edit":
                    case "apiedit":
                        model.WebAPIEdit();
                        break;
                    case "view":
                    case "apiview":
                        model.WebAPIView();
                        break;
                    case "save":
                    case "apisave":
                        model.WebAPISave();
                        break;
                    case "del":
                    case "apidel":
                        model.WebAPIDelete();
                        break;
                    case "apicontrols":
                        model.WebAPIControls();
                        break;
                    case "procedure"://存储过程
                        model.WebAPIProcedure();
                        break;
                    case "records"://数据集
                        model.WebAPIRecords();
                        break;
                }
                eBase.End();
            }
        }

        protected void Page_Load2(object sender, EventArgs e)
        {
            eUser user = new eUser("Application");
            //eModel model = new eModel(AppItem, ModelID, user);
            eModel model = new eModel(user);
            model.Ajax = true;
            model.clientMode = "webapi";
            switch (model.Action)
            {
                case "":
                    model.WebAPIList();
                    break;
                case "edit":
                case "apiedit":
                    model.WebAPIEdit();
                    break;
                case "view":
                case "apiview":
                    model.WebAPIView();
                    break;
                case "save":
                case "apisave":
                    model.WebAPISave();
                    break;
                case "del":
                case "apidel":
                    model.WebAPIDelete();
                    break;
                case "apicontrols":
                    model.WebAPIControls();
                    break;
                case "procedure":

                    break;
            }
        }
    }
}