﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LJH.GeneralLibrary;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.BusinessModel.Resource;
using LJH.Inventory.BusinessModel.SearchCondition;
using LJH.Inventory.WebAPIClient;
using LJH.Inventory.UI.Forms;
using LJH.Inventory.UI.Forms.General;
using LJH.Inventory.UI.Forms.Sale;

namespace LJH.Inventory.UI.Forms.Inventory.Report
{
    public partial class FrmDeliveryRecordReport : FrmReportBase
    {
        public FrmDeliveryRecordReport()
        {
            InitializeComponent();
        }

        #region 重写基类方法
        protected override void Init()
        {
            ucDateTimeInterval1.ShowTime = false;
            ucDateTimeInterval1.Init();
            ucDateTimeInterval1.SelectThisMonth();
            base.Init();
        }

        protected override void ShowItemInGridViewRow(DataGridViewRow row, object item)
        {
            StackOutRecord sor = item as StackOutRecord;
            row.Cells["colDeliveryDate"].Value = sor.LastActiveDate.ToString("yyyy-MM-dd");
            row.Cells["colSheetNo"].Value = sor.SheetNo;
            row.Cells["colCustomerName"].Value = sor.CustomerName;
            row.Cells["colProject"].Value = sor.ProjectID;
            row.Cells["colOrderID"].Value = sor.OrderID;
            row.Cells["colName"].Value = sor.Product.Name;
            row.Cells["colModel"].Value = sor.Product.Model;
            row.Cells["colSpecification"].Value = sor.Product.Specification;
            row.Cells["colColor"].Value = sor.Product.GetProperty(ProductNote.颜色);
            if (AppSettings.Current.OperatorPara.Permit(Permission.DeliverySheet, PermissionActions.ShowPrice)) row.Cells["colPrice"].Value = sor.Price;
            row.Cells["col面积"].Value = sor.Sqare;
            row.Cells["colCount"].Value = sor.Count;
            if (AppSettings.Current.OperatorPara.Permit(Permission.DeliverySheet, PermissionActions.ShowPrice)) row.Cells["colAmount"].Value = sor.Amount;
            row.Cells["colMemo"].Value = sor.Memo;
        }

        protected override List<object> GetDataSource()
        {
            StackOutRecordSearchCondition con = new StackOutRecordSearchCondition();
            con.LastActiveDate = new DateTimeRange(ucDateTimeInterval1.StartDateTime, ucDateTimeInterval1.EndDateTime);
            con.States = new List<SheetState>() { SheetState.已发货 };
            con.SheetTypes = new List<SheetType >() { SheetType.防火门送货单, SheetType.卷帘门送货单, SheetType.折叠门送货单 ,SheetType.挡烟垂壁送货单 , SheetType.入户门送货单, SheetType.配件送货单, SheetType.物料送货单 };
            List<StackOutRecord> items = (new StackOutSheetBLL(AppSettings.Current.ConnStr)).GetRecords(con).QueryObjects;
            if (items != null && items.Count > 0)
            {
                if (!string.IsNullOrEmpty(txtSpecification.Text)) items = items.Where(it => it.Product.Specification.Contains(txtSpecification.Text)).ToList();
                if (!string.IsNullOrEmpty(txtCustomer.Text)) items = items.Where(it => it.CustomerName != null && it.CustomerName.Contains(txtCustomer.Text)).ToList();
                if (!string.IsNullOrEmpty(txtProject.Text)) items = items.Where(it => it.ProjectID.Contains(txtProject.Text)).ToList();
                if (!string.IsNullOrEmpty(txtProduct.Text)) items = items.Where(it => it.Product.Name.Contains(txtProduct.Text)).ToList();
                if (!string.IsNullOrEmpty(txtOrderID.Text)) items = items.Where(it => !string.IsNullOrEmpty(it.OrderID) && it.OrderID.Contains(txtOrderID.Text)).ToList();
                if (!chk产品.Checked) items.RemoveAll(it => it.Product.ClassID == ProductClass.商品);
                if (!chk配件.Checked) items.RemoveAll(it => it.Product.ClassID == ProductClass.物料);
                if (!chk其它.Checked) items.RemoveAll(it => it.Product.ClassID == ProductClass.其它费用);
            }
            if (items != null && items.Count > 0)
            {
                var gps = (from item in items
                           group item by new { item.SheetNo, item.Product, item.OrderItem }).ToList();
                items.Clear();
                foreach (var gp in gps)
                {
                    var item = gp.First().Clone();
                    item.Count = gp.Sum(it => it.Count);
                    items.Add(item);
                }
                return (from item in items
                        orderby item.LastActiveDate ascending, item.InventoryItem descending, item.AddDate ascending
                        select (object)item).ToList();
            }
            return base.GetDataSource();
        }

        protected override void ShowItemsOnGrid(List<object> items)
        {
            base.ShowItemsOnGrid(items);
            if (AppSettings.Current.OperatorPara.Permit(Permission.DeliverySheet, PermissionActions.ShowPrice))
            {
                decimal total = 0;
                if (items != null) total = items.Sum(it => (it as StackOutRecord).Amount);
                lbl收入合计.Text = string.Format("合计：{0:C2}", total);
            }
        }
        #endregion
    }
}
