<?php
require_once"labClass.php";
$con=new labClass();
$rQ= $con->getThisPTemOrder();

  if ($rQ->rowCount()>0) {
  $chp=$rQ->fetchAll();
  foreach ($chp as $key => $p) {
    $nr=$p["orderID"];
                               }
                          }else{
    $Q =$con->genPOrderID();;
    $nr=$Q->rowCount()+1;
                      }
$Q =$con->genPOrderID();;
    $nr=$Q->rowCount()+1;

   //echo $Q->rowCount();                  
?>
 <table>
   <tr>
     <td width="40%" valign="top"> 
 <!--<input type="number" id="orderID" value="<?php echo $nr;?>" name="orderID" required>-->
 
   <div class="row">
    <div class="col">
      <h6>Search item here:</h6>  
      <input type="text" onkeyup ="submision()" class="form-control" id="tname" style="width:100%; height:40px; border-radius:12px;" placeholder="Test name" name="tname" required>
       <a href="#" onclick="
         var orderID =$('#orderID').val();var user =$('#user').val(); var sdate =$('#sdate').val(); if(orderID!='' && user!='' && sdate!='' ) { $.post('createPurchFos.php',{orderID:orderID,user:user,sdate:sdate},function(data){$('#dR').html(data).show();});} else{document.getElementById('rdn').innerHTML='Valide item detail required!..';}" >Create from os</a> <a href="#" onclick="
         var orderID =$('#orderID').val();var user =$('#user').val(); var sdate =$('#sdate').val(); if(orderID!='' && user!='' && sdate!='' ) { $.post('createPorderLL.php',{orderID:orderID,user:user,sdate:sdate},function(data){$('#dR').html(data).show();});} else{document.getElementById('rdn').innerHTML='Valide item detail required!..';}" >View list</a>
         
         <sub id="rdn"></sub>
    </div>
</div> 
<div class="row">
    <div class="col">
       
       
       
    

       
       
       
       
       
       
       
       
       
       
       
         
         
         <sub id="rdn"></sub>
    </div>
</div> 
 

<!-- SEARCH BOX END-->

 

 
 <div id="dispR">
   
   <?php
 $c=0;
// Fetch all order IDs and customer names
$orders = $con->genPOrderIDQ();
?>

<div class="container mt-4 p-4 bg-white border rounded shadow-sm">
  <h5 class="text-primary mb-3 fw-bold">Purchases list</h5>

   
    <ul class="list-group">
      <?php foreach ($orders as $o):$c++; ?>
        <li class="list-group-item d-flex justify-content-between align-items-center">
          <!-- Order ID as clickable link -->
          <input type ="hidden" name="orid<?php echo $c;?>" id="orid<?php echo $c;?>" value="<?= htmlspecialchars($o['orderID']) ?>"/>
          <a href="#" class="text-decoration-none fw-bold text-primary" 
             onclick=" var orderID =$('#orid<?php echo $c;?>').val();alert(orderID);var user =$('#user').val(); var sdate =$('#sdate').val(); if(orderID!='' && user!='' && sdate!='' ) { $.post('pqueryList.php',{orderID:orderID,user:user,sdate:sdate},function(data){$('#dR').html(data).show();});} else{document.getElementById('rdn').innerHTML='Valide item detail required!..';}">
             <?= htmlspecialchars($o['orderID']) ?>. 
          </a>

          <!-- Customer Name -->
          <span class="text-dark">
            <?= htmlspecialchars($o['cname']) ?>
          </span>
        </li>
      <?php endforeach; ?>
    </ul>
 
</div>

 
   
 </div>
     </td>
     <td valign="top">
     <div id="co">  
<!--INVOICE  DEVELOPMENT CODE-->
<table width="100%" border="0">
     <tr>
    <td colspan="2" align="center">
     KWIZERA INVESTMENT COMPANY PURCHASE ORDER                
      
    </td>
     
  </tr>
  <tr>
    <td>
     <b style="color:blue;">KWIZERA INVESTMENT COMPANY<br/>P.O.BOX -<br/>KATORO GEITA - TANZANIA</b>              
      
    </td>
    <td>
          ORDER NO:   
      <?php 
     // echo "".$nr;
      ?>
      <input style="background-color:white;border-radius:12px; width:40%;" type="number" id="orderID" value="<?php echo $nr;?>" name="orderID" required>
    </td>
  </tr>
  <tr>
   <td>
 <b style="color:blue;">Location:  Katoro-Geita<br/>Tel:  +255 788253958/796318675<br/>Email:  speciosylver@gmail.com <br/>
Date:<?php echo Date("Y-m-d");?>
      <br></b>  
      Pay by:
