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

        private void btnAdd_Load(object sender, EventArgs e)
        {
           OrderDetailsDB oddb=new OrderDetailsDB();
           DataSet Ds = oddb.GetOrderDetails(_orderId);
           int _quantity;
          // int _orderDetailId;
           int _inventoryId;
           string _hardwareName = string.Empty;
           int _inStock = 0;
       //    int _bin = 0;
            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();
               _inStock = Int32.Parse(Ds_Inventory.Tables[0].Rows[0]["InStock"].ToString());
            //   _bin = Int32.Parse(Ds_Inventory.Tables[0].Rows[0]["Bin"].ToString());
               //Add Quantity
               ListViewItem lvi = new ListViewItem(_quantity.ToString());
               lvi.SubItems.Add(_hardwareName);
               lvi.SubItems.Add(_inStock.ToString());
            //   lvi.SubItems.Add(_bin.ToString());
               lvi.SubItems.Add(_inventoryId.ToString());
               listView1.Items.Add(lvi);
            }
        }

        private void btnAd_Click(object sender, EventArgs e)
        {
            //Retrieve a collection of items from the list view control
            ListView.ListViewItemCollection checkedItems = listView1.Items;
            //Iterate through the collection
            foreach (ListViewItem item in checkedItems)
            {
                //Determine of item is checked
                if (item.Checked)
                {
                    int _quantity = int.Parse(item.SubItems[0].Text);
                    string _hardwareName = item.SubItems[1].Text;
                    int _inStock = int.Parse(item.SubItems[2].Text);
                    int _inventoryId = int.Parse(item.SubItems[3].Text);

                    //Update In Stock Amount
                    int _newInStock = _inStock - _quantity;

                    //Add item to Staged Items List View

                    //Add Quantity
                    ListViewItem lvi = new ListViewItem(_quantity.ToString());
                    //Add Hardware Name
                    lvi.SubItems.Add(_hardwareName);
                    //Add In Updated Stock Amount
                    lvi.SubItems.Add(_newInStock.ToString());
                    //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;
                }
            }
        }

        private void btnfinish_Click(object sender, EventArgs e)
        {
            //Ensure that listview1 is empty and listview2 is greater than zero before proceeding
            if (listView1.Items.Count == 0 && listView2.Items.Count > 0)
            {
                //Update the Orders table to reflect a new Order State of Picked (2)
                OrderDB odb = new OrderDB();
                odb.UpdateOrderState(_orderId, Services.OrderState.Picked);

                //Iterate through the Staged list view to update the in-stock amounts
                ListView.ListViewItemCollection items = listView2.Items;
                //Iterate through the collection
                foreach (ListViewItem item in items)
                {
                    //Get In-Stock amount
                    int _inStock = int.Parse(item.SubItems[2].Text);
                    //Get InventoryId
                    int _inventoryId = int.Parse(item.SubItems[3].Text);
                    //Update the in-stock inventory
                    InventoryDB Idb = new InventoryDB();
                      Idb.UpdateInStockAmount(_inventoryId, _inStock);
                }

             
                Application.DoEvents();

                //Close form
                this.Close();
            }
            else
            {
                MessageBox.Show("All picked items must be staged", "Picking Error");
            }
        }
    }
}