<?php

include_once("../include/main.inc.php");

check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################

@extract($_REQUEST);

if($parentID=="") {
	$parentID=0;
}

$heading="Manage Enquiry From Buy Page";

$record_type="Inquiry From Buy Page";

$script_name="manage-buyenquiry.php";

$table_name="crm_tbl";

if (isset($_POST['delete'])) {
   if (!empty($_POST['arr_id'])) {
       $catid = $_POST['arr_id'];
       foreach ($catid as $id) {
           mysqli_query($dbconn, "DELETE FROM $table_name WHERE id = '$id'");
       }
       $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
   } else {
       $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
   }
   header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
   exit;
}

if(isset($_REQUEST["inquiryId"])) {
  mysqli_query($dbconn,"UPDATE `$table_name` SET comment='$remark', `status_remark`='$status_remark' WHERE id='$inquiryId'");
  $success_msg = $record_type.' has been updated successfully';
  header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
  exit;
}

########################################################
admin_header("Manage $record_type");
$qry= db_query("SELECT crm_tbl.*, admin_details.name AS user FROM $table_name LEFT JOIN admin_details ON admin_details.slno=crm_tbl.agentId ORDER BY crm_tbl.id DESC");	
?>
<div class="content-wrapper">
  <div class="page-header">
    <h3 class="page-title mb-3"><?=$heading;?></h3>
  </div>
  <div class="card">
    <div class="card-body">
      <div class="row">
        <div class="col-12">
          <form action="" method="post">
            <div class="table-responsive">
              <table id="order-listing" class="table text-uppercase">
                <thead>
                  <tr>
                    <th>	
                      <div class="form-check mt-0">
                        <label class="form-check-label">
                          <input type="checkbox" class="form-check-input" name="check_all" id="check_all" value="1" onclick="checkall(this.form)">
                          <i class="input-helper"></i>
                        </label>
                      </div>
                    </th>
                    <th>S.No.</th>
                    <th>Date</th>
                    <th>Name</th>
                    <th>Mobile</th>
                    <th>Status</th>
                    <th>Remark</th>
                    <th>Assign to</th>
                    <th>Action</th>
                </tr>
              </thead>
              <tbody>
                <?php
                  $sl=$start+1;
                  while($arr=mysqli_fetch_array($qry)) {
                ?>
                <tr>
                  <td><input type="checkbox" name="arr_id[]" class="allSelect" value="<?= $arr['id'] ?>"></td>
                  <td><?=$sl?></td>
                  <td><p class="rec-date-dcs"><?= date('d M Y h:iA', strtotime($arr['recivedate'])) ?></p></td>
                  <td><p class="ordern"><?= $arr['name'] ?></p></td>
                  <td><p class="productid-dcs"><a href="tel:<?= $arr['mobile']?>" class="href"><?= $arr['mobile']?></a></p></td>
                  <td><p class="ordern"><?= ($arr['status_remark']=='')?'<span class="text-danger">New</span>':$arr['status_remark']; ?></p></td>
                  <td><p class="ordern"><?= $arr['comment'] ?></p></td>
                  <td><p class="ordern"><?= $arr['user'] ?></p></td>
                  <td>
                    <a class="btn btn-sm btn-info updateStatus" href="#" data-id="<?= $arr['id'] ?>" data-toggle="modal" data-target="#updatBox" data-status="<?= $arr['status_remark'] ?>" data-comment="<?= $arr['comment'] ?>">Action</a>
                  </td>
                  
                </tr>
                <?php  $sl++; }?>
                  
              </tbody>
            </table>
            <div class="btn-grp">
              <button type="submit" name="delete" onclick="return select_chk()" class="btn btn-danger btn-icon-text btn-sm">
                <i class="fas fa-trash-alt mr-1"></i>Delete
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
</div>

  <div class="modal" id="updatBox" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header p-3">
          <h5 class="modal-title">Update Inquiry Status</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
          <div class="modal-body p-3">
            <div class="form-group mb-2">
              <label>Status</label>
              <select name="status_remark" class="form-control" required>
                  <option value="">Select Status</option>
                  <option value="Converted">Converted</option>
                  <option value="Not converted">Not converted</option>
                  <option value="Hold">Hold</option>
                  <option value="Direct">Direct</option>
                  <option value="Not Responding">Not Responding</option>
                  <option value="Test">Test</option>
              </select>
            </div>
            <div class="form-group">
                <label>Remark</label>
                <textarea name="remark" class="form-control" required></textarea>
            </div>
            
          </div>
          <div class="modal-footer p-3">
            <input type="hidden" name="inquiryId" value="">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
        </form>
      </div>
    </div>
  </div>
  
  <style>
    .assignAgent {
        position: fixed;
        bottom: 7px;
        z-index: 99999;
        background: #fff;
        padding: 6px 5px;
        border: 1px solid #ccc;
        left: 13px;
        width: 250px;
        box-shadow: 0px 0px 4px 3px #060606;
        display: none;
    }

</style>
<div class="assignAgent">
    <div class="row">
        <div class="col-8">
            <select class="form-control form-control-sm p-0 pl-1" id="usrList">
                <option value="">Select agent</option>
                <?php
                    $agents = mysqli_query($dbconn, "SELECT slno, `name` FROM admin_details WHERE `status`='Y' ");
                    while($agent = mysqli_fetch_array($agents)){
                    echo '<option value="'.$agent['slno'].'">'.$agent['name'].'</option>';
                    }
                ?>
            </select>
        </div>
        <div class="col-4">
            <button type="button" class="btn btn-sm btn-success" id="ausr_btn">Assign</button>
        </div>
    </div>
</div>


  <script language="javascript">
  checked=false;
  function checkall(frm1){
    var aa= frm1;
    if(checked == false){
      checked = true
    }else{
      checked = false
    }
    for (var i =0; i < aa.elements.length; i++){
      aa.elements[i].checked = checked;
    }
  } 
function select_chk(){
  var chks = document.getElementsByName('arr_id[]');
  var hasChecked = false;
  for (var i = 0; i < chks.length; i++){
  if (chks[i].checked){
    hasChecked = true;
    break;
    }
  }
if (hasChecked == false){
alert("Please Select At Least One.");
return false;
}
}
</script>
<?php admin_footer(); ?>
<script>
  $(document).ready(function(){
    $('.updateStatus').click(function(){
      $('select[name=status_remark]').val($(this).data('status'));
      $('textarea[name=remark]').val($(this).data('comment'));
      $('input[name=inquiryId]').val($(this).data('id'));
    });


    $('.allSelect, #check_all').change(function(){
      let sn = 0;
      $('.allSelect').each(function(){
          if($(this).is(':checked')){
              sn += 1;
          }
      });
      if(sn == 0){
          $('.assignAgent').hide();
      }else{
          $('.assignAgent').show();
      }
    });
    
    $('#ausr_btn').click(function(){
      let sn = [];
      $('.allSelect').each(function(){
          if($(this).is(':checked')){
              sn.push($(this).val());
          }
      });
      if(sn.length){
          let user = $('#usrList').val();
          if(user.length){
              $.ajax({
                  url: "<?= SITE_ADMIN_URL.'/form_ajax.php'?>",
                  data: {enquiry_agent_id: user, enquiryId: sn},
                  type: 'post',
                  dataType: 'JSON',
                  success: function(data){
                      if(data.success == true){
                          window.location.reload();
                      }else{
                          console.error();
                          alert(data.message);
                      }
                  }
              });
          }else{
              alert('Please select a agent to assign order');
          }
      }else{                
          alert('Please select atleast 1 order');                
      }
    });


  
  });
</script>
    