using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using XYY.Common.Standard;
using XYY.Core.Standard.ExcelHelper.MSExcelHelper;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard.Finance;
using XYY.Service.Standard.Finance;

namespace XYY.Api.Finance.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FinanceCustomerSupplementsController : ApiControllerBase
    {
        IFinance_Customer_SupplementService supplementService;
        public FinanceCustomerSupplementsController(IFinance_Customer_SupplementService supplementService)
        {
            this.supplementService = supplementService;
        }

        public async Task<IActionResult> Add(Finance_Customer_Supplement fcs)
        {
            await this.supplementService.Add(fcs);
            return Ok();
        }

        public async Task<IActionResult> Edit(Finance_Customer_Supplement fcs)
        {
            await this.supplementService.Edit(fcs);
            return Ok();
        }

        [HttpPost]
        public async Task<IActionResult> GetPageResult(QueryModel queryModel)
        {
            var pageReuslt = await this.supplementService.GetPageResult(queryModel);
            return Ok(pageReuslt);
        }

        public async Task<IActionResult> InputFinanceCustomerSupplements(FinanceCustomerSupplementOrDeductionType type, string url, string month)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            byte[] data = client.DownloadData(url);
            MSExcelHelper excelHelper = new MSExcelHelper();
            var list = excelHelper.LoadDataByByte<CustomerSupplementMapping, Finance_Customer_Supplement>(data);
            FinanceCustomerSupplementOrDeductionType financeCustomerSupplementType = type;
            list.ForEach(x =>
            {
                x.FinanceCustomerSupplementType = type;
                if (x.FinanceCustomerSupplementType == FinanceCustomerSupplementOrDeductionType.补_退件费)
                {
                    x.Describe = "退件费";
                }
                else if (x.FinanceCustomerSupplementType == FinanceCustomerSupplementOrDeductionType.补_重派费)
                {
                    x.Describe = "重派费";
                } 
                else if (x.FinanceCustomerSupplementType == FinanceCustomerSupplementOrDeductionType.扣_重量差异费)
                {
                    x.Describe = "重量差异费";
                } 
                else if (x.FinanceCustomerSupplementType == FinanceCustomerSupplementOrDeductionType.扣_时间差异费)
                {
                    x.Describe = "时间差异费";
                }
                else if (x.FinanceCustomerSupplementType == FinanceCustomerSupplementOrDeductionType.扣_索赔)
                {
                    x.Describe = "索赔费用";
                }
                x.Month = month;
            });
            await this.supplementService.InputFinanceCustomerSupplements(type,list);
            return Ok();
        }

        public async Task<IActionResult> GetFinanceCustomerSupplementTypes()
        {
            var items = EnumsHelper.GetEnumItems(typeof(FinanceCustomerSupplementOrDeductionType));
            return Ok(items);
        }

        public class CustomerSupplementMapping : MSExcelClassMapping<Finance_Customer_Supplement>
        {
            public CustomerSupplementMapping()
            {
                Map(x => x.SystemNo).Name("跟踪号");
                Map(x => x.SystemNo).Name("运单号");
                Map(x => x.SystemNo).Name("内部单号");
                Map(x => x.FeeString).Name("费用");
                Map(x => x.FeeString).Name("金额");
                Map(x => x.FeeString).Name("退件费");
                Map(x => x.FeeString).Name("重派费用元/票（RMB）");
                Map(x => x.Describe).Name("明细");
                Map(x => x.OrderRemark).Name("重派单号");
                Map(x => x.OrderRemark).Name("退件原因");
            }
        }

        public async Task<IActionResult> Remave(Finance_Customer_Supplement fcs)
        {
            await this.supplementService.Remave(fcs);
            return Ok();
        }
    }
}