<?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';


//SET PAGE ORIGIN FOR NAVIGATION AND SECURITY
$page = $_SESSION['origin'] = 'report_contracts_billing';
$prev_page = ($_SESSION['origin'] == 'equipments') ? $_SESSION['prev_origin_equipment'] : (($_SESSION['origin'] == 'account')? $_SESSION['prev_origin'] :'');

//create backbutton to prev_origin
$back_btn_orgin =  ($prev_page != '')? '<a href="'.$prev_page.'" class="btn alt mar-right-2">'.$button_back.'</a>':'';

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

//Defaults
$contract = [
    'type' => (isset($_GET['type'])? $_GET['type'] : ''),
    'billing_plan' => (isset($_GET['billing_plan'])? $_GET['billing_plan'] : ''),
    'year' => (isset($_GET['year'])? $_GET['year'] : ''),
    'month' => (isset($_GET['month'])? $_GET['month'] : '')
  ];

//GET PARAMETERS
$pagination_page = isset($_GET['p']) ? $_GET['p'] : 1;
$status = isset($_GET['status']) ? '&status='.$_GET['status'] : '';
$search = isset($_GET['search']) ? '&search='.$_GET['search'] : '';

// Determine the URL
$url = 'index.php?page=report_contracts_billing'.$status.$search;

//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';

//CALL TO API
$api_url = '/v2/billing/'.$GET_VALUES;
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = json_decode($responses,true);}else{$responses = null;}

//Return QueryTotal from API
$api_url = '/v2/billing/'.$GET_VALUES.'&totals=';
$query_total = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total)){$query_total = json_decode($query_total,true);}else{$query_total = 0;}

template_header('reports', 'report_contracts_billing','view');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-box-open"></i>
        <div class="txt">
            <h2>'.$contract_h2.' ('.$query_total.')</h2>
            <p>'.$contract_p .'</p>
        </div>
    </div>
    '.$back_btn_orgin.'
</div>';


$view .= '
<div class="content-header responsive-flex-column pad-top-5">
     <form action="" method="get">
        <input type="hidden" name="page" value="report_contracts_billing">
            <div class="filters">
                <a href="#"><i class="fa-solid fa-filter"></i>'.$general_filters.'</a>
                <div class="list">
                    <label for="">'.$contract_type.'</label>
                    <select name="type">
                        <option value=""></option>
                        <option value="0" '.($contract['type']==0?' selected':'').'>'.$contract_type0.'</option>
                        <option value="1" '.($contract['type']==1?' selected':'').'>'.$contract_type1.'</option>
                        <option value="2" '.($contract['type']==2?' selected':'').'>'.$contract_type2.'</option>
                    </select>
                     <label for="">'.$contract_billinglabel.'</label>
                    <select name="billing_plan">
                        <option value=""></option>
                        <option value="4" '.($contract['billing_plan']==4?' selected':'').'>'.$contract_billing4.'</option>
                        <option value="0" '.($contract['billing_plan']==0?' selected':'').'>'.$contract_billing0.'</option>
                        <option value="1" '.($contract['billing_plan']==1?' selected':'').'>'.$contract_billing1.'</option>
                        <option value="2" '.($contract['billing_plan']==2?' selected':'').'>'.$contract_billing2.'</option>
                        <option value="3" '.($contract['billing_plan']==3?' selected':'').'>'.$contract_billing3.'</option>
                    </select>
                    <label for="">'.$general_year.'</label>
                    <select name="year">
                        <option value=""></option>
                        <option value="2023" '.($contract['year']==2023?' selected':'').'>2023</option>
                        <option value="2024" '.($contract['year']==2024?' selected':'').'>2024</option>
                        <option value="2025" '.($contract['year']==2025?' selected':'').'>2025</option>
                        <option value="2026" '.($contract['year']==2026?' selected':'').'>2026</option>
                        <option value="2027" '.($contract['year']==2027?' selected':'').'>2027</option>
                        <option value="2028" '.($contract['year']==2028?' selected':'').'>2028</option>
                        <option value="2029" '.($contract['year']==2029?' selected':'').'>2029</option>
                        <option value="2030" '.($contract['year']==2030?' selected':'').'>2030</option>
                    </select>
                    <label for="">'.$general_quarter.' - '.$general_month.'</label>
                    <select name="month">
                        <option value=""></option>
                        <optgroup label="Q1">
                            <option value="1" '.($contract['month']==1?' selected':'').'>Jan</option>
                            <option value="2" '.($contract['month']==2?' selected':'').'>Feb</option>
                            <option value="3" '.($contract['month']==3?' selected':'').'>Mar</option>
                        </optgroup>
                        <optgroup label="Q2">
                            <option value="4" '.($contract['month']==4?' selected':'').'>Apr</option>
                            <option value="5" '.($contract['month']==5?' selected':'').'>5</option>
                            <option value="6" '.($contract['month']==6?' selected':'').'>Jun</option>
                        </optgroup>
                        <optgroup label="Q3">
                            <option value="7" '.($contract['month']==7?' selected':'').'>Jul</option>
                            <option value="8" '.($contract['month']==8?' selected':'').'>Aug</option>
                            <option value="9" '.($contract['month']==9?' selected':'').'>Sept</option>
                        </optgroup>
                        <optgroup label="Q4">
                        <option value="10" '.($contract['month']==10 ?' selected':'').'>Oct</option>
                        <option value="11" '.($contract['month']==11 ?' selected':'').'>Nov</option>
                        <option value="12" '.($contract['month']==12 ?' selected':'').'>Dec</option>
                        </optgroup>
                    </select>
                <button type="submit">'.$button_apply.'</button>
            </div>
        </div>
        <div class="search">
            <label for="search">
                <input id="search" type="text" name="search" placeholder="'.$contract_search.'" value="" class="responsive-width-100">
                <i class="fas fa-search"></i>
            </label>
        </div>
    </form>
