using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace ass_v._1
{
    public partial class CustomerShop : Form
    {
        public CustomerShop()
        {
            InitializeComponent();
            DisplayProd();
        }
        SqlConnection Con = new SqlConnection(@"Data Source = phoenix\sqlexpress; Initial Catalog = RentAdmin; Integrated Security = True");
        private void DisplayProd()
        {
            Con.Open();
            string Query = "Select * from ProductTbl";
            SqlDataAdapter sda = new SqlDataAdapter(Query, Con);
            SqlCommandBuilder Builder = new SqlCommandBuilder(sda);
            var ds = new DataSet();
            sda.Fill(ds);
            ProductsDGV.DataSource = ds.Tables[0];
            Con.Close();
        }
        int Key = 0, Stock = 0;
        private void UpdateStock()
        {
            try
            {
                int NewQty = Stock - Convert.ToInt32(ProductQTYTb.Text);
                Con.Open();
                SqlCommand cmd = new SqlCommand("Update ProductTbl set ProductQTY=@PQTY where ProductID=@PID", Con);
                cmd.Parameters.AddWithValue("@PQTY", NewQty);
                cmd.Parameters.AddWithValue("@PID", Key);
                cmd.ExecuteNonQuery();
                Con.Close();
                DisplayProd();
                //Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        int n = 0, GrdTotal = 0;

        private void Resetbtn_Click(object sender, EventArgs e)//changed to delete
        {
            if (BillDGV.SelectedRows.Count > 0)
            {
                int rowIndex = BillDGV.SelectedRows[0].Index; // Get the index of the selected row
                BillDGV.Rows.RemoveAt(rowIndex); // Remove the row from the DataGridView
                
            }
            Reset();
        }
        private void Reset()
        {
            Key = 0;
            ProductQTYTb.Text = "";
            Stock = 0;
            ProductNameTb.Text = "";
            ProductMFeeTb.Text = "";
            DurationTb.Text = "";
        }
        int i = 0;//tochange

        private void ProductsDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            ProductNameTb.Text = ProductsDGV.SelectedRows[0].Cells[1].Value.ToString();
            Stock = Convert.ToInt32(ProductsDGV.SelectedRows[0].Cells[6].Value.ToString());
            ProductMFeeTb.Text = ProductsDGV.SelectedRows[0].Cells[8].Value.ToString();
            if (ProductNameTb.Text == "")
            {
                Key = 0;
            }
            else
            {
                Key = Convert.ToInt32(ProductsDGV.SelectedRows[0].Cells[0].Value.ToString());
            }
        }

        private void CustomerHome_externalbtn_Click(object sender, EventArgs e)
        {
            CustomerHome Obj = new CustomerHome();
            Obj.Show();
            this.Hide();
        }
        private void LastIndex()
        {
            Con.Open();
            SqlCommand cmd = new SqlCommand("SELECT TOP 1 ID FROM Transact1Tbl ORDER BY ID DESC", Con);
            SqlDataReader Rdr;
            Rdr = cmd.ExecuteReader();
            Rdr.Read();
            i=Rdr.GetInt32(0);
            Con.Close();

        }

        private void LogOut_externalbtn_Click(object sender, EventArgs e)
        {
            Login Obj = new Login();
            Obj.Show();
            this.Hide();
        }

        private void CheckOutBtn_Click(object sender, EventArgs e)
        {
            InsertTrans();
            Reset();
            MessageBox.Show("Transaction has been sent");
        }
        string CName = GlobalVariablesClass.CustomerName1;//Check

        private void BillDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ProductNameTb.Text = BillDGV.SelectedRows[0].Cells[1].Value.ToString();
            ProductQTYTb.Text= BillDGV.SelectedRows[0].Cells[2].Value.ToString();
            DurationTb.Text= BillDGV.SelectedRows[0].Cells[3].Value.ToString();
            ProductMFeeTb.Text= BillDGV.SelectedRows[0].Cells[4].Value.ToString();
        }

        private void InsertTrans()
        {
            LastIndex();
            Con.Open();
            SqlCommand cmd = new SqlCommand("insert into Transact1Tbl(ID,CustName,Total) values(@ID,@CN,@Total)", Con);
            cmd.Parameters.AddWithValue("@ID", (i+1));
            cmd.Parameters.AddWithValue("@CN", CName);
            cmd.Parameters.AddWithValue("@Total", GrdTotal);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Trans added");
            Con.Close();
            
        }

        private void Savebtn_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(ProductQTYTb.Text) > Stock)
            {
                MessageBox.Show("Insufficient Stock");
            }
            else if (ProductQTYTb.Text == ""|| DurationTb.Text=="" || Key == 0)
            {
                MessageBox.Show("Missing information");
            }
            else if (Convert.ToInt32(DurationTb.Text) < 1)
            {
                MessageBox.Show("Duration cannot be less than 1 month");
            }
            else
            {
                int total = Convert.ToInt32(ProductQTYTb.Text) *Convert.ToInt32(DurationTb.Text)* Convert.ToInt32(ProductMFeeTb.Text);
                DataGridViewRow newRow = new DataGridViewRow();
                newRow.CreateCells(BillDGV);
                newRow.Cells[0].Value = n + 1;
                newRow.Cells[1].Value = ProductNameTb.Text;
                newRow.Cells[2].Value = ProductQTYTb.Text;
                newRow.Cells[3].Value = DurationTb.Text;
                newRow.Cells[4].Value = ProductMFeeTb.Text;
                newRow.Cells[5].Value = total;
                GrdTotal += total;
                BillDGV.Rows.Add(newRow);
                n++;
                TotalLbl.Text = "£" + GrdTotal;
                UpdateStock();
                Reset();
            }
        }
    }
}