/*
 * 会员卡
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.Models;
using Library;
using LitJson;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class UserCardForWeChatController : BaseController
    {
        public UserCardForWeChatController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }

        #region 会员卡列表

        /// <summary>
        /// 根据条件查询会员卡列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(UserCardForWeChat data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 根据条件查询会员卡列表

        /// <summary>
        /// 会员卡列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(UserCardForWeChat data, string CodeTypeSelect, string DateTypeSelect, string MerchantIdSelect, int page = 1, int limit = 30)
        {

            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("CardType", "1"); //会员卡类型
            Fields.Add("CreateDate", "3"); //时间
            Fields.Add("BrandName", "1"); //商户名字
            Fields.Add("Title", "1"); //卡券名
            Fields.Add("Notice", "1"); //卡券使用提醒


            string condition = " and Status>-1";
            //展示类型
            if (!string.IsNullOrEmpty(CodeTypeSelect))
            {
                condition += " and CodeType=" + CodeTypeSelect;
            }
            //使用时间类型
            if (!string.IsNullOrEmpty(DateTypeSelect))
            {
                condition += " and DateType=" + DateTypeSelect;
            }
            //商户
            if (!string.IsNullOrEmpty(MerchantIdSelect))
            {
                condition += " and MerchantId=" + MerchantIdSelect;
            }


            Dictionary<string, object> obj = new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).IndexData("UserCardForWeChat", Fields, "Id desc", "0", page, limit, condition);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
                //展示类型
                string CodeType = dic["CodeType"].ToString();
                if (CodeType == "CODE_TYPE_TEXT") dic["CodeType"] = "文本";
                if (CodeType == "CODE_TYPE_BARCODE") dic["CodeType"] = "一维码";
                if (CodeType == "CODE_TYPE_QRCODE") dic["CodeType"] = "二维码";
                if (CodeType == "CODE_TYPE_ONLY_QRCODE") dic["CodeType"] = "仅显示二维码";
                if (CodeType == "CODE_TYPE_ONLY_BARCODE") dic["CodeType"] = "仅显示一维码";
                if (CodeType == "CODE_TYPE_NONE") dic["CodeType"] = "不显示任何码型";
                if (CodeType == "") dic["CodeType"] = "";
                //使用时间类型
                string DateType = dic["DateType"].ToString();
                if (DateType == "DATE_TYPE_FIX_TERM") dic["DateType"] = "支持固定时长有效类型";
                if (DateType == "DATE_TYPE_FIX_TIME_RANGE") dic["DateType"] = "固定日期有效类型";
                if (DateType == "DATE_TYPE_PERMANENT") dic["DateType"] = "永久有效类型";
                if (DateType == "") dic["DateType"] = "";
                //商户
                dic["MerchantId"] = RelationClass.GetMerchantInfoInfo(int.Parse(dic["MerchantId"].ToString()));

            }
            return Json(obj);
        }

        #endregion

        #region 增加会员卡

        /// <summary>
        /// 增加或修改会员卡信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 增加会员卡

        /// <summary>
        /// 增加或修改会员卡信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(UserCardForWeChat data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("CardType", data.CardType); //会员卡类型
            Fields.Add("LogoUrl", data.LogoUrl); //商户logo
            Fields.Add("CodeType", data.CodeType); //展示类型
            Fields.Add("BrandName", data.BrandName); //商户名字
            Fields.Add("Title", data.Title); //卡券名
            Fields.Add("Color", data.Color); //券颜色
            Fields.Add("Notice", data.Notice); //卡券使用提醒
            Fields.Add("Description", data.Description); //卡券使用说明
            Fields.Add("Quantity", data.Quantity); //卡券库存的数量
            Fields.Add("DateType", data.DateType); //使用时间类型
            Fields.Add("Prerogative", data.Prerogative); //会员卡特权说明
            Fields.Add("SupplyBonus", data.SupplyBonus); //显示积分
            Fields.Add("SupplyBalance", data.SupplyBalance); //是否支持储值
            Fields.Add("EnterName", data.EnterName); //入口名称
            Fields.Add("EnterTips", data.EnterTips); //入口右侧提示语
            Fields.Add("EnterUrl", data.EnterUrl); //入口跳转链接
            Fields.Add("MerchantId", data.MerchantId); //商户

            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).Add("UserCardForWeChat", Fields, 0);
            AddSysLog(data.Id.ToString(), "UserCardForWeChat", "add");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改会员卡

        /// <summary>
        /// 增加或修改会员卡信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            UserCardForWeChat editData = db.UserCardForWeChat.FirstOrDefault(m => m.Id == Id) ?? new UserCardForWeChat();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 修改会员卡

        /// <summary>
        /// 增加或修改会员卡信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(UserCardForWeChat data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("CardType", data.CardType); //会员卡类型
            Fields.Add("LogoUrl", data.LogoUrl); //商户logo
            Fields.Add("CodeType", data.CodeType); //展示类型
            Fields.Add("BrandName", data.BrandName); //商户名字
            Fields.Add("Title", data.Title); //卡券名
            Fields.Add("Color", data.Color); //券颜色
            Fields.Add("Notice", data.Notice); //卡券使用提醒
            Fields.Add("Description", data.Description); //卡券使用说明
            Fields.Add("Quantity", data.Quantity); //卡券库存的数量
            Fields.Add("DateType", data.DateType); //使用时间类型
            Fields.Add("Prerogative", data.Prerogative); //会员卡特权说明
            Fields.Add("SupplyBonus", data.SupplyBonus); //显示积分
            Fields.Add("SupplyBalance", data.SupplyBalance); //是否支持储值
            Fields.Add("EnterName", data.EnterName); //入口名称
            Fields.Add("EnterTips", data.EnterTips); //入口右侧提示语
            Fields.Add("EnterUrl", data.EnterUrl); //入口跳转链接
            Fields.Add("MerchantId", data.MerchantId); //商户

            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).Edit("UserCardForWeChat", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "UserCardForWeChat", "update");
            db.SaveChanges();

            UserCardForWeChat edit = db.UserCardForWeChat.FirstOrDefault(m => m.Id == data.Id) ?? new UserCardForWeChat();
            // new WeChatFunction(_accessor.HttpContext).CardCreate(edit);
        
            return "success";
        }

        #endregion

        #region 删除会员卡信息

        /// <summary>
        /// 删除会员卡信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "UserCardForWeChat", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", -1);
                new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).Edit("UserCardForWeChat", Fields, id);
            }
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "UserCardForWeChat", "open");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 1);
                new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).Edit("UserCardForWeChat", Fields, id);
            }
            db.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "UserCardForWeChat", "close");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 0);
                new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).Edit("UserCardForWeChat", Fields, id);
            }
            db.SaveChanges();
            return "success";
        }

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).Sort("UserCardForWeChat", Sort, Id);

            AddSysLog(Id.ToString(), "UserCardForWeChat", "sort");
            return "success";
        }
        #endregion

        #region 导入数据
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="ExcelData"></param>
        public string Import(string ExcelData)
        {
            ExcelData = HttpUtility.UrlDecode(ExcelData);
            JsonData list = JsonMapper.ToObject(ExcelData);
            for (int i = 1; i < list.Count; i++)
            {
                JsonData dr = list[i];

                db.UserCardForWeChat.Add(new UserCardForWeChat()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                db.SaveChanges();
            }
            AddSysLog("0", "UserCardForWeChat", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(UserCardForWeChat data, string CodeTypeSelect, string DateTypeSelect, string MerchantIdSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("CardType", "1"); //会员卡类型
            Fields.Add("CreateDate", "3"); //时间
            Fields.Add("BrandName", "1"); //商户名字
            Fields.Add("Title", "1"); //卡券名
            Fields.Add("Notice", "1"); //卡券使用提醒


            string condition = " and Status>-1";
            //展示类型
            if (!string.IsNullOrEmpty(CodeTypeSelect))
            {
                condition += " and CodeType=" + CodeTypeSelect;
            }
            //使用时间类型
            if (!string.IsNullOrEmpty(DateTypeSelect))
            {
                condition += " and DateType=" + DateTypeSelect;
            }
            //商户
            if (!string.IsNullOrEmpty(MerchantIdSelect))
            {
                condition += " and MerchantId=" + MerchantIdSelect;
            }


            Dictionary<string, object> obj = new AdminContent(_accessor.HttpContext, PublicFunction.MainTables).IndexData("UserCardForWeChat", Fields, "Id desc", "0", 1, 20000, condition, "", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
                //展示类型
                string CodeType = dic["CodeType"].ToString();
                if (CodeType == "CODE_TYPE_TEXT") dic["CodeType"] = "文本";
                if (CodeType == "CODE_TYPE_BARCODE") dic["CodeType"] = "一维码";
                if (CodeType == "CODE_TYPE_QRCODE") dic["CodeType"] = "二维码";
                if (CodeType == "CODE_TYPE_ONLY_QRCODE") dic["CodeType"] = "仅显示二维码";
                if (CodeType == "CODE_TYPE_ONLY_BARCODE") dic["CodeType"] = "仅显示一维码";
                if (CodeType == "CODE_TYPE_NONE") dic["CodeType"] = "不显示任何码型";
                if (CodeType == "") dic["CodeType"] = "";
                //使用时间类型
                string DateType = dic["DateType"].ToString();
                if (DateType == "DATE_TYPE_FIX_TERM") dic["DateType"] = "支持固定时长有效类型";
                if (DateType == "DATE_TYPE_FIX_TIME_RANGE") dic["DateType"] = "固定日期有效类型";
                if (DateType == "DATE_TYPE_PERMANENT") dic["DateType"] = "永久有效类型";
                if (DateType == "") dic["DateType"] = "";
                //商户
                dic["MerchantId"] = RelationClass.GetMerchantInfoInfo(int.Parse(dic["MerchantId"].ToString()));

            }

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("Status", "1");
            result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
            result.Add("Obj", diclist);
            Dictionary<string, object> ReturnFields = new Dictionary<string, object>();

            result.Add("Fields", ReturnFields);
            AddSysLog("0", "UserCardForWeChat", "ExportExcel");
            return Json(result);
        }

        #endregion

    }
}
