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

//SET ORIGIN FOR NAVIGATION
$prev_page = $_SESSION['prev_origin'] ?? '';
$page = 'product';

//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;
}

//GET PARAMETERS && STORE in SESSION for FURTHER USE/NAVIGATION
$pagination_page = $_SESSION['p'] = isset($_GET['p']) ? $_GET['p'] : 1;

//PAGE Security
$page_manage = 'product_manage';
$update_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'U');
$update_allowed_edit = 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 = '/v1/products/'.$GET_VALUES;
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
$responses = $responses[0];

//CALL TO API FOR Product_versions
$api_url = '/v1/products_versions/productrowid='.$_GET['rowID'];
$product_versions = ioServer($api_url,'');
//Decode Payload
if (!empty($product_versions)){$product_versions = decode_payload($product_versions);}else{$product_versions = null;}

//------------------------------
//Variables
//------------------------------
$status_text = 'prod_status_'.$responses->status ?? '';
$product_category_text = 'product_category'.$responses->product_category ?? '';
$parttype_text = 'part_type'.$responses->parttype ?? '';

// Handle success messages
if (isset($_GET['success_msg'])) {
    if ($_GET['success_msg'] == 1) {
        $success_msg = $message_pr_1;
    }
    if ($_GET['success_msg'] == 2) {
        $success_msg = $message_pr_2;
    }
    if ($_GET['success_msg'] == 3) {
        $success_msg = $message_pr_3;
    }
}

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


//------------------------------------
//
//------------------------------------
if ($update_allowed_edit === 1){
    $view .= '<a href="index.php?page=product_manage&id='.$_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>'.($product_data ?? '').'
                </div>
                <div class="order-detail">
                    <h3>'.$product_status.'</h3>
                    <p><span class="status id'.$responses->status.'">'.$$status_text.'</span></p>
                </div>
                <div class="order-detail">
                    <h3>'.$product_category.'</h3>
                    <p><span class="status id'.$responses->product_category.'">'.$$product_category_text.'</span></p>
                </div>
                 <div class="order-detail">
                    <h3>'.$product_parttype.'</h3>
                    <p><span class="status id'.$responses->parttype.'">'.$$parttype_text.'</span></p>
                </div>
                <div class="order-detail">
                    <h3>'.$product_code.'</h3>
                    <p>'.$responses->productcode.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$product_name.'</h3>
                    <p>'.$responses->productname.'</p>
                </div>';
   
$view .='                
            </div>
';

$view .='<div class="content-block order-details">
            <div class="block-header">
                <i class="fa-solid fa-user fa-sm"></i>
            </div>';
                    $picture = glob("./assets/images/products/".$responses->productcode.".{jpg,jpeg,png,gif}", GLOB_BRACE);
                if (!empty($picture)){
                    $view .='  
                    <div class="order-detail"> 
                        <img style="border-radius: 4px;height: 200px;margin: auto;" src="'.$picture[0].'" alt="">
                    </div>    
                        '; 
                }
$view .='                
         </div>
';
$view .= '</div>';
$view .= '<div class="content-block">
            <div class="block-header">
                <i class="fa-solid fa-bars fa-sm"></i>'.$product_description.'
            </div>
             <div class="table order-table">
           '.$responses->productdescription.'
            </div>
        </div>
    ';



$view .= '<div class="content-block">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>'.($product_version ?? '').'
            <a href="index.php?page=products_versions&productrowid='.$_GET['rowID'].'" class="btn2"> + </a>
        </div>';
                if (!empty($product_versions)){
                    $view .= '   
                    <div class="table">
                        <table class="sortable">
                            <thead>
                                <tr>
                                    <th>'.$product_version_number.'</th>
                                    <th>'.$product_version_version.'</th>
                                    <th>'.$product_version_software .'</th>
                                    <th>'.$general_actions.'</th>
                                </tr>
                            </thead>
                            <tbody>';
                    foreach ($product_versions as $version){
                      $view .= '<tr>
                                    <td>'.$version->rowID.'</td>
                                    <td>'.$version->version.'</td>
                                    <td>'.$version->software.'</td>
                                    <td><a href="index.php?page=products_versions&productrowid='.$_GET['rowID'].'&rowID='.$version->rowID.'" class="btn_link">'.$general_view.'</a></td>
                                </tr>';
                    }
                    $view .= '
                            </tbody>
                        </table>
                    </div>
                    ';
                }

$view .= '
    </div>
';

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$tab4.'
    </div>
    <div class="table order-table">
        <table>
            <tr>
                <td style="width:25%;">'.$product_serialized.'</td>
                <td>'.(($responses->sn == 1)? $enabled : $disabled).'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.$product_sales.'</td>
                <td>'.(($responses->salesflag == 1)? $enabled : $disabled).'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.$product_build.'</td>
                <td>'.(($responses->build == 1)? $enabled : $disabled).'</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>'.$responses->created.'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.$general_createdby.'</td>
                <td>'.$responses->createdby.'</td>
            </tr>
                <tr>
                <td style="width:25%;">'.$general_updated.'</td>
                <td>'.$responses->updated.'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.$general_updatedby.'</td>
                <td>'.$responses->updatedby.'</td>
            </tr>
        </table>
    </div>
</div>
';

$view .='</div>';

//OUTPUT
echo $view;    

template_footer()

?>