using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    //[Authority]
    public class LogController : BaseController
    {
        private BLL.T_Sys_LoginLogs loginLogsBLL = new BLL.T_Sys_LoginLogs();

        [Authority]
        /// <summary>
        /// 获取登录记录列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList()
        {
            string name = RequestString.GetQueryString("Name");
            string logindate = RequestString.GetQueryString("LoginDate");
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = "";
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += " and F_LoginName like '%" + name + "%' ";
            }
            if (!string.IsNullOrWhiteSpace(logindate))
            {
                sql += " and CONVERT(varchar , F_LoginDate, 120)>=CONVERT(varchar , '" + logindate + " 00:00:01', 120) and CONVERT(varchar , F_LoginDate, 120)<=CONVERT(varchar , '" + logindate + " 23:59:59', 120) ";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                 "T_Sys_LoginLogs",
                 "F_Id",
                 "*",
                 sql,
                 "ORDER BY F_Id desc",
                 pagesize,
                 pageindex,
                 true,
                 out recordCount);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        [Authority]
        /// <summary>
        /// 记录前端错误日志
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult ErrorLog(string log)
        {
            return Error(log);
        }

        [Authority]
        /// <summary>
        /// 删除登录记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            if (ids != null)
            {
                var str = string.Join(",", ids);
                if (loginLogsBLL.DeleteList(str))
                    return Success("删除成功");
                else
                    return Error("删除失败");
            }
            else
                return Error("删除失败");

        }
    }
}
