<?php
 include_once('../common/base.php');

 $response=array();
 $tdata=array();
 $total=0;
 $pageIndex=isset($_GET['page'])?$_GET['page']:1;
 $pageSize=5;
 if(isset($config['parameters']['work_order_data_show']) && $config['parameters']['work_order_data_show']=='0'){
     $pageSize=10;
 }

if(isset($config['parameters']['tool_kpi_enabled']) && $config['parameters']['tool_kpi_enabled']=='1'){
    $kpi=isset($config['parameters']['tool_kpi_boundary'])?$config['parameters']['tool_kpi_boundary']:0; 
    $sql="select  ROW_NUMBER() over ( order by
        case when a.currentCount is not null then 
            case when a.countLimit<>0 and cast(a.currentCount as decimal(18,6)) /a.countLimit >{$kpi} then 1 else 0 end 
        else
            case when a.timeLimit<>0 and cast(a.currentTime as decimal(18,6))/a.timeLimit >{$kpi} then 1 else 0 end 
        end desc,Equipment.EquNo asc
    ) as rownum,
    case when a.currentCount is not null then 
        case when a.countLimit<>0 and cast(a.currentCount as decimal(18,6)) /a.countLimit>{$kpi} then 1 else 0 end 
    else
        case when a.timeLimit<>0 and cast(a.currentTime as decimal(18,6))/a.timeLimit>{$kpi} then 1 else 0 end 
    end as needwarn,
    a.*,Equipment.EquNo from mdc_ToolLifeDetails a inner join Equipment on Equipment.MachineID=a.MachineID where convert(nvarchar(200),a.time,109)+'_'+a.toolNum+'_'+a.machineID=(select convert(nvarchar(200),max(b.time),109)+'_'+b.toolNum+'_'+b.machineID from mdc_ToolLifeDetails b where b.toolNum=a.toolNum and b.machineID=a.machineID group by b.machineID, b.toolNum)";

}else{
    $sql="select  ROW_NUMBER() over ( order by
        case when a.currentCount is not null then 
            case when a.prewarningCount<>0 and a.currentCount>a.prewarningCount then 1 else 0 end 
        else
            case when a.prewarningTime<>0 and a.currentTime>a.prewarningTime then 1 else 0 end 
        end desc,Equipment.EquNo asc
    ) as rownum,
    case when a.currentCount is not null then 
    case when a.prewarningCount<>0 and a.currentCount>a.prewarningCount then 1 else 0 end 
    else
    case when a.prewarningTime<>0 and a.currentTime>a.prewarningTime then 1 else 0 end 
    end as needwarn,
    a.*,Equipment.EquNo from mdc_ToolLifeDetails a inner join Equipment on Equipment.MachineID=a.MachineID where convert(nvarchar(200),a.time,109)+'_'+a.toolNum+'_'+a.machineID=(select convert(nvarchar(200),max(b.time),109)+'_'+b.toolNum+'_'+b.machineID from mdc_ToolLifeDetails b where b.toolNum=a.toolNum and b.machineID=a.machineID group by b.machineID, b.toolNum)";

 
}

$sql="select top {$pageSize} * from ({$sql}) as t where  rownum>".($pageIndex-1)*$pageSize;
$stmt=sqlsrv_query($conn, $sql);
if ($stmt) {
    while ($row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
      
        $t1=empty($row['currentTime'])?$row['currentCount']:$row['currentTime'];
        $t2=empty($row['currentTime'])?$row['countLimit']:$row['timeLimit'];
        if($t1==null){
            $t1=0;
        }
        if($t2==null){
            $t2=0;
        }
        $progress=0;
        if($t2!=0){
            $progress=$t1/$t2;
            $progress=round($progress,2)*100;
        }
        $row['progress']=$progress;
        $tdata[]=$row;
       
    }
    sqlsrv_free_stmt($stmt);
}

$sql2="select count(*) from mdc_ToolLifeDetails a inner join Equipment on Equipment.MachineID=a.MachineID where convert(nvarchar(200),a.time,109)+'_'+a.toolNum+'_'+a.machineID=(select convert(nvarchar(200),max(b.time),109)+'_'+b.toolNum+'_'+b.machineID from mdc_ToolLifeDetails b where b.toolNum=a.toolNum and b.machineID=a.machineID group by b.machineID,b.toolNum)";
$stmt2=sqlsrv_query($conn, $sql2);
if($stmt2){
    $row=sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_NUMERIC);
    $total=$row[0]==null?0:$row[0];
    sqlsrv_free_stmt($stmt2);
}

if ($total==0) {
    $pageIndex=0;
}

$response['tdata']=$tdata;
$response['code']=200;
$response['pageSize']=$pageSize;
$response['cur_page']=$pageIndex;
$response['total_page']=ceil($total/$pageSize);

echo json_encode($response);

?>