using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using EMIS.CommonLogic.Log;
using EMIS.ViewModel;
using EMIS.Web.Controls;
using Bowin.Common.Data;
using Bowin.Web.Controls.Mvc;
using Bowin.Common.Utility;

namespace EMIS.Web.Controllers
{
    [Authorization]
    public class OperateLogController : Controller
    {
        public IOperateLogServices OperateLogServices { get; set; }

        /// <summary>
        /// 操作日志页面
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 操作日志列表查询
        /// </summary>
        /// <param name="pararms"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult List(QueryParamsModel pararms)
        {
            try
            {
                ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
                var startTime = pararms.getExtraDateTime("StartTime");
                var endTime = pararms.getExtraDateTime("EndTime");

                return base.Json(OperateLogServices.GetOperateLogViewList(configuretView, startTime.Value, endTime.Value, (int)pararms.page, (int)pararms.rows));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string operateLogIDs)
        {
            try
            {
                var operateLogIDList = operateLogIDs.Split(',').Select(x => (Guid?)new Guid(x)).ToList();

                OperateLogServices.Delete(operateLogIDList);

                return base.Json("删除成功。");
            }
            catch (Exception ex)
            {
                return base.Json("删除失败,原因:" + ex.Message + "。");
            }
        }

        /// <summary>
        /// Excel导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Excel()
        {
            NpoiExcelHelper neh = new NpoiExcelHelper();
            ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
            var startTime = Request.Form["StartTime"].ParseStrTo<DateTime>();
            var endTime = Request.Form["EndTime"].ParseStrTo<DateTime>();
            if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";

            var dt = OperateLogServices.GetOperateLogViewList(configuretView, startTime.Value, endTime.Value).Select(x => new
            {
                x.LoginID,
                x.UserName,
                x.IP,
                x.TableName,
                x.SourceUrl,
                x.Operate,
                x.Detail,
                x.IsSuccessDesc,
                x.OperateTime
            }).ToTable();
            string[] liststring = { 
                                    "用户名", "姓名", "IP地址", "操作表名", "页面地址", 
                                    "操作类型", "操作详情", "是否成功", "操作时间" 
                                  };

            neh.Export(dt, liststring, "操作日志信息" + DateTime.Now.ToString("yyyyMMdd"));

            return RedirectToAction("MsgShow", "Common", new
            {
                msg = "导出成功。",
                url = Url.Content("~/OperateLog/List").AddMenuParameter()
            });
        }
    }
}