<?php

include('../config/constant.php');
include('../includes/header.php');
include('../includes/navbar.php');
?>
<!-- Begin Page Content-->
<div class="container-fluid">


  <!-- Button trigger modal -->

  <div class="card shadow mb-4">
    <div class="card-header py-3">
      <h6 class="m-0 font weight-bold text-primary">

       
          <div class="card-body">
          
        <br/>
        <br/>
        <br/>

            <div class="table-responsive">
              <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Productname</th>
                    <th>Size</th>
                    <th>Code</th>
                    <th>Received_at</th>
                    <th>DELETE</th>
                  </tr>
                </thead>
                <tbody>
                     <?php   
                                                  require_once '../config/db.php';
                                                  require_once '../config/database.class.php';
                                                  require_once '../config/functions.php';
                                                  $d = new database($db_config);
                                             
                                                 $query = "SELECT * FROM addcart ";
                                                 $executeQuery = $d->selectQueryInArray($query);
           
                                                   if($executeQuery )  { 
                                                   foreach ($executeQuery as $row)
                                                      {
                                                ?>
<tr>
        <td><?php echo$row['id']+ id;?></td>
        <td><?php echo$row['name'];?></td>
        <td><?php echo$row['email'];?></td>
        <td><?php echo$row['phone'];?></td>
        <td><?php echo$row['productname'];?></td>
        <td><?php echo$row['size'];?></td>
        <td><?php echo$row['code'];?></td>
        <td><?php echo$row['received_at'];?></td>
        <td>
        <button type="button" class="btn btn-danger"  onclick="deleteAddcart(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> DELETE</button>
        </td> 
        </div>
        </tr>
        	<?php
                                                    } 
                                                       }
                                                 else{
                                                  echo "No record found";
 
                                                     }
                                            ?>
                  <!-- append using getAllHome() function -->
                </tbody>
              </table>   
            </div></div>
          </div></div>
          <!-- End of Main Content -->


          <?php
          include('../includes/scripts.php');
          include('../includes/footer.php');
          ?>
          
        
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" ></script>
          <script type="text/javascript">
            $(document).ready(function(){
              getAllNews();
            });

            function saveNews(){
              var options = {
                beforeSend: function () {
              //anything before sending request eg loader etc....
            },
            success: function (res) {
              // once receive response from backend
              //parese json
              var json = $.parseJSON(res);
              if(json.status=='success'){
                alert(json.message);
                $('#newsModal').modal('hide');
                $('#id').val('');
                $('#email').val('');
                getAllNews();
              }
              else{
                alert(json.message);
              }
              
            },
            error: function (response) {

            }        
          };
          $('#addNewsForm').ajaxForm(options);
        }


        

function deleteAddcart(id){
  if(confirm("Are you sure to delete")){


   $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: 'delete_add_cart.php',
    success: function (response) {
      console.log(response)
      var json = $.parseJSON(response);

      if(json.status=='success'){
        alert(json.message);
        //getAllNews();
      }
      else{
        alert(json.message);
      }

    }
  });
 }
}

function getAllNews(){
  $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../news/get_all_news.php',
    success: function (response) {

      var json = $.parseJSON(response);
      var html = "";
      $('#dataTable tbody').html(html);

      for(i=0;i<json.length;i++){
        html = html+`
        <tr>
        <td>`+(i+1)+`</td>
        <td>`+json[i].email+`</td>
        <td>`+json[i].received_at+`</td>
        <td>
        <button type="button" class="btn btn-danger" onclick="deleteNews(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> DELETE</button>
        </td> 
        </div>
        </tr>`;
      }
      $('#dataTable tbody').html(html);
    }
  });
}



</script>
