<?php
defined(page_security_key) or exit;

$page = 'communication';
//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 communication values
$communication = [
    'rowID' => '',
    'status' => 1,
    'partnerID' => '',
    'email' => '',
    'type_1' => 1,
    'type_2' => 1,
    'type_3' => 1,
    'created' => '',
    'createdby' => '',
    'salesID' => '',
    'coms_type' => 1,
    'send' => ''
];

if (isset($_GET['id'])) {
    // ID param exists, edit an existing communication
    //CALL TO API
    $api_url = '/v1/communications/rowID='.$_GET['id'];
    $responses = ioServer($api_url,'');
    //Decode Payload
    if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
    
    $communication = 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/communications', $payload);
            if ($responses === 'NOK'){

            } else {
            header('Location: index.php?page=communications&status=1&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/communications', $payload);
            if ($responses === 'NOK'){

            } else {
            // Redirect and delete communication
            header('Location: index.php?page=communications&status=1&success_msg=3');
            exit;
            }
        }
    }
   
} else {
    // Create a new communication
    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/communications', $payload);
        if ($responses === 'NOK'){

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

template_header('Communication', 'communication', 'manage');

$view ='
<form action="" method="post">
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">'.$communication_h2.'</h2>
        <a href="index.php?page=communications" 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 communication?\')">';
}
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="#">'.$tab3.'</a>
         </div>
    ';

//GET PARTNERID
$view_partners = '';
if(isset($communication['partnerID']) && $communication['partnerID'] !=''){
    $partner_ID = strtok($communication['partnerID'],'-');
    $view_partners = '
        <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 class="content-block tab-content active">
            <div class="form responsive-width-100">
                <label for="status">'.$communication_status.'</label>
                <select id="status" name="status">
                    <option value="1" '.($communication['status']==1?' selected':'').'>'.$coms_status_1 .'</option>
                    <option value="0" '.($communication['status']==0?' selected':'').'>'.$coms_status_0 .'</option>
                </select>
                <label for="parttype">'.$communication_type.'</label>
                <select id="status" name="coms_type">
                    <option value="0" '.($communication['coms_type']==0?' selected':'').'>'.$coms_type_0.'</option>
                    <option value="1" '.($communication['coms_type']==1?' selected':'').'>'.$coms_type_1.'</option>
                </select>
                <label for=""><i class="required">*</i>'.$communication_partner.'</label>
                <input id="name" type="text" name="partnerID" placeholder="'.$communication_partner.'" value="'.$communication['partnerID'].'" required>
                <label for=""><i class="required">*</i>'.$communication_email.'</label>
                <input id="name" type="text" name="email" placeholder="'.$communication_email.'" value="'.$communication['email'].'" required>
                <label for="">'.$communication_firmware.'</label>
                <select id="" name="type_1">
                    <option value="1" '.($communication['type_1']==1?' selected':'').'>'.$coms_status_1 .'</option>
                    <option value="0" '.($communication['type_1']==0?' selected':'').'>'.$coms_status_0 .'</option>
                </select>
                <label for="">'.$communication_service.'</label>
                <select id="" name="type_2">
                    <option value="1" '.($communication['type_2']==1?' selected':'').'>'.$coms_status_1 .'</option>
                    <option value="0" '.($communication['type_2']==0?' selected':'').'>'.$coms_status_0 .'</option>
                </select>
                <label for="">'.$communication_marketing.'</label>
                <select id="" name="type_3">
                    <option value="1" '.($communication['type_3']==1?' selected':'').'>'.$coms_status_1 .'</option>
                    <option value="0" '.($communication['type_3']==0?' selected':'').'>'.$coms_status_0 .'</option>
                </select>
                <input type="hidden" name="rowID" value="'.$communication['rowID'].'"> 
                '.$view_partners.'
            </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="'.$communication['created'].'" readonly>
            <label for="">'.$general_createdby.'</label>
            <input id="name" type="text" name="" placeholder="'.$general_createdby.'" value="'.$communication['createdby'].'" readonly>
            <label for="">'.$communication_send.'</label>
            <input id="name" type="text" name="" placeholder="'.$general_updated.'" value="'.$communication['send'].'" readonly>
        </div>
    </div>';
$view .= '</form>';

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