<?php require('../connection.php');

function listPlant(){
    $stmt = $GLOBALS['con']->prepare("SELECT `itemnumber`,`plantnumber`,`desc`,`serialnumber` FROM `itemdetails` WHERE `clientnumber`='".$_REQUEST['client']."' AND `statuslive`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();	
while($pd=mysqli_fetch_array($cc)){$ca[]=$pd;}

$ret='<select class="form-control form-control-sm autofill w-100" id="plant" name="plant"><option value="">SELECT</option>'; 
foreach($ca as $px){
$ret.='<option value="'.$px['desc'].'|'.$px['plantnumber'].'" title="'.$px['desc'].'" >| '.$px['itemnumber'].' | '.$px['desc'].' - '.$px['plantnumber'].' / '.$px['serialnumber'].'</option>';//.PHP_EOL;
}
$ret.='</select>';
return($ret.$error);
}
//STE & RT - ITEM DESC, plant number: PLANTNUMBER
function serviceOptions(){
$services=array(
array("code"=>"CONS","desc"=>"Consultancy Work"),
array("code"=>"INSP","desc"=>"In Service Inspection"),
array("code"=>"ST","desc"=>"Inspection"),
array("code"=>"PP","desc"=>"Prior to purchase Inspection"),
array("code"=>"PO","desc"=>"Purchase Order"),
array("code"=>"SS","desc"=>"Special Service"),
array("code"=>"TECH","desc"=>"Technical Service")
);
foreach($services as $arrays=>$values){
$ret.='<option value="'.$values['desc'].'">'.$values['desc'].'</option>';
}
return $ret;
}

?>

<div class="form-group row">
<div class="col-md-2"><label for="service">Invoice Entry</label></div>
<div class="col-md-4">
<select id="service" name="service" class="form-control autocomplete" data-placeholder="Select">
<option value="">Select</option>
<option value="">Blank</option>
<?php echo serviceOptions()?>
</select>
</div>
</div>

<div id="plantRow" class="hidden bg-info p-2 mb-2">
<div class="form-group row">
<div class="col-md-2"><label class="control-label text-white" for="desc">Item</label></div>
<div class="col-md-9"><?php echo listPlant()?></div>
</div>
</div>

<div class="form-group row">
<div class="col-md-2"><label for="notes">Notes</label></div>
<div class="col-md-9">
<textarea id="notes" name="notes" class="form-control"></textarea>
</div>
</div>

<div class="form-group row">
<div class="col-md-2"><label for="rate">Fee</label></div>
<div class="col-md-6"><input type="text" id="rate" name="rate" class="form-control" placeholder="0.00"/></div>
</div>

<div class="form-group row">
<div class="col-md-2"><label for="rate">Apply Commission</label></div>
<div class="col-12 col-md-6 form-check">
<input type="checkbox" class="form-check-input" name="contractMode" id="rcom" name="rcom" value="1">
<label class="form-check-label" for="rcom"></label>
</div>
</div>

<div class="form-group row">
<div class="col-md-2"><label for="quantity">Quantity</label></div>
<div class="col-md-3">
<select id="quantity" name="quantity" class="form-control">
<?php foreach(range(1,10) as $num){?><option value="<?php echo $num?>"><?php echo $num?></option><?php };?>
</select>
</div>
</div>
<div class="spacer"></div>
<div class="text-center"><a href="javascript:void(0)" class="btn btn-info" onClick="addtoInvoice()">Add to invoice</a></div>


<script>
$('#service').on('change',function(){
var item=$(this).val()
switch (item){
    default:
        $('#plantRow').css('display','none')
    break;
    case "Inspection":
        $('#plantRow').css('display','block')
    break;
   // case "Purchase Order":
    //    $('#plantRow').css('display','block')
    //break;
}

})


$('#plant').on('change',function(){
var item=$(this).val()
item=item.split('|')
$('#notes').text(item[0]+', plant number: '+item[1])

})
$(".autofill").select2({ theme: "bootstrap", dropdownParent: $('#enrepContent') });
</script>