using System;
using System.Data;
using System.Data.SqlClient;

namespace TinyHeels.App_Code
{
    class OrderDetail
    {
        DBConnection strcon = new DBConnection();
        public int id { get; set; }
        public string orderCode { get; set; }
        public string productCode { get; set; }
        public string productDescription { get; set; }
        public double price { get; set; }
        public double discount { get; set; }
        public double extendedPrice { get; set; }
        public double quantity { get; set; }
        public double netAmount { get; set; }
        public string itemStatus { get; set; }
        //start of functions
        public void insertDetail()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                string sql = "INSERT INTO orderDetail(orderCode,productCode,productDescription,price,discount,extendedPrice,quantity,netAmount,itemStatus)"
                + "VALUES(@orderCode,@productCode,@productDescription,@price,@discount,@extendedPrice,@quantity,@netAmount,@itemStatus)";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddWithValue("@orderCode", this.orderCode);
                cmd.Parameters.AddWithValue("@productCode", this.productCode);
                cmd.Parameters.AddWithValue("@productDescription", this.productDescription);
                cmd.Parameters.AddWithValue("@price", this.price);
                cmd.Parameters.AddWithValue("@discount", this.discount);
                cmd.Parameters.AddWithValue("@extendedPrice", this.extendedPrice);
                cmd.Parameters.AddWithValue("@quantity", this.quantity);
                cmd.Parameters.AddWithValue("@netAmount", this.netAmount);
                cmd.Parameters.AddWithValue("@itemStatus", this.itemStatus);
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error in uploading in detail"+ex );
            }
        }
        //public void rollBackSingle()
        //{
        //    try
        //    {
        //        SqlConnection connection = strcon.connect();
        //        SqlCommand cmd = connection.CreateCommand();
        //        cmd.CommandText = "update order_detail set itemStatus='R' where detailId='" + this.id + "'";
        //        cmd.CommandType = CommandType.Text;
        //        cmd.ExecuteNonQuery();
        //        connection.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("There was a problem while Returning Item");
        //    }
        //}
        //public void rollBackAll()
        //{
        //    try
        //    {
        //        SqlConnection connection = strcon.connect();
        //        SqlCommand cmd = connection.CreateCommand();
        //        cmd.CommandText = "update order_detail set itemStatus='R' where orderCode='" + this.orderCode + "'";
        //        cmd.CommandType = CommandType.Text;
        //        cmd.ExecuteNonQuery();
        //        connection.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("There was a problem while Returning Item");
        //    }
        //}

        public DataTable getOrderByOrderId()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                using (SqlDataAdapter ds1 = new SqlDataAdapter("Select * from orderDetail where orderCode='" + this.orderCode + "'", connection))
                {
                    DataSet ds = new DataSet();
                    ds1.Fill(ds);
                    DataTable dt = new DataTable();
                    dt = ds.Tables[0];
                    connection.Close();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        public void cancelItems()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                string sql = "Delete from orderDetail Where orderCode='" + this.orderCode + "'";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not cancel order data");
            }
        }
    }
}
