using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using XYY.Model.Standard.Finance;
using XYY.Core.Standard.Mvc;
using XYY.Service.Standard.Finance;
using static XYY.Api.Finance.Controllers.FinanceServiceDeliveyBillController;
using System;
using static XYY.Api.Finance.Controllers.FinanceController;
using XYY.Common.Standard;
using System.Collections.Generic;

namespace XYY.Api.Finance.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FiannceCustomerDeliveryBillController : ApiControllerBase
    {
        private ICustomerDeliveyBillService _customerDeliveyBillService;
        public FiannceCustomerDeliveryBillController(ICustomerDeliveyBillService customerDeliveyBillService)
        {
            _customerDeliveyBillService = customerDeliveyBillService;
        }
        public async Task<IActionResult> GetQtys()
        {
            var billQtys = await _customerDeliveyBillService.GetGroupCounts();
            if (!billQtys.Any(x => x.Key == 0))
                billQtys.Add(0, 0);
            if (!billQtys.Any(x => x.Key == 1))
                billQtys.Add(1, 0);
            if (!billQtys.Any(x => x.Key == 2))
                billQtys.Add(2, 0);
            if (!billQtys.Any(x => x.Key == 3))
                billQtys.Add(3, 0);
            return Ok(billQtys.Select(x => new { Key = x.Key, Value = x.Value }).ToList());
        }


        public async Task<IActionResult> GetWaitCreateBill(int billType)
        {
            var data = await _customerDeliveyBillService.GetWaitCreateBill(billType);
            return Ok(data);
        }

        public async Task<IActionResult> GetData(int status)
        {
            var data = await _customerDeliveyBillService.GetDataAsStatus(status);
            return Ok(data);
        }

        public async Task<IActionResult> GetConfirmeds(int billId)
        {
            var data = await _customerDeliveyBillService.GetConfirmedsData(billId);
            return CSVFile("已确认信息.csv", data);
        }

        [HttpPost]
        public async Task<IActionResult> DeleteBill(DelivertyBillApiDto bill)
        {
            await _customerDeliveyBillService.DeleteBill(bill.billId);
            return Ok();
        }


        public async Task<IActionResult> GetNotConfirmeds(int billId)
        {
            var data = await _customerDeliveyBillService.GetNotConfirmedsData(billId);
            return File("未确认信息.xlsx", data);
        }

        public async Task<IActionResult> GetOriginals(int billId)
        {
            var data = await _customerDeliveyBillService.GetOriginalsData(billId);
            return File("帐单信息.xlsx", data);
        }


        public async Task<IActionResult> GetSupplements(int billId)
        {
            var data = await _customerDeliveyBillService.GetSupplementsData(billId);
            return File("补款信息.xlsx", data);
        }


        public async Task<IActionResult> GetDeductions(int billId)
        {
            var data = await _customerDeliveyBillService.GetDeductionsData(billId);
            return File("扣款信息.xlsx", data);
        }

        public async Task<IActionResult> GetClaims(int billId)
        {
            var data = await _customerDeliveyBillService.GetClaimData(billId);
            return File("索赔信息.xlsx", data);
        }

        public async Task<IActionResult> GetOutCustomer(int billId, int type, bool isYC = false)
        {
            var data = await _customerDeliveyBillService.GetOutCustomer(billId, type, isYC);
            return File("全部信息.xlsx", data);
        }

        public async Task<IActionResult> AddDeliveryBill(Finance_CustomerDeliveryBill bill)
        {
            if (string.IsNullOrEmpty(bill.Name))
                throw new Exception("帐单名称不可为空");
            var data = await _customerDeliveyBillService.AddDeliveryBill(bill);
            return Ok(data);
        }

        /// <summary>
        /// 重新处理账单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public async Task<IActionResult> ReProcessDeliveryBill(Finance_CustomerDeliveryBill bill)
        {
            if (string.IsNullOrEmpty(bill.Name))
                throw new Exception("帐单名称不可为空");
            var data = await _customerDeliveyBillService.ReProcessDeliveryBill(bill);
            return Ok(data);
        }

        public async Task<IActionResult> GetCustomerDeliveryPanel()
        {
            var data = await _customerDeliveyBillService.GetCustomerDeliveryPanelAsync();
            return Ok(data);
        }

        public async Task<IActionResult> GetCustomerDeliveryPanelDetail()
        {
            var data = await _customerDeliveyBillService.GetCustomerDeliveryPanelDetail();
            return Ok(data);
        }

        public async Task<IActionResult> GetRemittancePageResult(QueryModel queryModel)
        {
            if (queryModel.QueryParamer == null)
                queryModel.QueryParamer = new List<QueryParamer>();
            queryModel.QueryParamer.Add(new QueryParamer
            {
                Filed = "StartTime",
                Method = "GrE",
                Value = "2021-10-01"
            });
            var data = await _customerDeliveyBillService.GetRemittancePageResult(queryModel);
            return Ok(data);
        }
        public async Task<IActionResult> DownloadRemittance(QueryModel queryModel)
        {
            if (queryModel.QueryParamer == null)
                queryModel.QueryParamer = new List<QueryParamer>();
            queryModel.QueryParamer.Add(new QueryParamer
            {
                Filed = "StartTime",
                Method = "GrE",
                Value = "2021-10-01"
            });
            var data = await _customerDeliveyBillService.DownloadRemittance(queryModel);
            return File("帐期数据"+DateTime.Now.ToString_yyyyMMddHHmmss()+".xlsx", data); 
        }
        public async Task<IActionResult> AddDeliveryBillAsFile(Finance_CustomerDeliveryBill bill)
        {
            var data = await _customerDeliveyBillService.AddDeliveryBillAsFile(bill);
            return Ok(data);
        }

        public async Task<IActionResult> InputSupplementOrDeduction(DelivertyBillApiDto bill)
        {
            if (bill.type == 3)
                await _customerDeliveyBillService.InputWeightAndCalculation(bill.billId, bill.url);
            else
                await _customerDeliveyBillService.InputSupplementOrDeduction(bill.billId, bill.url, bill.type);
            return Ok();
        }

        public async Task<IActionResult> UpdateConfirmed(DelivertyBillApiDto bill)
        {
            await _customerDeliveyBillService.UpdateConfirmed(bill.billId, bill.url);
            return Ok();
        }

        public async Task<IActionResult> Pass(DelivertyBillApiDto bill)
        {
            await _customerDeliveyBillService.Pass(bill.billId, false);
            return Ok();
        }

        public async Task<IActionResult> PostPayment(PostPayment postPayment)
        {
            await _customerDeliveyBillService.PostPayment(postPayment.Id, postPayment.PostPaymentFee);
            return Ok();
        }

        public async Task<IActionResult> GetPaymentLog(int billId)
        {
            var dtos = await _customerDeliveyBillService.GetPaymentLog(billId);
            return Ok(dtos);
        }

        public async Task<IActionResult> Rereject(DelivertyBillApiDto bill)
        {
            await _customerDeliveyBillService.Rereject(bill.billId);
            return Ok();
        }

        public async Task<IActionResult> CalcClaim(CalcClaimParam param)
        {
            await _customerDeliveyBillService.CalcBillClaimedAmount(param.BillId, param.From, param.To);
            return Ok();
        }

        #region 头程账期处理
        /// <summary>
        /// 头程账期 账单生成
        /// 补数据需要手动清理旧数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GenerateFirstBills(int firstCustomer = 0, DateTime? From = null, DateTime? To = null)
        {
            var json = await _customerDeliveyBillService.GenerateFirstBills(firstCustomer, From, To);
            if (json.success) { return Ok(); } else { return GetError("执行失败",json.data); }
            
        }
        #endregion
    }
}
