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 Deliveries : Form
    {
        private BindingSource Bding = new BindingSource(); 
        public Deliveries()
        {
            InitializeComponent();
        }
        private void FillDataGrid()
        {
            OrderDB odb = new OrderDB();
            DataSet Ds = odb.GetOrders(Services.OrderState.Loaded);
            Bding.DataSource = Ds.Tables[0].DefaultView;
            dataGrid1.DataSource = Bding;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGetDirections_Click(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)Bding.Current;
            int customerId = Int32.Parse(drv["CustomerId"].ToString());
            CustomerDB cdb=new CustomerDB();
            DataSet Ds = cdb.GetCustomers(customerId);
            cdb.StreetAddress =Ds.Tables[0].Rows[0]["StreetAddress"].ToString().Trim();
            cdb.City =Ds.Tables[0].Rows[0]["City"].ToString().Trim();
            cdb.StateProvince =Ds.Tables[0].Rows[0]["StateProvince"].ToString().Trim();
            Services s = new Services();
            this.pictureBox1.Image = s.CallMapPoint(pictureBox1.Height, pictureBox1.Width, cdb.StreetAddress, cdb.City, cdb.StateProvince);
        }

        private void btnDeliver_Click(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)this.Bding.Current;
            int _orderId = Int32.Parse(drv["OrderId"].ToString());
            OrderDB odb = new OrderDB();
            odb.UpdateOrderState(_orderId, Services.OrderState.OutForDelivery);
            FillDataGrid();
        }

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

        private void Deliveries_Load(object sender, EventArgs e)
        {
            FillDataGrid();
        }
    }
}