<?php
defined(page_security_key) or exit;

$page = 'maintenance';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}
//PAGE Security
$update_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'U');
$delete_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'D');
$create_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'C');

if ($update_allowed === 1){
    if (isset($_POST['geoupdate'])){
        //GEOLOCATION UPDATE 
        geolocationUpdate($_SESSION['userkey']); 
    }
    
    if (isset($_POST['closeContract'])){
        //GEOLOCATION UPDATE 
        closeContract();
    }
    if (isset($_POST['updatecartest'])){
        //GEOLOCATION UPDATE 
        convertCartest();
    }
    if (isset($_POST['updateproductmeusurements'])){
        $total_measurement = traintotalMeasurement();
        $total_results = statisticalAnalyses($total_measurement);
        storeMeasurementProduct($total_results, $_SESSION['userkey']);
    }
    if (isset($_POST['updateequipmentmeusurements'])){
        storeMeasurementEquipment('');
    }
    if (isset($_POST['generatefile'])){
        generateLanguageFile($_POST['language'],$_SESSION['userkey']);
    }  
    if (isset($_POST['generateDealerInformation'])){
        generateDealerInformation($_SESSION['userkey']);
    }
}

// Handle success messages
if (isset($_GET['success_msg'])) {
    if ($_GET['success_msg'] == 1) {
        $success_msg = 'Settings updated successfully!';
    }
}

//EMPTY VIEW
$view = '';

template_header('Maintenance', 'maintenance', 'manage');

if (isset($success_msg)){
    $view .= ' <div class="msg error">
                    <i class="fas fa-check-circle"></i>
                    <p>'.$success_msg.'</p>
                    <i class="fas fa-times"></i>
                </div>';
}

$view .='
<form action="" method="post">
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">Maintenance</h2>
</div>';
   
$view .= '<div class="tabs">
            <a href="#" class="active">'.$general_actions .'</a>
            <a href="#" class="">Learning</a>
            <a href="#" class="">Translations</a>
        </div>
    ';

if ($update_allowed === 1){
$view .= '<div class="content-block tab-content active">
        <div class="form responsive-width-100">
            <label for="service">Expired contract closure</label>
            <input type="submit" name="closeContract" style="width: 15%;" value="closeContract" class="btn">
        </div>
        <div class="form responsive-width-100">
            <label for="service">CarTestUpdate</label>
            <input type="submit" name="updatecartest" style="width: 15%;" value="CarTestUpdate" class="btn">
        </div>
        <div class="form responsive-width-100">
            <label for="service">GeoUpdate</label>
            <input type="submit" name="geoupdate" style="width: 15%;" value="GeoUpdate" class="btn">
        </div>
        <div class="form responsive-width-100">
            <label for="service">GenerateDealerInfo</label>
            <input type="submit" name="generateDealerInformation" style="width: 15%;" value="DealerInfo" class="btn">
        </div>
   </div>
</div>';
}
if ($update_allowed === 1){
    $view .= '<div class="content-block tab-content">
        <div class="form responsive-width-100">
            <label for="service">Train Products</label>
            <input type="submit" name="updateproductmeusurements" style="width: 15%;" value="Train Products" class="btn">
       </div>
        <div class="form responsive-width-100">
            <label for="service">Train Assets</label>
            <input type="submit" name="updateequipmentmeusurements" style="width: 15%;" value="Train Assets" class="btn">
       </div>
    </div>';
    }

if ($update_allowed === 1){
    $view .= '<div class="content-block tab-content">
        <div class="form responsive-width-100">
            <label for="">'.$User_language.'</label>
            <select id="language" name="language">';
                $view .='<option value=""></option>';
            foreach ($supportedLanguages as $language){
                $view .='<option value="'.$language.'">'.$language.'</option>';
            }   
$view .='   </select>
            <input type="submit" name="generatefile" style="width: 15%;" value="Generate language" class="btn">
        </div>
    </div>';
    }

$view .= '</form>';

//Output
echo $view;
template_footer()
?>