<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/carrierfunctions.php');
?>
<?php 
function get_orpupidsl_count($pid){
if(isset($_SESSION['axes_carorder'])){
$max=count($_SESSION['axes_carorder']);
$sum=0;
$qty=0;
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_carorder'][$i]['qty'];
$epid=$_SESSION['axes_carorder'][$i]['pid'];
if($pid==$epid){
$sum+=$qty;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}	
}

function get_orpuslpid_count($pid){
if(isset($_SESSION['axes_orcarse'])){
$max=count($_SESSION['axes_orcarse']);
$sum=0;
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_orcarse'][$i]['pid'];
if($pid==$epid){
$sum+=1;
}    
}
return $sum;    
}else{
return 0;    
}
}
    
function pid_orpuslexits($pid){   
$max=count($_SESSION['axes_orcarse']);
$flag=0;    
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['axes_orcarse'][$i]['pid']){
$flag=1;    
break;    
}    
}
return $flag;    
}

function remove_orpuslpid($pid,$dmax){
$c=0;    
$max=count($_SESSION['axes_orcarse']);
for($i=($max-1);$i>=0;$i=$i-1){    
if($pid==$_SESSION['axes_orcarse'][$i]['pid']){
$c+=1;    
unset($_SESSION['axes_orcarse'][$i]);
if($c==$dmax){
break;    
}     
}    
}
$_SESSION['axes_carorder']=array_values($_SESSION['axes_carorder']);    
}
?>
<table class="table table-bordered table-striped">
<?php if(get_or_mobile_count()>0){ ?>
<tr>
<?php 
$max=count($_SESSION['axes_carorder']);
for($i=0;$i<$max;$i++){
if($_SESSION['axes_carorder'][$i]['imei']>=1){
$pid=$_SESSION['axes_carorder'][$i]['pid'];
$check=$_SESSION['axes_carorder'][$i]['check'];    
$qty=$_SESSION['axes_carorder'][$i]['qty'];    
$eqty=get_orpupidsl_count($pid);

if(isset($_SESSION['axes_orcarse'])){    
if(pid_orpuslexits($pid)){
$dmax=0;    
$sqty=get_orpuslpid_count($pid);
if($sqty!=$eqty){
if($sqty<$eqty){
$dmax=($eqty-$sqty);   
for($p=0;$p<$dmax;$p++){
add_orserial($pid,'','M');        
}   
}elseif($sqty>$eqty){
$dmax=($sqty-$eqty);    
remove_orpuslpid($pid,$dmax);    
}        
}    
}else{
add_orserial($pid,'','M');    
}
}else{
add_orserial($pid,'','M');    
}    
}        
}    
?>
<td colspan="4" align="center"></td>
</tr>
<tr>
<th width="30px" align="center">#</th>
<th width="300px">Name</th>
<th width="170px">IMEI/Serial</th>
<th width="25px" align="center"><i class="fa fa-trash"></i></th>
</tr>    
<?php } ?>
<?php
if(isset($_SESSION['axes_orcarse'])){
usort($_SESSION['axes_orcarse'], function ($a, $b) {
return $a['cartid'] <=> $b['cartid'];
});    
if(is_array($_SESSION['axes_orcarse'])){
?>	
<?php
    $s=0;
    $max=count($_SESSION['axes_orcarse']);
    for($i=($max-1);$i>=0;$i=$i-1){ ?>
<?php 
    if($_SESSION['axes_orcarse'][$i]['reumei']>=1){
        $requ=1;	
    }else{
            
    }
?>
<?php $s=$s+1; ?>

        <tr>
            <td align="center"><?php echo $s; ?></td>
            <td <?php if($requ){echo 'style="color:red;"';}?>><?php echo $_SESSION['axes_orcarse'][$i]['name']; ?></td>
            <td><input type="text" name="imei_<?php echo $i; ?>" maxlength="45" class="form-control imei" id="imei_<?php echo $i; ?>" value="<?php echo $_SESSION['axes_orcarse'][$i]['imei']; ?>"  size="2" style="height: 24px;" autocomplete="off"/></td>
            <td align="center"><a id="<?php echo $i; ?>" class="slremove"><span style="cursor: pointer;" class="fa fa-times"></span></a></td>
        </tr>
<?php } ?>	
<?php }else{ ?>
        <tr>
            <td colspan="4" align="center">No IMEI/Serial Information!!</td>
        </tr> 
<?php } ?> 
<?php }else{ ?>
        <tr>
            <td colspan="4" align="center">No IMEI/Serial Information!!!!!!</td>
        </tr>
<?php } ?>
</table>