using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.Models.Main;
using LitJson;
using Library;
using System.Data;
using MySystem.Service.Main;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("/v1/[controller]/[action]")]
    public class MySelfController : BaseController
    {
        public MySelfController(IHttpContextAccessor accessor) : base(accessor)
        {
        }

        #region 我的-到账记录-到账记录列表
        // [Authorize]
        public JsonResult CardInComeRecordList(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = CardInComeRecordListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private List<Dictionary<string, object>> CardInComeRecordListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            string TradeMonth = data["TradeMonth"].ToString(); //交易月份
            string pageSize = data["PageSize"].ToString();
            string pageNum = data["PageNum"].ToString();

            var dataList = MySelfUtil.CardInComeRecordList(MerchantId, TradeMonth, pageSize, pageNum);
            return dataList;
        }
        #endregion

        #region 我的-到账记录-到账记录详情
        // [Authorize]
        public JsonResult CardInComeRecordDetail(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> obj = CardInComeRecordDetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
        }
        private Dictionary<string, object> CardInComeRecordDetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            string OrderId = data["OrderId"].ToString(); //记录Id

            var obj = MySelfUtil.CardInComeRecordDetail(MerchantId, OrderId);
            return obj;
        }
        #endregion

        #region 我的-设置-修改登录手机号(原手机验证)
        // [Authorize]
        public JsonResult OldMobileCheck(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = OldMobileCheckDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson OldMobileCheckDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MobileCode = data["MobileCode"].ToString(); //短信验证码
            string Mobile = data["Mobile"].ToString(); //手机号
            MobileCodeCheck mobilecheck = RedisDbconn.Instance.Get<MobileCodeCheck>("MobileCodeCheck:" + Mobile);
            if (mobilecheck == null)
            {
                return new AppResultJson() { Status = "-1", Info = "短信验证码不正确" };
            }
            if (mobilecheck.CheckCode != MobileCode)
            {
                return new AppResultJson() { Status = "-1", Info = "短信验证码不正确" };
            }
            RedisDbconn.Instance.Clear("MobileCodeCheck:手机号");
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

        #region 我的-设置-修改登录手机号(新手机验证)
        // [Authorize]
        public JsonResult ChangeLoginMobile(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = ChangeLoginMobileDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson ChangeLoginMobileDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string LoginMobile = data["LoginMobile"].ToString(); //原手机号
            string Mobile = data["Mobile"].ToString(); //手机号
            string MobileCode = data["MobileCode"].ToString(); //短信验证码
            MobileCodeCheck mobilecheck = RedisDbconn.Instance.Get<MobileCodeCheck>("MobileCodeCheck:" + Mobile);
            if (mobilecheck == null)
            {
                return new AppResultJson() { Status = "-1", Info = "短信验证码不正确" };
            }
            if (mobilecheck.CheckCode != MobileCode)
            {
                return new AppResultJson() { Status = "-1", Info = "短信验证码不正确" };
            }
            RedisDbconn.Instance.Clear("MobileCodeCheck:" + Mobile);
            var info = MySelfUtil.ChangeLoginMobile(LoginMobile, Mobile);
            return new AppResultJson() { Status = "1", Info = "", Data = info };
        }
        #endregion

    }
}
