<?php
defined(page_security_key) or exit;

if (debug && debug_id == $_SESSION['id']){
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);  
}

include_once './assets/functions.php';
include_once './settings/settings_redirector.php';

//For navigation
$_SESSION['prev_origin'] = $_SERVER['REQUEST_URI'];

$page = 'report_build';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}

//Return RAW from API
$api_url = '/v1/equipments/productrowid=0&status=0&totals=';
$query_total_raw = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_raw)){$query_total_raw = decode_payload($query_total_raw);}else{$query_total_raw = null;}

//Return SEMI FINISHED GOODS from API
$api_url = '/v1/equipments/productrowid=0&status=1&totals=';
$query_total_sfg = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_sfg)){$query_total_sfg = decode_payload($query_total_sfg);}else{$query_total_sfg = null;}

//Return FINISH GOODS from API
$api_url = '/v1/equipments/productrowid=0&status=2&totals=';
$query_total_fg = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_fg)){$query_total_fg = decode_payload($query_total_fg);}else{$query_total_fg = null;}

//Return warranty from API
$api_url = '/v1/equipments/status=2&totals=';
$query_total_onstock = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_onstock)){$query_total_onstock = decode_payload($query_total_onstock);}else{$query_total_onstock = null;}



$startdate = date("Y-m-d", strtotime("-7 days"));
$enddate = date("Y-m-d");

//Return SFG from API - total
$api_url = '/v1/changelog/object_field=status&object_value=1&reporttype=1&between='.$startdate.'||'.$enddate;
$query_SFG_total = ioServer($api_url,'');
//Decode Payload
if (!empty($query_SFG_total)){$query_SFG_total = decode_payload($query_SFG_total);}else{$query_SFG_total = null;}

//Return SFG from API - details
$api_url = '/v1/changelog/object_field=status&object_value=1&reporttype=2&between='.$startdate.'||'.$enddate;
$query_SFG_details = ioServer($api_url,'');
//Decode Payload
if (!empty($query_SFG_details)){$query_SFG_details = decode_payload($query_SFG_details);}else{$query_SFG_details = null;}

//Return FG from API - totals
$api_url = '/v1/changelog/object_field=status&object_value=2&reporttype=1&between='.$startdate.'||'.$enddate;
$query_FG_total = ioServer($api_url,'');
//Decode Payload
if (!empty($query_FG_total)){$query_FG_total = decode_payload($query_FG_total);}else{$query_FG_total = null;}

//Return FG from API - details
$api_url = '/v1/changelog/object_field=status&object_value=2&reporttype=2&between='.$startdate.'||'.$enddate;
$query_FG_details = ioServer($api_url,'');
//Decode Payload
if (!empty($query_FG_details)){$query_FG_details = decode_payload($query_FG_details);}else{$query_FG_details = null;}

template_header('Build', 'report_build','view');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-house"></i>
        <div class="txt">
            <h2>'.$buildreport_h2.'</h2>
            <p>'.$buildreport_p.'</p>
        </div>
    </div>
</div>';

$view .= '
<div class="dashboard">
    <div class="content-block stat">
        <div class="data" onclick="location.href=\'index.php?page=equipments&productrowid=0&status=0\'">
            <h3>'.$product_location_raw.'</h3>
            <p>'.$query_total_raw.'</p>
        </div>
        <i class="fas fa-box-open"></i>
        <div class="footer">
            <i class="fa-solid fa-rotate fa-xs"></i>'.$buildreport_raw_text.'</div>
    </div>

    <div class="content-block stat">
    <div class="data" onclick="location.href=\'index.php?page=equipments&productrowid=0&status=1\'">
        <h3>'.$product_location_SFG.'</h3>
        <p>'.$query_total_sfg.'</p>
    </div>
    <i class="fa-solid fa-calendar-check"></i>
    <div class="footer">
        <i class="fa-solid fa-rotate fa-xs"></i>'.$buildreport_SFG_text.'
    </div>
    </div>

    <div class="content-block stat">
        <div class="data" onclick="location.href=\'index.php?page=equipments&productrowid=0&status=2\'">
            <h3>'.$product_location_FG.'</h3>
            <p>'.$query_total_fg.'</p>
        </div>
        <i class="fas fa-clipboard-check"></i>
        <div class="footer">
            <i class="fa-solid fa-rotate fa-xs"></i>'.$buildreport_FG_text.'
        </div>
    </div>

    <div class="content-block stat">
    <div class="data" onclick="location.href=\'index.php?page=equipments&status=2\'">
        <h3>'.$status2_text.'</h3>
        <p>'.$query_total_onstock.'</p>
    </div>
    <i class="fa-solid fa-calendar-check"></i>
    <div class="footer">
        <i class="fa-solid fa-rotate fa-xs"></i>'.$buildreport_onstock_text.'
    </div>
</div>
</div>';

$details_SFG = changeLogVisual(json_decode(json_encode($query_SFG_total), true), json_decode(json_encode($query_SFG_details), true));
$details_FG = changeLogVisual(json_decode(json_encode($query_FG_total), true),json_decode(json_encode($query_FG_details), true));

$view .= '
<div class="content-block-wrapper">
<div class="content-block order-details">
    <div class="block-header">
    <i class="fa-solid fa-bars fa-sm"></i>'.$data_build_sfg_view.'
    </div>'.$details_SFG.'</div>
    <div class="content-block order-details">
    <div class="block-header">
    <i class="fa-solid fa-bars fa-sm"></i>'.$data_build_fg_view.'
    </div>'.$details_FG.'</div>          
</div>
';

//OUTPUT
echo $view;

template_footer();
