<?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_dealer'] = $_SERVER['REQUEST_URI'];
$page = 'dealer';

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

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

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

//Translate status INT to STR
$dealer_status = 'general_status_'.$responses['status'];

// Handle success messages
if (isset($_GET['success_msg'])) {
    if ($_GET['success_msg'] == 1) {
        $success_msg = $message_eq_1;
    }
    if ($_GET['success_msg'] == 2) {
        $success_msg = $message_eq_2;
    }
    if ($_GET['success_msg'] == 3) {
        $success_msg = $message_eq_3;
    }
}

template_header('Dealer', 'dealer', 'view');
$view = '
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">'.($view_dealer_h2 ?? 'Dealer').' - '.encodeUuid($responses['rowID']).'</h2>
        <a href="index.php?page='.$_SESSION['origin'].'&p='.$_SESSION['p'].$_SESSION['search'].$_SESSION['partnerid'].'" class="btn alt mar-right-2">'.$button_cancel.'</a>
';

//------------------------------------
// EDIT BUTTON
//------------------------------------
if ($update_allowed === 1){
    $view .= '<a href="index.php?page=dealer_manage&rowID='.encodeUuid($responses['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_dealers_information ?? 'Dealer information').'
                </div>
                <div class="order-detail">
                    <h3>'.($dealers_status ?? 'status').'</h3>
                    <p><span class="status id'.$responses['status'].'">'.(${$dealer_status} ?? $dealer_status).'</span></p>
                </div>
                <div class="order-detail">
                    <h3>'.($dealers_name ?? 'name').'</h3>
                    <p>'.$responses['name'].'</p>
                </div>     
                <div class="order-detail">
                    <h3>'.($dealers_slug ?? 'slug').'</h3>
                    <p>'.$responses['dealer_slug'].'</p>
                </div>    
                <div class="order-detail">
                    <h3>'.($dealers_rating_overall ?? 'rating_overall').'</h3>
                    <p>'.$responses['rating_overall'].'</p>
                </div>
                <div class="order-detail">
                    <h3>'.($dealers_rating_website ?? 'rating_website').'</h3>
                    <p>'.$responses['rating_website'].'</p>
                </div>
            </div>
';

$view .='<div class="content-block order-details">
            <div class="block-header">
                <i class="fa-solid fa-user fa-sm"></i>
            </div>';
                if (!empty($responses['full_path'])){
                    $view .='  
                    <div class="order-detail"> 
                        <img style="border-radius: 4px;height: 200px;margin: auto;" src="'.$responses['full_path'].'" alt="">
                    </div>    
                        '; 
                }
$view .='                
         </div>
';

$view .= '</div>';
$view .= '<div class="content-block">
            <div class="block-header">
                <i class="fa-solid fa-bars fa-sm"></i>'.($view_dealer_details_1 ?? 'Descriptions').'
                <div class="tabs">
                    <a href="#" class="active">'.($dealers_short_description ?? 'Short').'</a>
                    <a href="#">'.($dealers_long_description ?? 'Long').'</a>
                    <a href="#">'.($dealers_usp1 ?? 'USP1').'</a>
                    <a href="#">'.($dealers_usp2 ?? 'USP2').'</a>
                    <a href="#">'.($dealers_usp3 ?? 'USP3').'</a>
                </div>
            </div>
            <div class="table order-table tab-content active">
             '.(${$responses['short_description']} ?? $responses['short_description']).'
            </div>
            <div class="table order-table tab-content">
             '.(${$responses['long_description']} ?? $responses['long_description']).'
            </div>
            <div class="table order-table tab-content">
             '.(${$responses['usp1']} ?? $responses['usp1']).'
            </div>
            <div class="table order-table tab-content">
             '.(${$responses['usp2']} ?? $responses['usp2']).'
            </div>
            <div class="table order-table tab-content">
             '.(${$responses['usp3']} ?? $responses['usp3']).'
            </div>
        </div>
    ';


$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.($view_dealer_details_3 ?? 'Location').'
    </div>
    <div class="table order-table">
        <table>
            <tr>
                <td style="width:25%;">'.($dealers_address ?? 'address').'</td>
                <td>'.$responses['address'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_postalcode ?? 'postalcode').'</td>
                <td>'.$responses['postalcode'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_city ?? 'city').'</td>
                <td>'.$responses['city'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_state ?? 'state').'</td>
                <td>'.$responses['state'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_country ?? 'country').'</td>
                <td>'.$responses['country'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_email ?? 'email').'</td>
                <td>'.$responses['email'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_phone ?? 'phone').'</td>
                <td>'.$responses['phone'].'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_url ?? 'url').'</td>
                <td>'.$responses['url'].'</td>
            </tr>
        </table>
    </div>
</div>
';    
//SHOW LOCATION BASED ON GEOLOCATION
if ((!empty($responses['lat']) || $responses['lat'] != '') && (!empty($responses['lng']) || $responses['lng'] != '')){

    $view .= '<div class="content-block">
                <div id="map" style="height:300px;z-index: 0;">
                </div>
                <script>
                // initialize Leaflet
                var map = L.map(\'map\').setView({lon: '.$responses['lng'].', lat: '.$responses['lat'].'}, 10);

                //Add TSS Avatar to MAP
                var TSSemp = L.icon({
                    iconUrl: \'./assets/images/marker.svg\',
                    iconSize:     [50, 50], // size of the icon
                });
                L.marker(['.$responses['lat'].','.$responses['lng'].'], {icon: TSSemp}).addTo(map);

                // add the OpenStreetMap tiles
                L.tileLayer(\'https://tile.openstreetmap.org/{z}/{x}/{y}.png\', {
                    maxZoom: 19,
                    attribution: \'&copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>\'
                }).addTo(map);

                // show the scale bar on the lower left corner
                L.control.scale({imperial: true, metric: true}).addTo(map);
                </script>
    </div>
    ';
    
}

//OPENING HOURS
if (!empty($responses['opening_hours']) || $responses['opening_hours'] !='' ){

    $opening_hours = is_string($responses['opening_hours']) ? json_decode($responses['opening_hours'],true) : '';

    $view .= '<div class="content-block">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>'.($dealers_openinghours ?? 'opening_hours').'
        </div>
        <div class="table order-table">
            <table>';
            foreach ($opening_hours as $day => $value){
                $view .= '<tr>
                            <td style="width:25%;">'.(${'general_day_'.$day} ?? 'Not specified').'</td>
                            <td>'.(isset($value['start']) ? $value['start'].' - '.$value['end']  : ($general_closed ?? 'Closed')).'</td>
                        </tr>';
            }
    $view .= '
            </table>
        </div>
    </div>
    ';
}

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.($view_dealer_details_2 ?? 'Settings').'
    </div>
    <div class="table order-table">
        <table>
            <tr>
                <td style="width:25%;">'.($dealers_garden_center ?? 'Garden_center').'</td>
                <td>'.($responses['garden_center'] == 0 ? $general_no : $general_yes).'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_brand_type ?? 'Brand_type').'</td>
                <td>'.${'brand_type_'.$responses['brand_type']}.'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_showroom_size ?? 'Showroom_size').'</td>
                <td>'.${'showroom_size_'.$responses['showroom_size']}.'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_focus_offering ?? 'focus_offering').'</td>
                <td>'.${'focus_offering_'.$responses['focus_offering']}.'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_type ?? 'dealer_type').'</td>
                <td>'.${'dealer_type_'.$responses['dealer_type']}.'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.($dealers_locations ?? 'dealer_locations').'</td>
                <td>'.($responses['locations'] == 0 ? $general_no : $general_yes).'</td>
            </tr>
        </table>
    </div>
</div>
';

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$tab3.'
    </div>
    <div class="table order-table">
        <table>
            <tr>
                <td style="width:25%;">'.$general_created.'</td>
                <td>'.getRelativeTime($responses['created']).'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.$general_updated.'</td>
                <td>'.getRelativeTime($responses['updated']).'</td>
            </tr>
        </table>
    </div>
</div>
';

//OUTPUT
echo $view;    

template_footer()

?>