<?php
include("header.php");
include("left-nev.php");
$conn=db();
$page_title="Order Management";
$page_subtitle="Manage Order";
$table="tbl_order";

?>
<!--######## delete cancel ########-->
<script type="text/javascript" src="jsfiles/pagetransfer.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!--######## delete cancel ########-->
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
    <?php echo $page_title;?>
    <small><?php echo $page_subtitle;?></small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> Home</a></li>
      <li><a href="dashboard.php">Dashboard</a></li>
      <li><a href=""><?php echo $page_title;?></a></li>
      <li class="active"><?php echo $page_subtitle;?></li>
    </ol>
  </section>
  <?php
  if($_GET['action']=="Delete")
  {
  $qry=mysqli_query($conn,"select * from ".$table." where odr_id='".$_REQUEST['id']."'");
  $res=mysqli_fetch_array($qry);
  mysqli_query($conn,"delete from ".$table." where odr_id='".$_REQUEST['id']."'");
  $_SESSION['page-msg']="Deleted Successfully";
  }
  ?>
  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-xs-12">
        <div class="box">
          <div class="box-header">
          </div>
          <!-- /.box-header -->
          <div class="box-body">
            <?php
            // strrev(base64_encode($inputdata));
            //$inputdata="==wMyETJtRWY";
            //echo $k=base64_decode(strrev($inputdata));
            ?>
            <script>
            $(document).ready(function(){
            
            
            $("#page-msg").fadeOut(2000)
            
            
            });
            </script>
            <center>
            <?php
            
            if($_SESSION["page-msg"]!=""){
            ?>
            <div class="form-group has-success" id="page-msg">
              
              <label class="control-label" for="inputSuccess"><i class="fa fa-check"></i> <?php echo $_SESSION["page-msg"];?></label>
              
            </div>
            <?php
            unset($_SESSION["page-msg"]);
            }
            ?>
            </center>
            <div class="table-responsive">
              <table id="example1" class="table table-bordered table-striped ">
                <thead>
                  
                  <tr>
                    <th>S.No</th>
                    <th>Order Id</th>
                    <th>User Details</th>
                    <th>Order Date</th>
                    <th>Amount</th>
                    <th>Action</th>
                  </tr>
                </thead>
<tbody>
<?php
$selsql = mysqli_query($conn, "SELECT * FROM $table WHERE order_status='1'");
$i = 1;
while ($respage = $selsql->fetch_object()) {
    $cuser = $respage->cuser;
$qry = mysqli_query($conn, "SELECT SUM(price * quantity) AS total_price FROM tbl_temp_cart WHERE cuser='$cuser'");
$res = mysqli_fetch_assoc($qry);
$grandtotal = $res['total_price'] ?? 0;
?>
  <tr>
    <td><?php echo $i; ?></td>
    <td><?php echo htmlspecialchars($respage->cuser); ?></td>
    <td>
      <?php echo htmlspecialchars($respage->fname . ' ' . $respage->lname); ?><br>
      <b>Phone:</b> <?php echo htmlspecialchars($respage->odr_phone ?? ''); ?>
    </td>
    <td><?php echo date('d-m-Y h:i A', strtotime($respage->order_date)); ?></td>
    <td><?php echo "₹" . number_format($grandtotal ?? 0, 2); ?></td>
    <td>
      <button 
        class="btn btn-sm btn-info viewOrderBtn" 
        data-id="<?php echo $respage->odr_id; ?>">
        View
      </button>
    </td>
  </tr>
<?php
  $i++;
}
?>
</tbody>


              </table>
            </div>
          </div>
          <!-- /.box-body -->
        </div>
        <!-- /.box -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </section>
  <!-- /.content -->
</div>
<!-- Order Details Modal -->
<div class="modal fade" id="orderModal" tabindex="-1" aria-labelledby="orderModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="orderModalLabel">Order Details</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">x</button>
      </div>
      <div class="modal-body" id="orderDetails">
        <p class="text-center text-muted">Loading...</p>
      </div>
    </div>
  </div>
</div>

<!-- /.content-wrapper -->
<?php include("footer.php");?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
  $('.viewOrderBtn').on('click', function() {
    var orderId = $(this).data('id');
    // Show modal
    $('#orderModal').modal('show');
    $('#orderDetails').html('<p class="text-center text-muted">Loading...</p>');

    // Fetch order details
    $.ajax({
      url: 'get-order-details.php',
      type: 'POST',
      data: { order_id: orderId },
      success: function(response) {
        $('#orderDetails').html(response);
      },
      error: function() {
        $('#orderDetails').html('<p class="text-danger text-center">Error loading order details.</p>');
      }
    });
  });
});
</script>

<!-- ./wrapper -->
<!-- jQuery 2.2.3 -->
<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<!-- page script -->
<script>
$(function () {
$("#example1").DataTable();
$('#example2').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false
});
});
</script>
</body>
</html>