using NFine.Application;
using NFine.Application.SystemManage;
using NFine.Code;
using NFine.Domain.Entity.SystemManage;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace NFine.Web.Areas.SystemManage.Controllers
{
    public class CommonReportController : ControllerBase
    {
        private CommonReportApp App = new CommonReportApp();


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string sqlTxt, string DBName, string TempName)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = App.GetGridJson(sqlTxt, DBName, TempName, ref pagination);
            }
            catch (Exception ex)
            {
                dt = null;
                return Error(ex.ToString());
            }
            var data = new
            {
                rows = dt,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());

        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonNew(Pagination pagination, string MenuID, string sqlTxt_Condition, string DBName, string TempName)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = App.GetGridJsonNew(MenuID, sqlTxt_Condition, DBName, TempName, ref pagination);
            }
            catch (Exception ex)
            {
                dt = null;
                return Error(ex.ToString());
            }
            var data = new
            {
                rows = dt,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());

        }


        //导出
        [HttpPost]
        public void ExportAll(string sqlTxt, string DBName, string TempName, string MenuID)
        {
            sqlTxt = HttpUtility.UrlDecode(sqlTxt);

            DataTable data = App.ExportAll(sqlTxt, DBName, TempName, MenuID);
            if (data != null && data.Rows.Count > 0)
            {
                AsposeCell.Export(data);
            }

        }

        //导出
        [HttpPost]
        public void ExportAllNew(string sqlTxt, string DBName, string TempName, string MenuID)
        {
            sqlTxt = HttpUtility.UrlDecode(sqlTxt);

            DataTable data = App.ExportAllNew(sqlTxt, DBName, TempName, MenuID);
            if (data != null && data.Rows.Count > 0)
            {
                AsposeCell.Export(data);
            }

        }

        //获取数据源DB
        [HttpGet]
        public ActionResult GetAllDataBase()
        {
            try
            {
                DataTable data = App.GetAllDataBase();
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Error(ex.ToString());
            }
        }

        //获取菜单Name
        [HttpGet]
        public ActionResult GetMenuID(string MenuTag)
        {
            try
            {
                DataTable data = App.GetMenuID(MenuTag);
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }


        //获取数据源DB
        [HttpGet]
        public ActionResult GetMenu(string MenuID)
        {
            try
            {
                DataTable Menu = App.GetMenuSQL(MenuID);

                if (Menu != null && Menu.Rows.Count > 0)
                {
                    string MenuCols = App.GetMenuColsNew(Menu.Rows[0]["SourceID"].ToString());
                    DataTable formatcols = App.Getformatcols(Menu.Rows[0]["SourceID"].ToString());
                    string UnVisiblecols = App.UnVisiblecols(Menu.Rows[0]["SourceID"].ToString());
                    String[] Mergercols = { };
                    if (!string.IsNullOrEmpty(Menu.Rows[0]["Mergercols"].ToString()))
                    {
                        Mergercols = Menu.Rows[0]["Mergercols"].ToString().Split(',').ToArray();
                    }
                    String[] footercols = { };
                    if (!string.IsNullOrEmpty(Menu.Rows[0]["footercols"].ToString()))
                    {
                        footercols = Menu.Rows[0]["footercols"].ToString().Split(',').ToArray();
                    }
                    var data = new
                    {
                        sqlTxt = Menu.Rows[0]["sqlTxt"].ToString(),
                        MenuID = Menu.Rows[0]["MenuID"].ToString(),
                        Cols = MenuCols,
                        HiddenCols = UnVisiblecols,
                        ReportName = Menu.Rows[0]["ReportName"].ToString(),
                        Mergercols = Mergercols,
                        XCol = Menu.Rows[0]["XCol"].ToString(),
                        footercols = footercols,
                        TempName = Menu.Rows[0]["TempName"].ToString(),
                        formatcols = formatcols,
                        footerrow = Menu.Rows[0]["footerrow"].ToString().ToBool(),
                        IsExistsDataSource = Menu.Rows[0]["IsExistsDataSource"].ToString(),
                        DBName = Menu.Rows[0]["DBName"].ToString(),
                        SourceID = Menu.Rows[0]["SourceID"].ToString(),
                    };
                    return Content(data.ToJson());
                }
                else
                {
                    return Content("无菜单数据");
                }

            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }


        //获取栏位的栏位筛选信息
        [HttpGet]
        public ActionResult GetColsSelectGrid(string SourceID, string ColsName, string InputKey)
        {
            try
            {
                DataTable dt = App.GetColsSelectData(SourceID, ColsName, InputKey);

                if (dt != null && dt.Rows.Count > 0)
                {
                    string Cols = App.GetColsSelectCols(dt);
                    var data = new
                    {
                        cols = Cols,
                        gridData = dt,

                    };
                    return Content(data.ToJson());
                }
                else
                {
                    return Content("无菜单数据");
                }

            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }



        //获取数据源sql等数据
        [HttpGet]
        public ActionResult GetMenuSQL(string MenuID)
        {
            try
            {
                DataTable data = App.GetMenuSQL(MenuID);
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Error(ex.ToString());
            }
        }


        //提交数据源信息
        [HttpPost]
        public ActionResult SetDataSource(string SysDataSourceFlag, string FormatSql, string DbId, string MenuID, string DbParameters, string SourceID, string TempName, string XCol)
        {
            try
            {
                App.SetDataSource(SysDataSourceFlag, FormatSql, DbId, MenuID, DbParameters, SourceID, TempName, XCol);
                return Content("操作成功");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }



        [HttpPost]
        public ActionResult SetDataSourceText(string SysDataSourceFlag, string FormatSql, string DbId, string DbParameters, string TempName, string XCol)
        {
            try
            {
                App.SetDataSourceText(SysDataSourceFlag, FormatSql, DbId, DbParameters, TempName, XCol);
                return Content("OK");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }

        //获取过滤数据
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFilter(Pagination pagination, string SourceID)
        {

            DataTable data = App.GetFilter(SourceID);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = data
            };
            return Content(JsonData.ToJson());
        }



        //获取过滤条件详情
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetCondition(Pagination pagination, string SourceID)
        {

            DataTable data = App.GetCondition(SourceID);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = data
            };
            return Content(JsonData.ToJson());
        }

        //设置过滤条件
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetCondition(string SourceID, string List_Condition)
        {
            try
            {
                App.SetCondition(SourceID, List_Condition);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }

        //记录账号的过滤条件
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetDefaultRecord(string SourceID, string List_Record)
        {
            try
            {
                App.SetDefaultRecord(SourceID, List_Record);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }




        //获取列设置信息
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetCols(Pagination pagination, string SourceID)
        {

            DataTable data = App.GetCols(SourceID);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = data
            };
            return Content(JsonData.ToJson());
        }

        //获取个性化信息
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridFormat(Pagination pagination, string ColId)
        {

            DataTable data = App.GetGridFormat(ColId);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = data
            };
            return Content(JsonData.ToJson());
        }
        //保存个性化设置
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetGridFormat(string ColId, string List_GridFormat)
        {
            try
            {
                App.SetGridFormat(ColId, List_GridFormat);
                return Content("保存成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }


        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult ClearAll(string MenuID)
        {
            try
            {
                App.ClearAll(MenuID);
                return Content("清空成功");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }



        //重置列

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult ReSetCols(string MenuID)
        {
            try
            {
                App.ReSetCols(MenuID);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }


        //保存列设置

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetCols(string SourceID, string List_Cols)
        {
            try
            {
                App.SetCols(SourceID, List_Cols);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }


        [HttpGet]
        public ActionResult GetColsVisible(Pagination pagination, string MenuID, string Roles, string Cols)
        {
            try
            {
                DataTable dt = App.GetColsVisible(MenuID, Roles, Cols, ref pagination);


                var data = new
                {
                    rows = dt,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Error(ex.ToString());
            }

        }



        [HttpGet]
        public ActionResult GetRoles()
        {
            try
            {
                DataTable data = App.GetRoles();
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Error(ex.ToString());
            }

        }


        [HttpGet]
        public ActionResult GetColsFiledName(string MenuID)
        {
            try
            {
                DataTable data = App.GetColsFiledName(MenuID);
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Error(ex.ToString());
            }

        }



        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetColsVisibleALL(string Cols, string IsCommon, string MenuID)
        {
            try
            {
                App.SetColsVisibleALL(Cols, IsCommon, MenuID);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }


        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetColsVisibleAdd(string Cols, string Roles, string MenuID)
        {
            try
            {
                App.SetColsVisibleAdd(Cols, Roles, MenuID);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }


        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SetColsVisibleDel(string MenuID, string IDList)
        {
            try
            {
                App.SetColsVisibleDel(MenuID, IDList);
                return Content("操作成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }

        }



        [HttpGet]
        public ActionResult GetHiddenCols(string MenuID, string Cols)
        {
            try
            {
                DataTable data = App.GetHiddenCols(MenuID, Cols);
                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return Error(ex.ToString());
            }

        }

        [HttpGet]
        public ActionResult Filter()
        {
            return View();
        }

        [HttpGet]
        public ActionResult DataSource()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Cols()
        {
            return View();
        }


        [HttpGet]
        public ActionResult Condition()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ColsSelect()
        {
            return View();
        }
        [HttpGet]
        public ActionResult GridHeader()
        {
            return View();
        }
        [HttpGet]
        public ActionResult GridFormat()
        {
            return View();
        }
        [HttpGet]
        public ActionResult WatchPanel()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ColsVisible()
        {
            return View();
        }


    }
}