<?php ob_start(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
<?php   require_once 'head_link.php'; ?> 

</head>

<body>
 
<?php   require_once 'header1.php'; ?> 

<?php
 
	if(isset($_POST['btnsave']))
	{
		
		$UserId = $_POST['UserId'];
		$CustId = $_POST['CustId'];
		$Comments = $_POST['Comments'];
		$FolCat = $_POST['FolCat'];
		$Date = $_POST['Date'];
		 
		if(empty($CustId)){
			$errMSG = "Please Enter Client Name.";
		}
		else if(empty($FolCat)){
			$errMSG = "Please Enter Follow Up Category.";
		}
		 
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO follow_up (user_id,cust_id,fol_comments,fol_cat,status,follow_date) VALUES(:UserId,:CustId,:Comments,:FolCat,1,:Date)');
			
			
			$stmt->bindParam(':UserId',$UserId);
			$stmt->bindParam(':CustId',$CustId);
			$stmt->bindParam(':Comments',$Comments);
			$stmt->bindParam(':FolCat',$FolCat);
			$stmt->bindParam(':Date',$Date);
			 
			if($stmt->execute())
			{
				?>
              <script>
				//alert('Expense Data Successfully Add ...');
				//window.location.href='followup';
				</script>
          <?php	
		  
		  }
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>

 <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (isset($_POST['btnsave'])) {

    $UserName = $_POST['UserName']; 
    $Text     = $_POST['Comments'];    
    $Number   = $_POST['CustPhone']; 

    $Message = "$Text From - $UserName , Call: 01751-891037, Visit Our Website: www.itmsofts.com";

    // Initialize curl
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://api.sms.net.bd/sendsms',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => http_build_query(array(
        'api_key' => 'LvpiximdpLedV8UDt5TCZvrY090wBCUeZg6J7j62',
        'msg'     => $Message,
        'to'      => $Number
      )),
    ));

    $response = curl_exec($curl);
    $hasError = ($response === false);

    curl_close($curl);
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>SMS Status</title>
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
      <style>
        /* Simple animations */
        .animate-icon {
          animation: pop 0.6s ease;
        }
        @keyframes pop {
          0% { transform: scale(0.5); opacity: 0; }
          100% { transform: scale(1); opacity: 1; }
        }
        .animate-text {
          animation: fadeUp 0.8s ease;
        }
        .animate-subtext {
          animation: fadeUp 1.2s ease;
        }
        @keyframes fadeUp {
          0% { transform: translateY(10px); opacity: 0; }
          100% { transform: translateY(0); opacity: 1; }
        }
      </style>
    </head>
    <body>
     
    <!-- ❌ Error Modal -->
    <div class="modal fade" id="errorModal" tabindex="-1" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg text-center p-5"
             style="background: rgba(255, 255, 255, 0.95); backdrop-filter: blur(10px); border-radius: 18px;">
          <div class="text-danger mb-3">
            <svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" fill="red"
                 class="bi bi-x-circle-fill" viewBox="0 0 16 16">
              <path
                d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM4.646 4.646a.5.5 0 0 0 0 .708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646a.5.5 0 0 0-.708 0z"/>
            </svg>
          </div>
          <h4 class="fw-bold text-danger">SMS Sending Failed</h4>
          <p class="text-muted">Please check your internet connection or API settings.</p>
          <div class="d-grid">
            <a href="followup" class="btn btn-danger rounded-pill">🔄 Try Again</a>
          </div>
        </div>
      </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <script>
      document.addEventListener('DOMContentLoaded', function () {
        const successModalEl = document.getElementById('successModal');
        const errorModalEl   = document.getElementById('errorModal');
        const progressBar    = document.getElementById('progressBar');

        <?php if ($hasError): ?>
          // Show error modal
          const errorModal = new bootstrap.Modal(errorModalEl, {
            backdrop: 'static',
            keyboard: false
          });
          errorModal.show();
        <?php else: ?>
          // Show success modal
          const successModal = new bootstrap.Modal(successModalEl, {
            backdrop: 'static',
            keyboard: false
          });
          successModal.show();

          successModalEl.addEventListener('shown.bs.modal', function () {
            progressBar.style.transition = 'width 3s ease-in-out';
            progressBar.style.width = '100%';
          });

          // Redirect after session time
          setTimeout(() => {
            window.location.href = 'lead-category';
          }, <?= $MsgDuration ?? 3000; ?>);
        <?php endif; ?>
      });
    </script>

    </body>
    </html>
    <?php
    exit;
  }
}
?>

  
 