<select class="form-control" name="pOpt" id="pOpt">
    <option value="">Select</option>
    <option value="CASH">CASH</option>
    <option value="M-PESA">M-PESA</option>
    <option value="AIRTEL MONEY">AIRTEL MONEY</option>
    <option value="TIGO PESA">TIGO PESA</option>
    <option value="HALO PESA">HALO PESA</option>
    <option value="NMB">NMB</option>
    <option value="CRDB">CRDB</option>
    <option value="Unspecified">Unspecified</option> 
    </select>
    </td>
    <td>
      <img src="" >
    </td>

  </tr>
   <tr>
    <td colspan="2">
     
   <div id="cp">M/S:</div>
        <input class="form-control" id="cus" name="cus" list="cnames" onchange="getCusN()">
<datalist id="cnames">
<?php
 $Q= $con->getCusRecord();;
 if ($Q->rowCount()>0) {
 $lb=$Q->fetchAll();
     foreach ($lb as $key => $l){
      ?>
      <option value="<?php echo  $l["cname"];?>">  
             <?php
           }
           }
      ?></datalist>  
    </td>
  </tr>
</table>


<!--CODE END YEAR-->

<div id="dR">
<?php
require_once "pharmptempLLPO.php";
//require_once "createPorderLL.php";
?></div>
</div>
<div>
   <button type="submit" class="btn btn-primary" value="Print" style="width:13%; height:40px;" onclick="printPageArea();">Print </button>
   <button type="submit" class="btn btn-primary"  style="width:13%; height:40px;" onclick="editData();">Edit </button><button type="submit" class="btn btn-primary"  style="width:13%; height:40px;" onclick="clearData();">Clear </button>
   <button type="submit" id="sbtn" value="Save" class="btn btn-primary"  style="width:13%; height:40px;" onclick="savData();">Save </button>
