using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.EntitiesExtension;
using Hncore.Infrastructure.Extension;
using Hncore.Pass.Vpn.Domain;
using Hncore.Pass.Vpn.Service;
using Home.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Home.Controllers
{
    public class LineListController : MvcBaseController
    {

        ProductRouteService m_ProductRouteService;

        ProductService m_ProductService;
        public LineListController(ProductRouteService _ProductRouteService, ProductService _ProductService)
        {
            m_ProductRouteService = _ProductRouteService;
            m_ProductService = _ProductService;
        }

        [HttpGet]
        public async Task<IActionResult> Index([FromQuery]LineSearchModel request)
        {
            request = request ?? new LineSearchModel();
            Expression<Func<ProductRouteEntity, bool>> exp =null;

            if (request.ProductId > 0)
            {
                exp = m => m.ProductId == request.ProductId;
            }

            if (request.KeyWord.Has())
            {
                Expression<Func<ProductRouteEntity, bool>> filterExpr = m =>
                    m.Province.Contains(request.KeyWord)
                  || m.City.Contains(request.KeyWord)
                  || m.Name.Contains(request.KeyWord)
                  || m.ServerUrl.Contains(request.KeyWord);
                if (exp == null)
                    exp = filterExpr;
                else exp=exp.And(filterExpr);
            }

            var ret = await m_ProductRouteService.Query(exp).OrderBy(m=>m.Sort).ToListAsync();


            var products = await m_ProductService.Query(m=>m.OnLine == 1).OrderBy(m=>m.Sort).ToListAsync();
            ViewData["products"] = products;
            return View(ret);
        }

         public async Task<IActionResult> Index2([FromQuery]LineSearchModel request)
        {
            request = request ?? new LineSearchModel();
            Expression<Func<ProductRouteEntity, bool>> exp =null;

            if (request.ProductId > 0)
            {
                exp = m => m.ProductId == request.ProductId;
            }

            if (request.KeyWord.Has())
            {
                Expression<Func<ProductRouteEntity, bool>> filterExpr = m =>
                    m.Province.Contains(request.KeyWord)
                  || m.City.Contains(request.KeyWord)
                  || m.Name.Contains(request.KeyWord)
                  || m.ServerUrl.Contains(request.KeyWord);
                if (exp == null)
                    exp = filterExpr;
                else exp=exp.And(filterExpr);
            }

            var ret = await m_ProductRouteService.Query(exp).OrderBy(m=>m.Sort).ToListAsync();


            var products = await m_ProductService.Query(m=>m.OnLine == 1).OrderBy(m=>m.Sort).ToListAsync();
            ViewData["products"] = products;
            return View(ret);
        }

         public async Task<IActionResult> Xieyi()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> Excel([FromQuery]LineSearchModel request)
        {
            request = request ?? new LineSearchModel();
            Expression<Func<ProductRouteEntity, bool>> exp = m => 1 == 1;

            if (request.ProductId > 0)
            {
                exp = exp.And(m => m.ProductId == request.ProductId);
            }

            if (request.KeyWord.Has())
            {
                exp = exp.Or(m => m.Province.Contains(request.KeyWord));
                exp = exp.Or(m => m.City.Contains(request.KeyWord));
                exp = exp.Or(m => m.Name.Contains(request.KeyWord));
                exp = exp.Or(m => m.KeyWord.Contains(request.KeyWord));
            }

            var ret = await m_ProductRouteService.Query(exp).ToListAsync();
           
            var data = new ExcelData<ProductRouteEntity>
            {
                SheetName ="线路表",
                Data = ret
            };

            var title = new List<ExcelTitle>(){
                  new ExcelTitle { Property = "ProductName", Title = "产品" },
                  new ExcelTitle { Property = "Province", Title = "省份" },
                   new ExcelTitle { Property = "City", Title = "城市" },
                  new ExcelTitle { Property = "Name", Title = "运营商" },
                  new ExcelTitle { Property = "ServerUrl", Title = "服务器" },
                  new ExcelTitle { Property = "BandWidth", Title = "实时带宽" },
                  new ExcelTitle { Property = "IpRemark", Title = "IP量" },
                  new ExcelTitle { Property = "Status", Title = "状态" },
                };
            var fileBytes = ExcelHelper.ExportListToExcel(data, title);

            var fileName = $"线路表.xlsx";
            Response.Headers.Add("X-Suggested-Filename", fileName.UrlEncode());
            return File(fileBytes, "application/octet-stream", fileName);

        }

    }
}
