using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bowin.Common.Utility;
using EMIS.ViewModel;
using EMIS.Web.Controls;
using Bowin.Common.Data;
using EMIS.CommonLogic.ProcurementPlan;
using Bowin.Common.Linq.Entity;
using EMIS.CommonLogic.CalendarManage;

namespace EMIS.Web.Controllers.ProcurementPlan
{
    [Authorization]
    public class PurchasingStatisticsController : Controller
    {
        public IPurchasingStatisticsServices PurchasingStatisticsServices { get; set; }
        public ISchoolYearServices schoolYearServices { get; set; }

        [HttpGet]
        public ActionResult List()
        {
            //默认加载当前校历下学年学期的下一个学期
            var schoolYearView = schoolYearServices.GetSchoolYearIsCurrent(true);
            var schoolyear = schoolYearServices.GetSchoolYearViewListAfterCurrent().OrderBy(x => x.Code).Where(x => x.Value > schoolYearView.Value).FirstOrDefault();
            ViewBag.SchoolYearID = schoolyear.SchoolYearID;
            return View();
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="pararms"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult List(QueryParamsModel pararms)
        {
            ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
            //避开全选值
            Guid? publishID = null;
            Guid? coursematerialID = null;
            Guid? teachingMaterialPoolID = null;
            Guid? schoolyearID = null;
            schoolyearID = pararms.getExtraGuid("SchoolYearDropdown");
            publishID = pararms.getExtraGuid("PublishDropdown"); ;
            coursematerialID = pararms.getExtraGuid("CourseDropdown"); ;
            teachingMaterialPoolID = pararms.getExtraGuid("TeachingMaterialDropdown"); ;
            return base.Json(PurchasingStatisticsServices.GetPurchasingStatisticsViewGrid(configuretView, schoolyearID, teachingMaterialPoolID, coursematerialID, publishID, (int)pararms.page, (int)pararms.rows));
        }


        #region 2.0 页面Excel表格导出
        [HttpPost]
        public ActionResult Excel()
        {
            //避开全选值
            Guid? publishID = null;
            Guid? coursematerialID = null;
            Guid? teachingMaterialPoolID = null;
            Guid? schoolyearID = null;
            schoolyearID = Request.Form["SchoolYearDropdown"].ParseStrTo<Guid>();
            publishID = Request.Form["PublishDropdown"].ParseStrTo<Guid>();
            coursematerialID = Request.Form["CourseDropdown"].ParseStrTo<Guid>();
            teachingMaterialPoolID = Request.Form["TeachingMaterialDropdown"].ParseStrTo<Guid>();

            NpoiExcelHelper neh = new NpoiExcelHelper();
            ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
            var dt = PurchasingStatisticsServices.GetPurchasingStatisticsList(configuretView, schoolyearID, teachingMaterialPoolID, coursematerialID, publishID).Select(x => new
            {
                x.SchoolyearName,
                x.TeachingMaterialCode,
                x.CoursematerialName,
                x.ISBN,
                x.TeachingMaterialName,
                x.Price,
                x.PublishTime,
                x.PublishName,
                x.Author,
                x.CountNumber
            }).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToTable();
            string[] liststring = { "学年学期","教材编号","课程名称","ISBN","教材名称","单价"
                                  ,"版本时间","出版单位","作者","总征订量"};
            neh.Export(dt, liststring, "采购统计信息");
            return RedirectToAction("MsgShow", "Common", new
            {
                msg = "导出成功!",
                url = Url.Content("~/PurchasingStatistics/List").AddMenuParameter()
            });
        }
        #endregion
    }
}