</div>
     </td>
   </tr>
 </table>
 <script>
 function shareData(){ 
  var ecc =0;
var tprice =0;
var cc=document.getElementById('cc').value; 
for (var i = cc; i > 0; i--) {
  

var iname =document.getElementById('iname'+i).value; 
//document.getElementById('iname'+i).disabled=false; 
var rate =document.getElementById('rate'+i).value; 
//document.getElementById('rate'+i).disabled=false;
var eQty =document.getElementById('eQty'+i).value; 
//document.getElementById('eQty'+i).disabled=false;
document.getElementById('tprice'+i).value=rate*eQty;
//tprice+rate*eQty; 
tprice =tprice+rate*eQty;
//alert(tprice);
  
  
  
if(tprice==""){ 
         ecc++;
              }
     
if(iname==""){ 
         ecc++;
             }
if(eQty==""){ 
         ecc++;
             }
if(rate==""){ 
         ecc++;
             }
}
 document.getElementById('tprice').value=tprice;
 //alert(tprice);
} 

 function editData(){ 
  var ecc =0;
var cc=document.getElementById('cc').value; 
for (var i = cc; i > 0; i--) {
  

//var iname =document.getElementById('iname'+i).value; 
//document.getElementById('iname'+i).disabled=false; 
var rate =document.getElementById('rate'+i).value; 
document.getElementById('rate'+i).disabled=false;
var eQty =document.getElementById('eQty'+i).value; 
document.getElementById('eQty'+i).disabled=false;
var tprice =document.getElementById('tprice'+i).value; 
//document.getElementById('tprice'+i).disabled=false;

  
  
  
if(tprice==""){ 
         ecc++;
              }
     
//if(iname==""){ 
         //ecc++;
             //}
if(eQty==""){ 
         ecc++;
             }
if(rate==""){ 
         ecc++;
             }
}



   /* if(ecc==0)
    { 
        $.post("expensesRegFormPro.php",
          {submit:submit,tname:tname,tPrice:tPrice,sdate:sdate},
          function(data){
      $('#disp').html(data).show();

    });
    }*/
 
} 
 
 
 
 function savData() {
    var ecc = 0;
    var dataArray = [];

    var cc = document.getElementById('cc').value;
    var pOpt = document.getElementById('pOpt').value;
    var orderID = document.getElementById('orderID').value;
    var tsprice = document.getElementById('tprice').value;
    var cus = document.getElementById('ms').innerHTML;
    var add = document.getElementById('mc').innerHTML;
    var user = document.getElementById('user').value;
    var sdate = document.getElementById('sdate').value;

    var pst = '';
    var cash = '';

    if (document.getElementById('pst').checked == true) {
        pst = document.getElementById('pst').value;
        cash = document.getElementById('tprice').value;
    } else if (document.getElementById('npst').checked == true) {
        pst = document.getElementById('npst').value;
        cash = document.getElementById('cash').value;
    } else {
        ecc++;
    }

    // Validate shared fields
    if (!pOpt || !cus || !add || !tsprice || !orderID) {
        ecc++;
    }

    // Loop through item rows
    for (var i = cc; i > 0; i--) {
        var itemID = document.getElementById('item' + i).value;
        var iname = document.getElementById('iname' + i).value;
        var rate = document.getElementById('rate' + i).value;
        var eQty = document.getElementById('eQty' + i).value;
        var tprice = document.getElementById('tprice' + i).value;

        // Validate item fields
        if (!itemID || !iname || !rate || !eQty || !tprice) {
            ecc++;
        }

        // Prepare row object
        dataArray.push({
            itemID: itemID,
            iname: iname,
            rate: rate,
            eQty: eQty,
            tprice: tprice
        });
    }

    // Final validation check
    if (ecc == 0) {
        // Submit entire payload at once
        $.post("pharmPurchaseOrderProLPO.php", {
            cus: cus,
            add: add,
            orderID: orderID,
            pOpt: pOpt,
            user: user,
            cash: cash,
            pst: pst,
            tsprice: tsprice,
            sdate: sdate,
            items: JSON.stringify(dataArray) // send as JSON string
        }, function (data) {
            $('#disp').html(data).show();
        });
    } else {
        alert('All detail required!');
    }
}

 
 
 
 
 
/*function savData(){ 
var cash=0;
var n=0;
var ecc =0;
var pst ='';
var cc=document.getElementById('cc').value;
var pOpt=document.getElementById('pOpt').value;  
for (var i = cc; i > 0; i--) {

var iname =document.getElementById('iname'+i).value;   
var rate =document.getElementById('rate'+i).value; 
var eQty =document.getElementById('eQty'+i).value;  
var tprice =document.getElementById('tprice'+i).value;  
var orderID =document.getElementById('orderID').value;
var itemID =document.getElementById('item'+i).value;
var tsprice =document.getElementById('tprice').value;
var cus =document.getElementById('ms').innerHTML;
var add =document.getElementById('mc').innerHTML;
if( document.getElementById('pst').checked==true)
        {
    var pst =document.getElementById('pst').value;
        }
else if(document.getElementById('npst').checked==true)
        {
    var pst =document.getElementById('npst').value;
        }    
          else{
         ecc++;
              }
if(itemID==""){ 
         ecc++;
              }
if(pOpt==""){ 
         ecc++;
              }
 if(cus==""){ 
         ecc++;
              }
    if(add==""){ 
         ecc++;
              }
 if(tsprice==""){ 
         ecc++;
              }
if(orderID==""){ 
         ecc++;
              }
if(tprice==""){ 
         ecc++;
              }

if(tprice==""){ 
         ecc++;
              }

if(iname==""){ 
         ecc++;
             }

if(eQty==""){ 
         ecc++;
             }

if(rate==""){ 
         ecc++;
             }
}
////////////////////
 if(ecc==0)
    { 
for (var i = cc; i > 0; i--) {
  n=i;

  pOpt=document.getElementById('pOpt').value;
  iname =document.getElementById('iname'+i).value;  
  rate =document.getElementById('rate'+i).value; 
  eQty =document.getElementById('eQty'+i).value;  
  tprice =document.getElementById('tprice'+i).value;  
  orderID =document.getElementById('orderID').value;
  tsprice =document.getElementById('tprice').value;
  itemID =document.getElementById('item'+i).value;
  cus =document.getElementById('ms').innerHTML;
  add =document.getElementById('mc').innerHTML;
  var user =document.getElementById('user').value;
  var sdate =document.getElementById('sdate').value;
  if( document.getElementById('pst').checked==true)
        {
        pst =document.getElementById('pst').value;

        cash =document.getElementById('tprice').value;
        }
   else if(document.getElementById('npst').checked==true)
        {
        pst =document.getElementById('npst').value;
        cash =document.getElementById('cash').value;
        }    
         alert(n);
         $.post("pharmPurchaseOrderProLPO.php",
          {cus:cus,add:add,itemID:itemID,orderID:orderID,n:n,pOpt:pOpt,user:user,cash:cash,pst:pst,rate:rate,eQty:eQty,iname:iname,tprice:tprice,tsprice:tsprice,sdate:sdate},
          function(data){
      $('#disp').html(data).show();

    }); }
}else{
    alert('All detail require!');
}
////////////////
} */
 
  function printPageArea(){
    var printContent = document.getElementById('co').innerHTML;
    var originalContent = document.body.innerHTML;
    document.body.innerHTML = printContent;
    window.print();
    document.body.innerHTML = originalContent;
}
function ncashS(){
     $('#cs').show();
     document.getElementById('pst').checked=false;
  }
 function cashS(){
    
       document.getElementById('npst').checked=false;
      $('#cs').hide();
      document.getElementById('cash').value=0;
    }
    
   function clearData(){
    
    var user=$('#user').val();
    $('#dR').empty();
    $.post('clearPurchOs.php',{user:user},function(data){
      $('#dR').html(data);

    });
  }
  function submision(){
    
    var searchValue=$('#tname').val();
    $('#dispR').empty();
    $.post('pharmPurchaseOrderItemListLPO.php',{searchValue:searchValue},function(data){
      $('#dispR').html(data);

    });
  }

   function getCusN(){
    
    var cus=$('#cus').val();
    $('#cp').empty();
    $.post('pharmgetcus.php',{cus:cus},function(data){
      $('#cp').html(data);});
    $('#cus').hide();
  }
 
</script>
 