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

public partial class templateView : System.Web.UI.Page
{
    public string TemplateID = eParameters.QueryString("TemplateID");
    public string designJson = "";
    public string designData = "";
    public dynamic template = new eViewObject();

    public string RecordID = "";
    public dynamic record = new eViewObject();
    protected void Page_Load(object sender, EventArgs e)
    {
        eUser user = new eUser("Manage");
        user.Check();

        if (TemplateID.Length == 0)
        {
            TemplateID = eBase.DataBase.getValue("select top 1 TemplateID from a_eke_sysDataTemplates where deltag=0 order by addTime desc");
        }
        template = eBase.DataBase.getValues("select * from a_eke_sysDataTemplates where TemplateID='" + TemplateID + "'");
        designJson = template.designJson;
        designData = template.designData;
        RecordID = template.RecordID;


        //eBase.Writeln(designJson);
        //eBase.Write("<hr>");
         /*
          JsonData jd = JsonMapper.ToObject(designJson);
          foreach (JsonData sheet in jd)
          {
              eBase.Writeln(sheet["name"].ToString());
              if (sheet.Contains("config"))
              {
                  JsonData config = sheet["config"];
                  if (config.Contains("borderInfo"))
                  {
                      JsonData borderInfo  = config["borderInfo"];
                      JsonData borderInfo2 = JsonMapper.ToObject("[]");

                      eBase.Writeln("borderInfo" + borderInfo.Count.ToString());
                  }
              }
          }
         eBase.Writeln(jd.ToJson());
         */



       // eBase.Writeln(designJson);
        //eBase.End();

        eRecords ers = new eRecords(template.RecordIDS);
        List<LuckysheetData> allSheets = MultiSheetParser.ParseAllSheets(designJson);

        LuckysheetData firstSheet = allSheets[0];
        string html = HtmlExporter.GenerateTableHtml(firstSheet, ers.DataSource);
        litBody.Text = html;
     


    }
    public void sub2()
    {


        // 获取所有工作表名称
        List<string> sheetNames = LuckysheetHelper.GetSheetNames(designJson);
        //eBase.WriteLine("找到 " + sheetNames.Count + " 个工作表:");
        foreach (string name in sheetNames)
        {
            //eBase.WriteLine("  - " + name);
        }


        eBase.WriteHTML(designData);

        // 解析所有工作表
        List<LuckysheetData> allSheets = MultiSheetParser.ParseAllSheets(designData);
        //eBase.WriteLine("成功解析 " + allSheets.Count + " 个工作表");

        if (allSheets.Count > 0)
        {
            // 单个工作表导出
            LuckysheetData firstSheet = allSheets[0];
            //string html = HtmlExporter.ExportToHtml(firstSheet, firstSheet.Name ?? "第一个工作表");
            //eBase.Write(html);

            string html2 = HtmlExporter.GenerateTableHtml2(firstSheet);
            litBody.Text = html2;
            //eBase.Write(html2);
        }

        /*
        LuckysheetData data = LuckysheetParser.Parse(jsonText);
        string html = HtmlExporter.ExportToHtml(data, "我的工作表");
        eBase.Write(html);
        eBase.End();
        */
    }

}