<?php  require_once 'sidebar.php'; ?>

  <main id="main" class="main">

    <div class="pagetitle bg-light rounded p-2">
      <h1> Follow Up </h1>
    </div> 
	
    <section class="section">
      <div class="row">
        <div class="col-lg-8">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title">
			  <?php
	if(isset($errMSG)){
			?>
            <div class="alert alert-danger">
            	<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
            </div>
            <?php
	}
	else if(isset($successMSG)){
		?>
        <div class="alert alert-success">
              <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
        </div>
        <?php
	}
	?>  
			  
</h5>
			  
<form method="post" enctype="multipart/form-data" class="form-horizontal"> 
     
    
	<table class="table table-hover table-responsive">
	
   <tr>
    	
		<?php
				   $pq=mysqli_query($con,"select * from stuff left join `user` on user.userid=stuff.userid where stuff.userid='".$_SESSION['id']."' ");
				   while($pqrow=mysqli_fetch_array($pq)){
				?>
        
        <input class="form-control" type="hidden" name="UserId"  value="<?php echo $pqrow['userid']; ?>" />
        <input class="form-control" type="hidden" name="UserName"  value="<?php echo $pqrow['business_name']; ?>" />
		<?php }?>
    </tr>
	
	
	
	<tr>
    	<td><label class="control-label">Name </label>  </td>
        <td> 
		<select style="width:100%;" class="form-control chosen-select" Id="CustId" name="CustId"  >
		<option value="#">Select Client</option>
		
				      	<?php 
				      	$sql = "SELECT lead_id,lead_name FROM phone_book where  user_id='".$_SESSION['id']."' ";
								$result = $con->query($sql);

								while($row = $result->fetch_array()) {
									echo "<option value='".$row[0]."'>".$row[1]."</option>";
								} 
				      	?>
	   </select> 
     </tr>
	
	
	<tr>
    	<td><label class="control-label"> Name </label></td>
		<td><input class="form-control" type="text" id="CustName" readonly></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Business / Org </label></td>
		<td><input class="form-control" type="text" id="Org" readonly></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Address  </label></td>
		<td><input class="form-control" type="text"  id="Address" readonly></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Phone  </label></td>
		<td><input class="form-control" type="text"  id="CustPhone" name="CustPhone" ></td>
    </tr>
	
	
	<tr>
    	<td><label class="control-label"> Comments</label></td>
		<td><textarea class="form-control" name="Comments" >Dear Sir, Are you looking for trustable sales-inventory software ?  Product-Stock-Customer-Supplier-Invoice-Payment-Due-Expense, And More.We are providing best quality software.</textarea> </td>
    </tr>
	
	<tr style="display:none;">
    	<td><label class="control-label"> Category</label></td>
		<td><input class="form-control" type="text" name="FolCat" id="FolCat" placeholder="Follow By Category"></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Date </label></td>
		<td> <input class="form-control" type="date" name="Date" placeholder="" value="<?php echo date('Y-m-d'); ?>" /></td>
    </tr>

	
    </table>
	
	<tr>
        <td colspan="2"><button type="submit" name="btnsave" class="btn btn-primary">
        <span class="bi bi-check-circle"></span> Update
        </button>
        
        <a class="btn btn-danger"  href="followup"> <span class="bi bi-x-circle"></span> Cancel </a>
        
        </td>
    </tr>
    
</form> 
			  
			  
            </div>
          </div>

          
        </div>

       
    </section>

  </main> 

    <?php  require_once 'footer1.php'; ?>
	
	
<script>
	$("#CustId").on("change", function(){
		
		var customerID = $("#CustId").val();
		if(customerID == "")
		{
			alert("Please enter a valid customer ID!");
		}
		else{
			$.ajax({url: "ajax_c_load_follow.php?c=" + customerID, success: function(result){
				var customer = JSON.parse(result); 
				$("#CustName").val(customer.CName);
				$("#CustPhone").val(customer.CPhone);
				$("#FolCat").val(customer.Cat);
				$("#Address").val(customer.Addrs);
				$("#Org").val(customer.BizInfo);
				  
			}});
		}
	});
</script>

<script src = "js/chosen.js"></script>
<script type = "text/javascript">
	$('.chosen-select').chosen({width: "100%"});
</script>
