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

  if ($rQ->rowCount()>0) {
  $chp=$rQ->fetchAll();
  foreach ($chp as $key => $p) {
    $nr=$p["orderID"];
                               }
                          }else{
    $Q =$con->genOrderID();;
    $nr=$Q->rowCount()+1;
                      }
?> 
  
 ///////
 ///////
 <table>
   <tr>
     <td width="40%" valign="top"> 
 <input type="hidden" 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>  
    </div>
</div> 
 

<!-- SEARCH BOX END-->

 

 
 <div id="dispR">
   
 <?php
   //require_once "labToolkitListOp.php";
   ?>
   
 </div>
     </td>
     <td valign="top">
     <div id="co">  
<!--INVOICE  DEVELOPMENT CODE-->
<table width="100%" border="0">
  <tr>
    <td>
     KWIZERA INVESTMENT COMPANY SALES INVOICE               
      
    </td>
    <td>
          ORDER NO:   
      <?php 

      echo "".$nr;
      ?>
    </td>
  </tr>
  <tr>
   <td colspan="2">
<p/>
<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> 
      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>
    <tr>
    <td colspan="2">
      <img src="assets/logo.png" style="width:100%;height:70px;" >
    </td>

  </tr>

  </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 "pharmtempL.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" 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 processData(){ 
document.getElementById('dispR').innerHTML='';
var ecc=0; 
var cname =document.getElementById('cname').value; 
$.trim(cname);  
  //FIRST NAME VALIDATION
  
if(cname==""){ 

         ecc++;
         alert('Item customer is required!..');
             }
 
  
    if(ecc==0)
    { 
        $.post("pharmGetAgBN.php",
          {cname:cname},
          function(data){
      $('#ph').html(data).show();

    });
    }
  } 


function savData() {
    let ecc = 0;
    let pst = '';
    let cash = 0;

    // Current time
    const now = new Date();
    const hours = now.getHours();
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const ampm = hours >= 12 ? 'PM' : 'AM';
    const tim = `${hours}:${minutes}:${seconds} ${ampm}`;
    alert('Current Time: ' + tim);

    // Shared values
    const cc      = document.getElementById('cc').value;
    const pOpt    = document.getElementById('pOpt').value;
    const orderID = document.getElementById('orderID').value;
    const tsprice = document.getElementById('tprice').value;
    const cus     = document.getElementById('ms').innerHTML;
    const user    = document.getElementById('user').value;
    const sdate   = document.getElementById('sdate').value;

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

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

    // Build items array
    let items = [];
    for (let i = cc; i > 0; i--) {
        let itemID = document.getElementById('item' + i).value;
        let iname  = document.getElementById('iname' + i).value;
        let rate   = document.getElementById('rate' + i).value;
        let eQty   = document.getElementById('eQty' + i).value;
        let tprice = document.getElementById('tprice' + i).value;

        if (!itemID || !iname || !rate || !eQty || !tprice) {
            ecc++;
        }

        items.push({
            itemID: itemID,
            iname: iname,
            rate: rate,
            eQty: eQty,
            tprice: tprice
        });
    }

    // Final validation
    if (ecc === 0) {
        $.post("pharmInvpiceLSavPro.php", {
            cus: cus,
            tim: tim,
            orderID: orderID,
            user: user,
            cash: cash,
            pst: pst,
            pOpt: pOpt,
            tsprice: tsprice,
            sdate: sdate,
            items: JSON.stringify(items) // send all rows in one go
        }, function (data) {
            $('#disp').html(data).show();
        });
    } else {
        alert('All details required!');
    }
}



/*function savData(){ 
var n=0;
var cash=0;
var ecc =0;
var pst =''; 
const now = new Date(); var hours = now.getHours();
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const ampm = hours >= 12 ? 'PM' : 'AM';
var tim = `${hours}:${minutes}:${seconds} ${ampm}`;
alert('Current Time: ' + tim);  
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;
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(cus==""){ 
         ecc++;
              }
 if(tsprice==""){ 
         ecc++;
              }
if(pOpt==""){ 
         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;
 // alert(n);
  var 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;
  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;
        }    
         
         $.post("pharmInvpiceLSavPro.php",
          {cus:cus,tim:tim,itemID:itemID,n:n,orderID:orderID,user:user,cash:cash,pst:pst,rate:rate,eQty:eQty,pOpt:pOpt,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 submision(){
    
    var searchValue=$('#tname').val();
    $('#dispR').empty();
    $.post('pharmInvpiceL.php',{searchValue:searchValue},function(data){
      $('#dispR').html(data);

    });
  }*/

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

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

    });
    }
    
</script>
 