using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Customer;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Customer;
using Edu.Module.Customer;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Edu.WebApi.Controllers.Customer
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class B2BRemitController : BaseController
    {
        /// <summary>
        /// 提现
        /// </summary>
        private readonly CustomerRemitModule customerRemitModule = new CustomerRemitModule();


        #region 提现管理

        /// <summary>
        /// 获取同业提现申请分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCustomerRemitPageList() {
            var userInfo = base.UserInfo;
            ResultPageModel pmodel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            RB_Customer_Remit_Extend demodel = JsonHelper.DeserializeObject<RB_Customer_Remit_Extend>(base.RequestParm.Msg.ToString());
            demodel.GroupId = userInfo.Group_Id;

            var list = customerRemitModule.GetCustomerRemitPageList(pmodel.PageIndex, pmodel.PageSize, out long count, demodel);
            pmodel.Count = Convert.ToInt32(count);
            pmodel.PageData = list.Select(x => new
            {
                x.Id,
                x.CustomerId,
                x.CustomerName,
                x.CustomerImage,
                x.ContactNumber,
                x.Type,
                x.AppliedMoney,
                x.RemitMoney,
                x.AuditStatus,
                AuditDate = x.AuditDate.HasValue ? x.AuditDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
                RemitDate = x.RemitDate.HasValue ? x.RemitDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
                RejectDate = x.RejectDate.HasValue ? x.RejectDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
                x.AuditRemark,
                x.RemitRemark,
                x.RejectRemark,
                CreateDate = x.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                x.Year,
                x.BankAccountId,
                x.BankName,
                x.BankIcon,
                x.BankCardNo,
                x.AccountHolder
            });
            return ApiResult.Success("", pmodel);
        }

        /// <summary>
        /// 设置同业提现申请状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCustomerRemitState() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int RemitId = parms.GetInt("RemitId", 0);
            int State = parms.GetInt("State", 1);//状态  1审核通过   2审核驳回
            string Remark = parms.GetStringValue("Remark");//备注
            if (RemitId <= 0) {
                return ApiResult.ParamIsNull("请传递提现ID");
            }

            string msg = customerRemitModule.SetCustomerRemitState(RemitId, State, Remark, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        #endregion

        #region 幻灯片

        /// <summary>
        /// 获取幻灯片配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetB2BSlideShowInfo() {
            var userInfo = base.UserInfo;

            var Robj = customerRemitModule.GetB2BSlideShowInfo(userInfo);
            return ApiResult.Success("", Robj);
        }

        /// <summary>
        /// 保存幻灯片配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetB2BSlideShowInfo() {
            var userInfo = base.UserInfo;
            RB_B2B_SlideShow_Extend demodel = JsonHelper.DeserializeObject<RB_B2B_SlideShow_Extend>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Title)) {
                return ApiResult.ParamIsNull("请输入标题");
            }
            if (string.IsNullOrEmpty(demodel.SubTitle)) {
                return ApiResult.ParamIsNull("请输入副标题");
            }
            if (demodel.SlideShowList == null || !demodel.SlideShowList.Any()) {
                return ApiResult.ParamIsNull("请选择轮播图片");
            }
            foreach (var item in demodel.SlideShowList) {
                if (item.Sort <= 0) {
                    return ApiResult.ParamIsNull("请传递排序");
                }
                if (string.IsNullOrEmpty(item.Image)) {
                    return ApiResult.ParamIsNull("请传递图片路劲");
                }
                if (string.IsNullOrEmpty(item.Path)) {
                    return ApiResult.ParamIsNull("请传递跳转地址");
                }
            }
            demodel.Content = JsonHelper.Serialize(demodel.SlideShowList);
            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = customerRemitModule.SetB2BSlideShowInfo(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed(msg);
            }
        }

        #endregion
    }
}
