<?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 ORIGIN FOR NAVIGATION
$_SESSION['prev_origin'] = $_SERVER['REQUEST_URI'];
$page = $_SESSION['origin'] = 'account';

//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');
$healthindex_allowed = isAllowed('report_healthindex' ,$_SESSION['profile'],$_SESSION['permission'],'R');

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

//CALL TO API FOR General information
$api_url = '/v1/accounts/'.$GET_VALUES;
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
$responses = $responses[0];

// Handle success messages
if (isset($_GET['success_msg'])) {
    if ($_GET['success_msg'] == 1) {
        $success_msg = $message_account_1;
    }
    if ($_GET['success_msg'] == 2) {
        $success_msg = $message_account_2;
    }
    if ($_GET['success_msg'] == 3) {
        $success_msg = $message_account_3;
    }
}

//Get variables
$account_details = json_decode($responses->accountdetails);
$account_hierarchy = json_decode($responses->accounthierarchy);

template_header('Account', 'account', 'view');
$view = '
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">'.$account_h2.' - '.$_GET['rowID'].'</h2>
        <a href="index.php?page=accounts" class="btn alt mar-right-2">'.$button_cancel.'</a>
';
if ($update_allowed === 1){
    $view .= '<a href="index.php?page=account_manage&rowID='.$_GET['rowID'].'" class="btn">Edit</a>';
}

$view .= '</div>';

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

$view .= '<div class="content-block-wrapper">';

$view .= '  <div class="content-block order-details">
                <div class="block-header">
                <i class="fa-solid fa-circle-info"></i></i>'.$view_account_information.'
                </div>
                <div class="order-detail">
                    <h3>'.$account_status.'</h3>
                    <p>'.(($responses->status == 'Customer')? '<span class="status enabled">'.$responses->status:'<span class="status">'.$responses->status).'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$account_name.'</h3>
                    <p>'.$account_details->billcompany.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$account_firstname.'</h3>
                    <p>'.$account_details->billfirstname.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$account_lastname.'</h3>
                    <p>'.$account_details->billlastname.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$account_email.'</h3>
                    <p>'.$account_details->billemail.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$account_phone.'</h3>
                    <p>'.$account_details->billphonenumber.'</p>
                </div>
            ';
$view .='                
            </div>
';

$view .='<div class="content-block order-details">
            <div class="block-header">
                <i class="fa-solid fa-user fa-sm"></i>'.$view_account_contact.'
            </div>
            <div class="order-detail">
                <h3>'.$account_contactfirstname.'</h3>
                <p>'.$account_details->contactfirstname.'</p>
            </div>
            <div class="order-detail">
                <h3>'.$account_contactlastname.'</h3>
                <p>'.$account_details->contactlastname.'</p>
            </div>
            <div class="order-detail">
                <h3>'.$account_contactemail.'</h3>
                <p>'.$account_details->contactemail.'</p>
            </div>
            <div class="order-detail">
                <h3>'.$account_contactphonenumber.'</h3>
                <p>'.$account_details->contactphonenumber.'</p>
            </div>
        </div>';

$view .= '</div>';
$view .= '<div class="content-block">
            <div class="block-header">
                <i class="fa-solid fa-bars fa-sm"></i>'.$account_billing.'
            </div>
             <div class="table order-table">
            <table>
                <tr>
                    <td style="width:25%;">'.$account_billstreetadress.'</td>
                    <td>'.$account_details->billstreetadress.'</td>
                </tr>
                <tr>
                    <td></td>
                    <td>'.$account_details->billdistrict.'</td>
                </tr>
                <tr>
                    <td style="width:25%;">'.$account_billpostalcode.'</td>
                    <td>'.$account_details->billpostalcode.'</td>
                </tr>
                <tr>
                    <td>'.$account_billcity.'</td>
                    <td>'.$account_details->billcity.'</td>
                </tr>
                <tr>
                    <td>'.$account_billstate.'</td>
                    <td>'.$account_details->billstate.'</td>
                </tr>
                <tr>
                    <td>'.$account_billcountry.'</td>
                    <td>'.$account_details->billcountry.'</td>
                </tr>
            </table>    
            </div>
        </div>
    ';
    $view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$account_shipping.'
    </div>
     <div class="table order-table">
    <table>
        <tr>
            <td style="width:25%;">'.$account_streetadress.'</td>
            <td>'.$account_details->streetadress.'</td>
        </tr>
        <tr>
            <td style="width:25%;">'.$account_postalcode.'</td>
            <td>'.$account_details->postalcode.'</td>
        </tr>
            <td></td>
            <td>'.$account_details->district.'</td>
        </tr>
        <tr>
            <td>'.$account_city.'</td>
            <td>'.$account_details->city.'</td>
        </tr>
        <tr>
            <td>'.$account_state.'</td>
            <td>'.$account_details->state.'</td>
        </tr>
        <tr>
            <td>'.$account_country.'</td>
            <td>'.$account_details->country.'</td>
        </tr>
        <tr>
            <td>'.$account_vatnumber.'</td>
            <td>'.$account_details->vatnumber.'</td>
        </tr>
        <tr>
            <td>'.$account_loghandlername.'</td>
            <td>'.$account_details->loghandlername.'</td>
        </tr>
        <tr>
            <td>'.$account_loghandleraccount.'</td>
            <td>'.$account_details->loghandleraccount.'</td>
        </tr>
    </table>    
    </div>
</div>
';
$view .= '<div class="content-block">
            <div class="block-header">
                <i class="fa-solid fa-bars fa-sm"></i>'.$view_asset_details.'
            </div>
             <div class="table order-table">
            <table>
                <tr>
                    <td style="width:25%;">'.$general_salesid.'</td>
                    <td>'.$account_hierarchy->salesid.'</td>
                </tr>
                <tr>
                    <td style="width:25%;">'.$general_soldto.'</td>
                    <td>'.$account_hierarchy->soldto.'</td>
                </tr>
                    <td>'.$general_created.'</td>
                    <td>'.getRelativeTime($responses->created).'</td>
                </tr>
                <tr>
                    <td>'.$general_createdby.'</td>
                    <td>'.$responses->createdby.'</td>
                </tr>
            </table>    
            </div>
        </div>
    ';

$soldto_id = explode("-",$account_hierarchy->soldto) ?? '';   

//DISPLAY RELATED COMMUNICATION RECORDS
$view_communication = '';
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){
    $view_communication = ' <a href="index.php?page=communications&partnerid='.$soldto_id[0].'" class="btn">'.$button_partner_assigned_communication.'</a>';
}

//DISPLAY RELATED USERS
$view_users ='';
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){
    $view_users = ' <a href="index.php?page=users&partnerid='.$soldto_id[0].'" class="btn">'.$button_partner_assigned_users.'</a>';
}

$healthindex ='';
if ($healthindex_allowed === 1){
    $healthindex = '<a href="index.php?page=report_healthindex&partnerid='.$soldto_id[0].'" class="btn">'.$view_asset_data_ranking.'</a>';
}

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$view_asset_actions.'
    </div>
    <a href="index.php?page=equipments&partnerid='.$soldto_id[0].'" class="btn">'.$button_partner_assigned_equipment.'</a>
    <a href="index.php?page=contracts&accountID='.$account_hierarchy->soldto.'" class="btn">'.$button_partner_assigned_contracts.'</a>
    '.$view_communication.'
    '.$view_users.'
    '.$healthindex.'
</div>';

//OUTPUT
echo $view;    

template_footer()

?>