using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SinosysHardware.Business;

namespace SinosysHardware.UI
{
    public partial class Warehouse : Form
    {
        private BindingSource Bding = new BindingSource();
        public Warehouse()
        {
            InitializeComponent();
        }

        private void FillGridData()
        {
            OrderDB odb = new OrderDB();
            DataSet Ds_Orders = odb.GetOrders(Services.OrderState.Placed);
            Bding.DataSource = Ds_Orders.Tables[0].DefaultView;
            this.dataGrid1.DataSource = Bding;
            DataSet Ds_pickedOrders = odb.GetOrders(Services.OrderState.Picked);
            this.cboPicked.DisplayMember = "OrderID";
            this.cboPicked.ValueMember = "OrderID";
            this.cboPicked.DataSource = Ds_pickedOrders.Tables[0].DefaultView;


        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPickOrder_Click(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)Bding.Current;
            int orderId=Int32.Parse(drv["OrderId"].ToString());
            Picking picking = new Picking(orderId);
            picking.ShowDialog();
            FillGridData();
            picking.Dispose();

            
        }

        private void btnLoadTruck_Click(object sender, EventArgs e)
        {
            if(cboPicked.Items.Count>0)
            {
                int _orderId = (int)cboPicked.SelectedValue;
                Loading loading = new Loading(_orderId);
                loading.ShowDialog();
                FillGridData();
                loading.Dispose();
            }
        }

        private void btnSynchronize_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                statusBar1.Text = "Synchronizing database...";
                Services s = new Services();
                s.SyncWithDataSource();
                FillGridData();
                statusBar1.Text = "";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            Cursor.Current = Cursors.Default;
            MessageBox.Show("同步完成！");
        }

        private void Warehouse_Load(object sender, EventArgs e)
        {
            FillGridData();
        }
    }
}