<?php
defined(page_security_key) or exit;

//SET ORIGIN FOR NAVIGATION
$_SESSION['prev_origin'] = $_SERVER['REQUEST_URI'];
$page = $_SESSION['origin'] ='partner';

//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');

// Default input product values
$partner = [
    'partnerID' => '',
    'partnertype' => '',
    'partnername' => '',
    'partneradress' => '',
    'partnernotes' => '',
    'salesID' => '',
    'status' => 1,
    'created' => '',
    'createdby' => ''
];

$partner_ID = $_GET['id'] ?? '';

if ($partner_ID !=''){
    $url = 'index.php?page=partners&id='.$partner_ID.'';
} else {
    $url = 'index.php?page=partners';
}

if (isset($_GET['id'])) {
    // ID param exists, edit an existing product
    //CALL TO API
    $api_url = '/v1/partners/partnerID='.$partner_ID;
    $responses = ioServer($api_url,'');
    //Decode Payload
    if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
    
    $partner = json_decode(json_encode($responses[0]), true);

    if ($update_allowed === 1){
        if (isset($_POST['submit'])) {
            //GET ALL POST DATA
            $data = json_encode($_POST, JSON_UNESCAPED_UNICODE);
            //Secure data
            $payload = generate_payload($data);
            
            //API call
            $responses = ioServer('/v1/partners', $payload);
            if ($responses === 'NOK'){

            } else {
            header('Location: index.php?page=partners&success_msg=2');
            exit;
            }
        }
    }
    
    if ($delete_allowed === 1){
        if (isset($_POST['delete'])) {
            //GET ALL POST DATA
            $data = json_encode($_POST , JSON_UNESCAPED_UNICODE);
            //Secure data
            $payload = generate_payload($data);
            //API call
            $responses = ioServer('/v1/partners', $payload);
            // Redirect and delete product
            if ($responses === 'NOK'){

            } else {
            header('Location: index.php?page=partners&success_msg=3');
            exit;
            }
        }
    }
   
} else {
    // Create a new product
    if (isset($_POST['submit']) && $create_allowed === 1) {
        //GET ALL POST DATA
        $data = json_encode($_POST, JSON_UNESCAPED_UNICODE);
        //Secure data
        $payload = generate_payload($data);
        //API call
        $responses = ioServer('/v1/partners', $payload);
        if ($responses === 'NOK'){

        } else {
        header('Location: index.php?page=partners&success_msg=1');
        exit;
        }
    }
}

template_header('Partner', 'partner', 'manage');

$view ='
<form action="" method="post">
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">'.$partner_h2.'</h2>
        <a href="index.php?page=partners" class="btn alt mar-right-2">'.$button_cancel.'</a>
';

if ($delete_allowed === 1){
    $view .= '<input type="submit" name="delete" value="Delete" class="btn red mar-right-2" onclick="return confirm(\'Are you sure you want to delete this partner?\')">';
}
if ($update_allowed === 1){
    $view .= '<input type="submit" name="submit" value="Save" class="btn">';
}
   
$view .= '</div>';

$view .= '<div class="tabs">
            <a href="#" class="active">'.$tab1 .'</a>
            <a href="#">'.$tab2.'</a>
            <a href="#">'.$tab3.'</a>
        </div>
    ';

//Define Service and partner enabled
$view .= '<div class="content-block tab-content active">
            <div class="form responsive-width-100">
                <label for="status">'.$partner_status.'</label>
                <select id="status" name="status">
                    <option value="1" '.(($partner['status']==1 )?' selected':'').'>'.$enabled .'</option>
                    <option value="0" '.(($partner['status']==0 )?' selected':'').'>'.$disabled .'</option>
                </select>
                <label for="partnertype">'.$partner_partnertype.'</label>
                <select id="partnertype" name="partnertype" required>
                ';
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4 ){
$view .= '          <option value="'.$partnertype1.'" '.($partner['partnertype']== $partnertype1?' selected':'').'>'.$partnertype1.'</option>
                    <option value="'.$partnertype2.'" '.($partner['partnertype']== $partnertype2?' selected':'').'>'.$partnertype2.'</option>';
}
$view .= '          
                    <option value="'.$partnertype3.'" '.($partner['partnertype']== $partnertype3?' selected':'').'>'.$partnertype3.'</option>
                    <option value="'.$partnertype4.'" '.($partner['partnertype']== $partnertype4?' selected':'').'>'.$partnertype4.'</option>
                </select>
                <label for="partnername">'.$partner_partnername.'</label>
                <input id="name" type="text" name="partnername" placeholder="'.$partner_partnername.'" value="'.$partner['partnername'].'" required">
                <label for="partnernotes">'.$partner_note.'</label>
                <textarea id="partnernotes" name="partnernotes" placeholder="'.$partner_note.'">'.$partner['partnernotes'].'</textarea>
                <input type="hidden" name="partnerID" value="'.$partner_ID.'">
                <label for="">'.$partner_view_assigned_assets.'</label>
                <a href="index.php?page=equipments&partnerid='.$partner_ID.'" class="btn" style="width: 15%;">'.$button_view.'</a>
        ';
               
$view .= '</div>
        </div>';

//GET PARTNERDATA
$partner_data = json_decode($partner['salesID'])?? json_decode($_SESSION['partnerhierarchy']) ;
//BUID UP DROPDOWNS
$salesid_dropdown = listPartner('salesid',$_SESSION['permission'],$partner_data->salesid,'');
$soldto_dropdown = listPartner('soldto',$_SESSION['permission'],$partner_data->soldto,'');

//DISPLAY
$view .= '<div class="content-block tab-content">
            <div class="form responsive-width-100">
';
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){
    $view .= '<label for="status">'.$general_salesid.'</label>';
    $view .= $salesid_dropdown;   
}
    $view .= '<label for="status">'.$general_soldto.'</label>';
    $view .= $soldto_dropdown;
    $view .= '
        <input type="hidden" name="old_salesid" value="'.$partner_data->salesid.'">   
        <input type="hidden" name="old_soldto" value="'.$partner_data->soldto.'">   
    </div>
</div>';

$view .= '<div class="content-block tab-content">
        <div class="form responsive-width-100">
            <label for="">'.$general_created.'</label>
            <input id="name" type="text" name="" placeholder="'.$general_created.'" value="'.$partner['created'].'" readonly>
            <label for="">'.$general_createdby.'</label>
            <input id="name" type="text" name="" placeholder="'.$general_createdby.'" value="'.$partner['createdby'].'" readonly>
        </div>
    </div>';


$view .= '</form>';


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