using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BO.Utils;
using BO.Utils.BillRpc;
using BWP.WinFormControl;
using Distribution.LocalBo;

namespace Distribution
{
  public partial class DistributionForm : Form,IAfterLogin
  {

    public string RoleName { get { return "配货员"; } }
    public Form Generate()
    {
      return this;
    }


    private bool mIsCustomerSynced, mIsDeliverGoodsLineSynced;
    private bool mIsFirstSearch = true;
    private BackgroundWorker mBackgroundWorkerSyncBaseInfo;

    private BindingList<SaleOutStore> mSaleOutStoreList;
    private BindingList<SaleOutStore> mSaleOutStoreAllList;
    private BindingList<SaleOutStore> mSaleOutStoreDingHuoList;

    public DistributionForm()
    {
      InitializeComponent();
      dataGridViewSaleOutStore.AutoGenerateColumns = false;
      dataGridViewDingHuo.AutoGenerateColumns = false;
      dataGridViewFaHuo.AutoGenerateColumns = false;

      cbxSelectCustomer.Init(x => CustomerRpc.SyncListForDropDown(x,!mIsCustomerSynced));

      cbxSelectXianLu.Init(x => DeliverGoodsLineRpc.SyncListForDropDown(x,!mIsDeliverGoodsLineSynced));

      dateInput.Date=DateTime.Today;
      dateInput.Text = DateTime.Today.ToString("yyyy/MM/dd");
      dateInput.AfterDateChange += delegate
      {
        //每次日期变了之后要设置     mIsFirstSearch = true;
        mIsFirstSearch = true;
      };
      mBackgroundWorkerSyncBaseInfo =new BackgroundWorker();
      mBackgroundWorkerSyncBaseInfo.DoWork += backgroundWorkerSyncBaseInfo_DoWork;
      mBackgroundWorkerSyncBaseInfo.RunWorkerCompleted += backgroundWorkerSyncBaseInfo_RunWorkerCompleted;
      mBackgroundWorkerSyncBaseInfo.RunWorkerAsync();


    }

    private void backgroundWorkerSyncBaseInfo_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      mIsCustomerSynced = true;
      mIsDeliverGoodsLineSynced = true;
      }

    private void backgroundWorkerSyncBaseInfo_DoWork(object sender, DoWorkEventArgs e)
    {
      CustomerRpc.SyncList();
      DeliverGoodsLineRpc.SyncList();
   }

    private void DistributionForm_Load(object sender, EventArgs e)
    {

    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
      btnSearch.Enabled = false;
      Application.DoEvents();
      mSaleOutStoreAllList = SaleOutStoreRpc.SyncList(dateInput.Date.Value, cbxSelectCustomer.Text, cbxSelectXianLu.Text, "", mIsFirstSearch);
      SetSaleOutStoreList();
      dataGridViewSaleOutStore.DataSource = mSaleOutStoreList;
      mIsFirstSearch = false;
      btnSearch.Enabled = true;
    }

    private void dataGridViewSaleOutStore_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      if (e.RowIndex < 0 || e.ColumnIndex < 0)
      {
        return;
      }

      var currentRow = dataGridViewSaleOutStore.CurrentRow.DataBoundItem as SaleOutStore;
      //按线路配货
      if (rbtnXianLu.Checked)
      {
        mSaleOutStoreDingHuoList=new BindingList<SaleOutStore>();
        foreach (SaleOutStore outStore in mSaleOutStoreAllList.Where(x=>x.SaleOutStore_ID==currentRow.SaleOutStore_ID))
        {
          //已发货数量直接查本地数据库
          mSaleOutStoreDingHuoList.Add(outStore);
        }
        dataGridViewDingHuo.DataSource = mSaleOutStoreDingHuoList;
        dataGridViewDingHuo.CurrentCell = null;
      }
      else if (rbtnDanPin.Checked)
      {//按单品配货
        
      }
      //rbtnDanPin
    }

    void SetSaleOutStoreList()
    {
      mSaleOutStoreList=new BindingList<SaleOutStore>();
      foreach (var grouping in mSaleOutStoreAllList.GroupBy(x=>x.SaleOutStore_ID))
      {
        var fd = grouping.FirstOrDefault();
        if (fd != null)
        {
          mSaleOutStoreList.Add(fd);
        }
      }
    }
  }
}
