using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mall.Common.API;
using Mall.Model.Extend.User;
using Mall.Module.User;
using Mall.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Mall.WebApi.Controllers.User
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class UserVipController : BaseController
    {
        private readonly IHttpContextAccessor _accessor;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accessor"></param>
        public UserVipController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        private readonly UserVipModule userVipModule = new UserVipModule();


        #region VIP购买自定义页面设置

        /// <summary>
        /// 获取购买vip自定义页面设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetFXGradeCustom()
        {
            var parms = RequestParm;
            RB_Distributor_FXGradeCustom_Extend demodel = JsonConvert.DeserializeObject<RB_Distributor_FXGradeCustom_Extend>(parms.msg.ToString());

            demodel.TenantId = Convert.ToInt32(parms.uid);
            demodel.MallBaseId = parms.MallBaseId;
            var model = userVipModule.GetGradeCustomList(demodel).FirstOrDefault();
            if (model == null)
            {
                model = new RB_Distributor_FXGradeCustom_Extend();
            }
            return ApiResult.Success("", model);
        }

        /// <summary>
        /// 新增修改购买vip自定义页面设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetFXGradeCustom()
        {
            var req = RequestParm;
            RB_Distributor_FXGradeCustom_Extend demodel = JsonConvert.DeserializeObject<RB_Distributor_FXGradeCustom_Extend>(req.msg.ToString());
            if (string.IsNullOrWhiteSpace(demodel.BottomRule))
            {
                return ApiResult.Failed("活动规则不能为空");
            }
            if (string.IsNullOrWhiteSpace(demodel.BackImage))
            {
                return ApiResult.Failed("背景图片不能为空");
            }
            if (string.IsNullOrWhiteSpace(demodel.VipICO))
            {
                return ApiResult.Failed("VIP图标不能为空");
            }
            if (string.IsNullOrWhiteSpace(demodel.BackColor))
            {
                return ApiResult.Failed("背景颜色不能为空");
            }
            if (demodel.Id == 0)
            {
                demodel.TenantId = Convert.ToInt32(req.uid);
                demodel.MallBaseId = req.MallBaseId;
                demodel.CreateDate = System.DateTime.Now;
            }
            demodel.Status = 0;
            demodel.UpdateDate = System.DateTime.Now;

            bool flag = userVipModule.AddOrUpdateFXGradeCustom(demodel);
            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }
        #endregion



        #region Vip购买记录
        /// <summary>
        /// 获取vip购买记录
        /// </summary>
        /// <param name="requestMsg"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetVipBuyPageList()
        {
            var parms = RequestParm;
            ResultPageModel pagelist = JsonConvert.DeserializeObject<ResultPageModel>(parms.msg.ToString());
            RB_Vip_Buy_Extend demodel = JsonConvert.DeserializeObject<RB_Vip_Buy_Extend>(parms.msg.ToString());
            demodel.TenantId = parms.TenantId;
            demodel.MallBaseId = parms.MallBaseId;
            var list = userVipModule.GetVipBuyPageList(pagelist.pageIndex, pagelist.pageSize, out long count, demodel);

            pagelist.count = Convert.ToInt32(count);
            pagelist.pageData = list.Select(x => new
            {
                x.Id,
                x.UserName,
                x.Money,
                ExpiryDateStr = x.ExpiryDate.HasValue ? x.ExpiryDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
                x.GradeName,
                x.PayState,
                x.PayWay,
                PayTimeStr= x.PayTime.HasValue ? x.PayTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""
            });
            return ApiResult.Success("", pagelist);
        }
        #endregion
    }
}