<?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">
      <h5 class="m-0 font weight-bold text-primary">Contact Us</h5>
      </div>

        <div class="modal fade" id="contactModal" tabindex="1" role="dialog" aria-labelledby="examplemodalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Contact</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <form action="../contact/add_contact.php" id="addContactForm" method="POST" enctype="multipart/form-data">
                  <div class="modal-body">
                    <div class="form-group">

                      <input type="hidden" name="id" id="id" class="form-control" placeholder="Enter Id">
                    </div>
                    <div class="form-group">
                      <label>phone</label>
                      <input type="phone" name="phone" id="phone" class="form-control" placeholder="Enter Phone">
                    </div>
                    <div class="form-group">
                      <label>whatsapp</label>
                      <input type="phone" name="whatsapp" id="whatsapp" class="form-control" placeholder="Enter Whatsapp No.">
                    </div>
                    
                    <div class="form-group">
                      <label>Address</label>
                      <input type="text" name="address" id="address" class="form-control" placeholder="Enter  Address">
                    </div>
                    
                    
                    <div class="form-group">
                      <label>Email1</label>
                      <input type="email" name="email1" id="email1" class="form-control" placeholder="Enter Email1">
                    </div>
                     <div class="form-group">
                      <label>Email2</label>
                      <input type="email" name="email2" id="email2" class="form-control" placeholder="Enter Email2">
                    </div>
                    
                    
                    
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" name="registerbtn" class="registerbtn btn-primary btn" onclick="saveContact();">Save</button>
                  </div>
                </div></form>
              </div>
            </div>
          </div>
          <div class="card-body">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contactModal" style="float: right">
          Add new in contact
        </button>
        <br/>
        <br/>
        <br/>

            <div class="table-responsive">
              <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>Phone</th>
                    <th>Address</th>
                    <th>Email</th>
                    <th>Created_at</th>
                    <th>Updated_at</th>
                    <th>EDIT</th>
                    <th>DELETE</th>
                  </tr>
                </thead>
                <tbody>
                  <!-- append using getAllHome() function -->
                </tbody>
              </table>   
            </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(){
              getAllContact();
            });

            function saveContact(){
              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);
                $('#contactModal').modal('hide');
                $('#id').val('');
               
                $('#phone').val('');
                $('#whatsapp').val('');
                $('#address').val('');
                $('#email1').val('');
                $('#email2').val('');
               
                getAllContact();
              }
              else{
                alert(json.message);
              }
              
            },
            error: function (response) {

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


        function editContact(id){
// alert(id);
$('#contactModal').modal('show');
$('#id').val(id);

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

    $('#id').val(json[0].id);
     $('#phone').val(json[0].phone);
     $('#whatsapp').val(json[0].whatsapp);
     $('#address').val(json[0].address);
     $('#email1').val(json[0].email1);
     $('#email2').val(json[0].email2);
   
  }
});
}

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


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

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

    }
  });
 }
}

function getAllContact(){
  $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../contact/get_all_contact.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].phone+`</td>
        <td>`+json[i].address+`</td>
        <td>`+json[i].email1+`</td>
        <td>`+json[i].created_at+`</td>
        <td>`+json[i].updated_at+`</td>
        <td> 
        <button type="button" class="btn btn-success" onclick="editContact(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> Edit</button>
        </td>
        <td>
        <button type="button" class="btn btn-danger" onclick="deleteContact(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> DELETE</button>
        </td> 
        </div>
        </tr>`;
      }
      $('#dataTable tbody').html(html);
    }
  });
}



</script>
