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 Loading : Form
    {
        private int orderId;
        public Loading(int _orderId)
        {
            InitializeComponent();
            this.orderId = _orderId;
        }

        private void Loading_Load(object sender, EventArgs e)
        {
            TruckDB Tdb = new TruckDB();
            DataSet Ds_Truck = Tdb.GetTruckToLoad(orderId);
            this.txtTruckInfo.Text = "Truck Number: " + Ds_Truck.Tables[0].Rows[0]["LicensePlateStateProvince"].ToString() + Ds_Truck.Tables[0].Rows[0]["LicensePlateNumber"].ToString();
            OrderDetailsDB odb = new OrderDetailsDB();
            DataSet Ds = odb.GetOrderDetails(orderId);
            int _quantity;
            int _inventoryId;
            string _hardwareName = string.Empty;
            foreach(DataRow row in Ds.Tables[0].Rows)
            {
                _quantity =Int32.Parse(row["Quantity"].ToString());
                _inventoryId=Int32.Parse(row["InventoryId"].ToString());
                InventoryDB Idb = new InventoryDB();
                DataSet Ds_Inventory = Idb.GetInventory(_inventoryId);
                _hardwareName = Ds_Inventory.Tables[0].Rows[0]["Name"].ToString();
                //Add Quantity
                ListViewItem lvi = new ListViewItem(_quantity.ToString());
                //Add Hardware Name
                lvi.SubItems.Add(_hardwareName);
                //Add Inventory Id
                lvi.SubItems.Add(_inventoryId.ToString());
                //Add ListView Items to ListView
                listView1.Items.Add(lvi);

            }

            
        }

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

        private void btnFinish_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count == 0 && listView2.Items.Count > 0)
            {
                //Update the Orders table to reflect a new Order State of Loaded (3)
                OrderDB odb = new OrderDB();
                odb.UpdateOrderState(orderId, Services.OrderState.Loaded);
             
             
                Application.DoEvents();

                //Close form
                this.Close();
            }
            else
            {
                MessageBox.Show("All staged items must be loaded on the truck", "Loading Error");
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            ListView.ListViewItemCollection checkItems = listView1.Items;
            foreach(ListViewItem item in checkItems)
            {
              if(item.Checked)
              {
                  int _quantity = Int32.Parse(item.SubItems[0].Text);
                  string _hardwareName = item.SubItems[1].Text;
                  int _inventoryId = Int32.Parse(item.SubItems[2].Text);
                  //Add Quantity
                  ListViewItem lvi = new ListViewItem(_quantity.ToString());
                  //Add Hardware Name
                  lvi.SubItems.Add(_hardwareName);
                  //Add Inventory Id
                  lvi.SubItems.Add(_inventoryId.ToString());
                  //Add ListView Items to ListView
                  listView2.Items.Add(lvi);
                  //Remove item from list
                  listView1.Items.Remove(item);
                  break;
              }

            }
        }
    }
}