using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Sugar.Enties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utils;
using Utils.Jwt;

namespace PMS.NetCore.Controllers
{
    /// <summary>
    /// 缴费信息接口
    /// </summary>
    [Produces("application/json;charset=UTF-8")]
    [Route("api/PayCost")]
    [ServiceFilter(typeof(Filter.TokenAuthorize))]
    public class PayCostController : BaseController
    {
        /// <summary>
        /// 获取租户信息
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        [Route("GetTenantInfo")]
        public JsonResult GetTenantInfo(string phone)
        {
            var res = new ApiResponse
            {
                Code = 200,
                IsSuccess = false,
                ErrMsg = "" 
                
            }; 
            logger.Info($"【GetTenantInfo】输入参数：{phone}");
            if(phone.IsNullOrEmpty())
            {
                res.ErrMsg = "手机号不能为空";
                return Json(res);
            }
            if(!phone.IsCellPhone())
            {
                res.ErrMsg = "手机号格式不正确";
                return Json(res);
            }
            try
            {
                var manager = new fdc_pm_ownerManager();
                var entity = manager.GetTenant(phone);
                if(entity==null)
                {
                    res.IsSuccess = true;
                    res.Data = null;
                    return Json(res);
                }
                var model = new
                {
                    entity.Name,
                    entity.Tel,
                    entity.ContractPerson,
                    entity.Houses
                };
                res.IsSuccess = true;
                res.Data = model;


                logger.Info($"【GetTenantInfo】登录接口输出：{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch(Exception ex)
            {
                logger.Error(ex);
                res.Code = 500;
                res.IsSuccess = false;
                res.ErrMsg = ex.Message;
                return Json(res);
            }
            
        }

        /// <summary>
        /// 绑定租户
        /// </summary>
        /// <param name="input">绑定相关信息</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        [Route("BindTenant")]
        public JsonResult BindTenant([FromBody] BindTenantInput input)
        {
            var res = new ApiResponse
            {
                Code = 200,
                IsSuccess = false,
                ErrMsg = ""

            };
            logger.Info($"【BindTenant】输入参数：{JsonConvert.SerializeObject(input)}");
            if (input.Phone.IsNullOrEmpty())
            {
                res.ErrMsg = "手机号不能为空";
                return Json(res);
            }
            if (!input.Phone.IsCellPhone())
            {
                res.ErrMsg = "手机号格式不正确";
                return Json(res);
            }
            if(input.WechatOpenID.IsNullOrEmpty())
            {
                res.ErrMsg = "微信ID不能为空";
                return Json(res);
            }
            if (input.Type!="0"&&input.Type!="1" && input.Type != "2")
            {
                res.ErrMsg = "绑定类型不正确";
                return Json(res);
            }
            try
            {
                var manager = new fdc_pm_ownerManager();               
                var output = manager.BindTenant(input);
                 
               
                res.IsSuccess = true;                 
                res.Data = output;


                logger.Info($"【BindTenant】登录接口输出：{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.Code = 500;
                res.IsSuccess = false;
                res.ErrMsg = ex.Message;
                return Json(res);
            }

        }

        /// <summary>
        /// 获取缴费信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        [Route("GetPaymentInfos")]
        public JsonResult GetPaymentInfos([FromBody] PaymentInput input)
        {
            var res = new ApiResponse
            {
                Code = 200,
                IsSuccess = false,
                ErrMsg = ""

            };
            logger.Info($"【BindTenant】输入参数：{JsonConvert.SerializeObject(input)}");

            if(input.WechatOpenID.IsNullOrEmpty())
            {
                res.ErrMsg = "微信ID不能为空";
                return Json(res);
            }
            if(!input.StartDate.IsNullOrEmpty())
            {
                if(!StringHelper.IsDate(input.StartDate))
                {
                    res.ErrMsg = "开始日期格式不正确";
                    return Json(res);
                }
            }
            if (!input.EndDate.IsNullOrEmpty())
            {
                if (!StringHelper.IsDate(input.StartDate))
                {
                    res.ErrMsg = "结束日期格式不正确";
                    return Json(res);
                }
            }
            try
            {
                var manager = new fdc_pm_ownerManager();
                var list = manager.GetPaymentInfo(input);
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.Code = 500;
                res.IsSuccess = false;
                res.ErrMsg = ex.Message;
                return Json(res);
            }

        }
    }
}
