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 NewOrder : Form
    {
    
        private decimal _totalPrice = 0;
        public NewOrder()
        {
            InitializeComponent();
        }

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

        private void btnCreate_Click(object sender, EventArgs e)
        {
            //Ensure that items have been added to the order first
            if (listView1.Items.Count > 0)
            {
                //Get Customer Id
               int _customerId =(int)cboCustomer.SelectedValue;

                //Get Delivery Date
                string _deliveryDate = dateTimePicker1.Value.ToShortDateString();

                //Set OrderState to Placed
                int _orderState = 1;

                //Get collection of items in ListView
                ListView.ListViewItemCollection _orderItems = listView1.Items;

                //Insert into the Orders and OrderDetails tables
                OrderDB odb = new OrderDB();
                odb.CreateOrder(_customerId, _deliveryDate, _orderState, _orderItems);
                MessageBox.Show("A new order has been placed.\nPlease synchronize your device to make this order available to everyone else.");
                this.Close();
            }
            else
            {
                MessageBox.Show("You must add hardware items to your order", "Empty Order");
            }
        }

        private void btnRemove_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 _qty = int.Parse(item.SubItems[0].Text);
                    int _inventoryId = int.Parse(item.SubItems[2].Text);

                    //Get price based on InventoryId
                    InventoryDB Idb = new InventoryDB();
                    decimal _price =Idb.GetPrice(_inventoryId);

                    //Get Subtotal based on Qty
                    decimal _subTotal = _qty * _price;

                    //Subtract price from Total Price
                    _totalPrice -= _subTotal;

                    //Update column header
                    listView1.Columns[1].Text = "Hardware   $ " + _totalPrice.ToString();

                    //Remove item from list
                    listView1.Items.Remove(item);
                    break;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
              //Get Quantity
               int _quantity = (int)numericUpDownQty.Value;

               //Get selected Inventory object
                int inventoryId =(int)cboInventoryItems.SelectedValue;
                InventoryDB Idb = new InventoryDB();
                DataSet Ds=Idb.GetInventory(inventoryId);
                Idb.InventoryId = inventoryId;
                Idb.InventoryName = Ds.Tables[0].Rows[0]["Name"].ToString();
                Idb.Picture = (byte[])Ds.Tables[0].Rows[0]["Picture"];
                Idb.Price = Convert.ToDecimal(Ds.Tables[0].Rows[0]["Price"]);
                Idb.InStock = Convert.ToInt32(Ds.Tables[0].Rows[0]["InStock"]);
             //   Idb.DistributionCenter = Convert.ToInt32(Ds.Tables[0].Rows[0]["DistributionCenter"]);
              //  Idb.Bin = Convert.ToInt32(Ds.Tables[0].Rows[0]["Bin"]);

                decimal _price = _quantity * Idb.Price;

            //Get total price
            _totalPrice += _price;


            //Verify there's enough hardware left in stock
            if (Idb.InStock >= _quantity)
            {
                //Add Quantity
                ListViewItem lvi = new ListViewItem(_quantity.ToString());
                //Add Inventory Name
                lvi.SubItems.Add(Idb.InventoryName);
                //Add Inventory ID
                lvi.SubItems.Add(Idb.InventoryId.ToString());
                //Add ListView Items to ListView
                listView1.Items.Add(lvi);
                //Update column header
                listView1.Columns[1].Text = "Hardware   $ " + _totalPrice.ToString();
              
            }
            else
            {
                MessageBox.Show("The quantity you requested exceeds our quantity in stock", "Quantity exceeds stock");
            }
        }

        private void cboInventoryItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Get selected Inventory object
            int inventoryId = (int)cboInventoryItems.SelectedValue;

            InventoryDB Idb = new InventoryDB();
            DataSet Ds = Idb.GetInventory(inventoryId);
            //  Display price
            lblPrice.Text = "$ " + Convert.ToDecimal(Ds.Tables[0].Rows[0]["Price"]).ToString();
            Idb.Picture = (byte[])Ds.Tables[0].Rows[0]["Picture"];
            //  Display hardware picture in PictureBox
            pictureBox1.Image = new Bitmap(Idb.PictureAsStream());
        }

        private void NewOrder_Load(object sender, EventArgs e)
        {
            //Set default delivery date to tomorrow
            dateTimePicker1.MinDate = System.DateTime.Today.AddDays(1);
            CustomerDB cdb = new CustomerDB();
            DataSet DsCustomers = cdb.GetCustomers();
            InventoryDB Idb = new InventoryDB();
            DataSet DsInventory = Idb.GetInventory();
            //Display customer information
            cboCustomer.DisplayMember = "CustomerName";
            cboCustomer.ValueMember = "CustomerId";
            cboCustomer.DataSource = DsCustomers.Tables[0].DefaultView;
           
          

            //Display inventory items
            cboInventoryItems.DisplayMember = "Name";
            cboInventoryItems.ValueMember = "InventoryId";
            cboInventoryItems.DataSource = DsInventory.Tables[0].DefaultView;
         
          
        }
    }
}