<?php
if($_POST["iname"] && $_POST["icat"] && $_POST["ipprice"] && $_POST["isprice"] && $_POST["wsprice"] && $_POST["itemID"] && $_POST["expd"])
{ 
    $wsprice= $_POST['wsprice'];
    $isprice= $_POST['isprice'];
    $ipprice= $_POST['ipprice'];
    $icat= $_POST['icat'];
	$iname= $_POST['iname'];
	$inam0= $_POST['inam0'];
    $itemID= $_POST['itemID'];
    $expd= $_POST['expd']; 
  //CHECKING ENDS HER
  require_once"labClass.php";
  $con=new labClass();
  $Q= $con->getThisItemFED($itemID,$iname);
  if($Q->rowCount()<=0)
    {
   $rQ= $con->updateItemD($iname,$icat,$ipprice,$isprice,$wsprice,$itemID,$expd);

   
   if($rQ->rowCount()>0)
      {
         $k= $con->getThisStoreItemUpD($inam0);
         if($k->rowCount()>0)
      {
          $r= $con->updateStoreItemD($inam0,$icat,$ipprice,$isprice,$wsprice,$iname,$expd);
          
         ///------SEND MESSAGE=----
         
          if($r->rowCount()>0)
      {
         
         $params = [];
           //////////////////////////
            
              $phone ="255759441039";//;
              //$amountFormatted = number_format($cash, 2);
              $message = "Habari za kazi!.Bei ya  {$iname} kwa sasa ni TZS {$isprice} rejareja  na {$wsprice} jumla.Asante";
              $params[] = [
                          "Number" => $phone,
                          "Text" => $message
                          ];
                     
           
           smsAPI($params);
         
         
         
         
         
         ///----END SMS
          
          
          
          
      ?>
      <h6 class="text-success">Save!..</h6>
      <?php
      
       }
  else{
       ?>
        <h6 class="text-danger">Store not updated!..!..</h6>
       <?php
       }
      }
  else{
      $iQty=0;
      $rQr= $con->itemRegStore($iname,$icat,$ipprice,$wsprice,$isprice,$iQty,$expd);
   
       ?>
        <h6 class="text-danger">Item registered to store!..!..</h6>
       <?php
       }
       }
       }
  else{
       ?>
        <h6 class="text-danger">Same name exist!..</h6>
       <?php
       }
       }
  else{
       ?>
       <h6 class="text-danger">All field required!..</h6>
       <?php
       }
       
function smsAPI($params){

date_default_timezone_set('Africa/Dar_es_Salaam'); 
$api_url     = "https://api.onfonmedia.co.ke/v1/sms/SendBulkSMS";
$api_key     = "czu3YnOb5j4tfR08LsEmUIhvkP1BZS9qdoXlCgN6rwax7G2e"; 
$client_id   = "rombostar";  
$sender_id   = "ROMBO STAR"; 

if (!empty($params)) {
    $payload = json_encode([
        "SenderId" => $sender_id,
        "MessageParameters" => $params,
        "ApiKey" => $api_key,
        "ClientId" => $client_id
    ]);

    $ch = curl_init($api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); 
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch); 
    file_put_contents("sms_response_log.txt", "[" . date("Y-m-d H:i:s") . "] HTTP $httpCode - $response\n", FILE_APPEND);
} else {
    file_put_contents("sms_response_log.txt", "[" . date("Y-m-d H:i:s") . "] No unpaid customers found.\n", FILE_APPEND);
}
}
?>