</div>
';

$view .= '
<div class="content-block">
  <div class="table">
    <table class="sortable">
      <thead>
          <tr>
              <th>#</th>
              <th>'.$contract_status.'</th>
              <th>'.$contract_account.'</th>
              <th>'.$contract_type.'</th>
              <th>'.$contract_billinglabel.'</th>
              <th>'.$contract_start_date.'</th>
              <th>'.$contract_end_date.'</th>
              <th>'.$contract_reference.'</th>
              <th>'.$contract_billing_count.'</th>
              <th>'.$contract_service_2.'</th>
              <th>'.$contract_overrun.'</th>
              <th>'.$contract_reference.'</th>
              <th>'.$general_actions.'</th>
          </tr>
      </thead>
    <tbody>
          ';

            if (empty($responses)){

                $view .= '
                <tr>
                    <td colspan="8" style="text-align:center;">'.$message_no_contract.'</td>
                </tr>';
            }
            else {
                foreach ($responses as $response){    

                    $view .= ' 
                    <tr>
                        <td>'.$response['#'].'</td>
                        <td>'.(($response['Status'] == $contract_status1)? '<span class="status enabled">'.$response['Status']: (($response['Status'] == $contract_status3) ? '<span class="status disabled">'.$response['Status'] : '<span class="status">'.$response['Status'])).'</span></td>
                        <td>'.$response['Account'].'</td>
                        <td>'.$response['Type'].'</td>
                        <td>'.$response['Billingplan'].'</td>
                        <td>'.$response['Start date'].'</td>
                        <td>'.$response['End date'].'</td>
                        <td>'.$response['Reference'].'</td>
                        <td>'.$response['Consumed'].'</td>
                        <td>'.$response['Contracted'].' </td>
                        <td><span class="status '.(($response['Deviation'] < 100) ? 'enabled' : 'disabled').'">'.$response['Deviation'].'</span></td>
                        <td>'.$response['Period'].'</td>
                        <td><a href="index.php?page=contract&rowID='.$response['#'].'" class="btn_link">'.$general_view .'</a></td>
                    </tr>
                    ';  
                }
            }
$view .= ' 
            </tbody>
        </table>
    </div>
</div>
';

$view.='<div class="pagination">';
if ($pagination_page > 1) {
    $page = $pagination_page-1;
    $view .= '<a href="'.$url.'&p=1">'.$general_first.'</a>';
    $view .= '<a href="'.$url.'&p='.$page.'">'.$general_prev.'</a>';
}
$totals = ceil($query_total / $page_rows_contracts) == 0 ? 1 : ceil($query_total / $page_rows_contracts);
$view .= '<span> '.$general_page.$pagination_page.$general_page_of.$totals.'</span>';
if ($pagination_page * $page_rows_contracts < $query_total){
    $page = $pagination_page+1;
    $view .= '<a href="'.$url.'&p='.$page.'">'.$general_next.'</a>';
    $view .= '<a href="'.$url.'&p='.$totals.'">'.$general_last.'</a>';

}
$view .= '</div>';
//OUTPUT
echo $view;

template_footer();
?>