using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Bailun.DC.Web.Areas.Logistics.Controllers
{
    [Area("Logistics")]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public ActionResult EbayUkReport()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="start"></param>
        /// <returns></returns>
        public JsonResult EbayUkReportJson(DateTime start)
        {
            var service = new Services.OrdersServices();

            var obj = service.ListEbayUKLogistics(start, start);
            var listconfig = service.ListEbayReportUKConfig();

            var groupby = obj.GroupBy(a => a.seller_account).ToList();

            var list = new List<Models.Ebay.mEBayUKReport>();

            foreach (var item in groupby)
            {
                var m = new Models.Ebay.mEBayUKReport();
                m.seller_account = item.Key;
                m.ordercount = item.Count();
                m.zsurfacemailcount = item.Where(a => listconfig.Where(b => b.type == 2).Select(c => c.config_value).Contains(a.logistics_order_id)).Count();
                m.zpackagecount = item.Where(a => listconfig.Where(b => b.type == 3).Select(c => c.config_value).Contains(a.logistics_order_id)).Count();
                m.zcount = m.zsurfacemailcount + m.zpackagecount;
                m.percenz = (m.zcount / m.ordercount)*100;

                m.hsurfacemailcount = item.Where(a => listconfig.Where(b => b.type == 4).Select(c => c.config_value).Contains(a.logistics_order_id)).Count();
                m.hpackagecount = item.Where(a => listconfig.Where(b => b.type == 5).Select(c => c.config_value).Contains(a.logistics_order_id)).Count();
                m.hcount = m.hsurfacemailcount + m.hpackagecount;
                m.percenh = (m.hcount / m.ordercount)*100;

                var need = ((m.ordercount * 0.5 - m.hcount) > 0 ? (m.ordercount * 0.5 - m.hcount) : 0);
                m.needaddh = (int)need;
                m.needcount = 0;

                list.Add(m);
            }

            return Json(list);
        }
    }
}