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

  if ($rQ->rowCount()>0) {
  $chp=$rQ->fetchAll();
  foreach ($chp as $key => $p) {
    $nr=$p["orderID"];
                               }
                          }else{
    $Q =$con->genProfID();;
    $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-->
<font color="blue">
<table width="100%" border="0">
    <tr>
    <td colspan="2">
    <h4>ROMBO STAR PROFORMA INVOICE</h4>              
      
    </td>
</tr>
  <tr>
    <td valign="top">
        <h6>
            <p/><p/><p/>
        Address: Olympic Mabatini-Mwanza<br/>
        Tel:  +255 758087092<br/>
        Email: rombostarco.ltd@gmail.com<br/>         
      <h6>
    </td>
    <td>
          <h6>
            Proforma No:   
      <?php 

      echo "".$nr;
      ?>
      <br/>
      Date:  <?php echo Date("Y-m-d");?></h6>
      
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <img src="assets/logo.png" style="width:100%;height:70px;" >
    </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>
</font>

<!--CODE END YEAR-->

<div id="dR">
<?php
require_once "proformaL.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('isp'+i).value=rate;
document.getElementById('tprice'+i).value=rate*eQty;
document.getElementById('tp'+i).innerHTML=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;
 document.getElementById('tsp').innerHTML=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;
$('#rate'+i).show();
$('#isp'+i).hide(); 
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(){ 
    var ecc = 0;
    var cc = document.getElementById('cc').value;  
    var orderID = document.getElementById('orderID').value;
    var tsprice = document.getElementById('tprice').value;
    var cus = document.getElementById('ms').innerHTML;
    var user = document.getElementById('user').value;
    var sdate = document.getElementById('sdate').value;

    // Collect all rows
    var items = [];

    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 itemID = document.getElementById('item'+i).value;

        // Validation
        if(itemID=="" || cus=="" || tsprice=="" || orderID=="" || tprice=="" || iname=="" || eQty=="" || rate==""){ 
            ecc++;
        }

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

    if(ecc === 0){
        $.post("profSavPro.php", {
            cus: cus,
            orderID: orderID,
            user: user,
            tsprice: tsprice,
            sdate: sdate,
            items: JSON.stringify(items) // send as JSON string
        }, function(data){
            $('#disp').html(data).show();
        });
    } else {
        alert('All detail require!');
    }
}


/*function savData(){ 
var n=0;
var cash=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;
/*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("profSavPro.php",
          {cus:cus,itemID:itemID,n:n,orderID:orderID,user:user, 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 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('profSearch.php',{searchValue:searchValue},function(data){
      $('#dispR').html(data);

    });
    }
    
</script>
 