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

namespace XYY.Api.Finance.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FinanceServiceDeliveyBillController : ApiControllerBase
    {
        private IServiceDeliveyBillService _serviceDeliveyBillService;
        public FinanceServiceDeliveyBillController(IServiceDeliveyBillService serviceDeliveyBillService)
        {
            _serviceDeliveyBillService = serviceDeliveyBillService;
        }
        public async Task<IActionResult> GetQtys(string billName)
        {

            var billQtys = await _serviceDeliveyBillService.GetGroupCounts(billName);
            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> GetData(int status, string billName)
        {
            var data = await _serviceDeliveyBillService.GetDataAsStatus(status, billName);
            return Ok(data);
        }

        public class FreightDto
        {
            public int channelId
            {
                get; set;
            }

            public DateTime start
            {
                get; set;
            }

            public DateTime end
            {
                get; set;
            }
        }

        public async Task<IActionResult> FreightRecalculation(FreightDto dto)
        {
            await _serviceDeliveyBillService.FreightRecalculation(dto.channelId, dto.start, dto.end);
            return Ok();
        }

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


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

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


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


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

        public async Task<IActionResult> AddDeliveryBill(Finance_ServiceDeliveryBill bill)
        {
            var data = await _serviceDeliveyBillService.AddDeliveryBill(bill);
            return Ok(data);
        }

        public async Task<IActionResult> InputSupplementOrDeduction(DelivertyBillApiDto bill)
        {
            await _serviceDeliveyBillService.InputSupplementOrDeduction(bill.billId, bill.url, bill.type);
            return Ok();
        }
        public async Task<IActionResult> ChangeBillName(Finance_ServiceDeliveryBill bill)
        {
            await _serviceDeliveyBillService.ChangeBillName(bill);
            return Ok();
        }

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

        public IActionResult GetDeliveryBillTypes()
        {
            var model = EnumsHelper.GetEnumItems(typeof(DeliveryBillType));
            return Ok(model);
        }


        public IActionResult GetAddDeliverBillTempler()
        {
            var data = _serviceDeliveyBillService.GetAddDeliverBillTempler();
            return File("模板.xlsx", data);
        }

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

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

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

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

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


        public class DelivertyBillApiDto
        {
            public string url { get; set; }
            public int billId { get; set; }
            public int type { get; set; }
        }
    }
}
