<?php
defined(page_security_key) or exit;

$page = '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');

// Default input product values
$account = [
    'rowID' => '',
    'status' => 'Lead',
    'accountdetails' => [
        'billcompany' => '',
        'billfirstname' => '',
        'billlastname' => '',
        'billemail' => '',
        'billphonenumber' => '',
        'contactfirstname' => '',
        'contactlastname' => '',
        'contactemail' => '',
        'contactphonenumber' => '',
        'billstreetadress' => '',
        'billpostalcode' => '',
        'billdistrict' => '',
        'billcity' => '',
        'billstate' => '',
        'billcountry' => '',
        'streetadress' => '',
        'postalcode' => '',
        'district' => '',
        'city' => '',
        'state' => '',
        'country' => '',
        'vatnumber' => '',
        'loghandlername' => '',
        'loghandleraccount' => ''
    ],
    'created' => $date,
    'createdby' => $_SESSION['username'],
    'accounthierarchy' => [
        'salesid' => '',
        'soldto' => ''
        ]
];

//defaults
$accountdetails = json_decode(json_encode($account['accountdetails'],JSON_UNESCAPED_UNICODE));
$accounthierarchy = json_decode(json_encode($account['accounthierarchy'],JSON_UNESCAPED_UNICODE));

$rowID = $_GET['rowID'] ?? '';

if ($rowID !=''){
    $url = 'index.php?page=account&rowID='.$rowID.'';
} else {
    $url = 'index.php?page=accounts';
}

if (isset($_GET['rowID'])) {
    // ID param exists, edit an existing product
    //CALL TO API
    $api_url = '/v1/accounts/rowID='.$rowID;
    $responses = ioServer($api_url,'');
    //Decode Payload
    if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
    
    $account = json_decode(json_encode($responses[0]), true);
    $accountdetails = json_decode($account['accountdetails']);
    $accounthierarchy = json_decode($account['accounthierarchy']);
    
    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/accounts', $payload);
            if ($responses === 'NOK'){

            } else {
            header('Location: index.php?page=account&rowID='.$rowID.'&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/accounts', $payload);
            // Redirect and delete product
            if ($responses === 'NOK'){

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

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

template_header('Account', 'account', 'manage');

$view ='
<form action="" method="post">
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100"><?=$page?>'.$account_h2.'</h2>
        <a href="'.$url .'" 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 account?\')">';
}
if ($update_allowed === 1){
    $view .= '<input type="submit" name="submit" value="Save" class="btn">';
}
    
$view .= '</div>';

$view .= '<div class="tabs">
            <a href="#" class="active">'.$view_account_information.'</a>
            <a href="#">'.$view_account_contact.'</a>
            <a href="#">'.$account_billing.'</a>
            <a href="#">'.$account_shipping.'</a>
            <a href="#">'.$tab3.'</a>
        </div>
    ';
//Define Permission & Profile

$view .= '<div class="content-block tab-content active">
            <div class="form responsive-width-100">
                <label for="">'.$account_status.'</label>
                <select id="status" name="status">
                    <option value="'.$accountstatus_0.'" '.($account['status']==$accountstatus_0?' selected':'').'>'.$accountstatus_0.'</option>
                    <option value="'.$accountstatus_1.'" '.($account['status']==$accountstatus_1?' selected':'').'>'.$accountstatus_1.'</option>
                    <option value="'.$accountstatus_2.'" '.($account['status']==$accountstatus_2?' selected':'').'>'.$accountstatus_2.'</option>
                 </select>
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_name.'</label>
                <input name="accountdetails[billcompany]" type="text" value="'.$accountdetails->billcompany.'">
            </div>   
            <div class="form responsive-width-100">
                <label for="">'.$account_firstname.'</label>
                <input name="accountdetails[billfirstname]" type="text" value="'.$accountdetails->billfirstname.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_lastname.'</label>
                <input name="accountdetails[billlastname]" type="text" value="'.$accountdetails->billlastname.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_email.'</label>
                <input name="accountdetails[billemail]" type="text" value="'.$accountdetails->billemail.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_phone.'</label>
                <input name="accountdetails[billphonenumber]" type="text" value="'.$accountdetails->billphonenumber.'">
            </div>
        </div>';

$view .= '<div class="content-block tab-content">
 
            <div class="form responsive-width-100">
                <label for="">'.$account_contactfirstname.'</label>
                <input name="accountdetails[contactfirstname]" type="text" value="'.$accountdetails->contactfirstname.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_contactlastname.'</label>
                <input name="accountdetails[contactlastname]" type="text" value="'.$accountdetails->contactlastname.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_contactemail.'</label>
                <input name="accountdetails[contactemail]" type="text" value="'.$accountdetails->contactemail.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_contactphonenumber.'</label>
                <input name="accountdetails[contactphonenumber]" type="text" value="'.$accountdetails->contactphonenumber.'">
            </div>
</div>';

$view .= '<div class="content-block tab-content">
 
            <div class="form responsive-width-100">
                <label for="">'.$account_billstreetadress.'</label>
                <input name="accountdetails[billstreetadress]" type="text" value="'.$accountdetails->billstreetadress.'">
                <input name="accountdetails[billdistrict]" type="text" value="'.$accountdetails->billdistrict.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_billpostalcode.'</label>
                <input name="accountdetails[billpostalcode]" type="text" value="'.$accountdetails->billpostalcode.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_billcity.'</label>
                <input name="accountdetails[billcity]" type="text" value="'.$accountdetails->billcity.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_billstate.'</label>
                <input name="accountdetails[billstate]" type="text" value="'.$accountdetails->billstate.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_billcountry.'</label>
                <input name="accountdetails[billcountry]" type="text" value="'.$accountdetails->billcountry.'">
            </div>
        </div>';

$view .= '<div class="content-block tab-content">
            <div class="form responsive-width-100">
                <label for="">'.$account_streetadress.'</label>
                <input name="accountdetails[streetadress]" type="text" value="'.$accountdetails->streetadress.'">
                <input name="accountdetails[district]" type="text" value="'.$accountdetails->district.'">
            </div> 
            <div class="form responsive-width-100">
                <label for="">'.$account_postalcode.'</label>
                <input name="accountdetails[postalcode]" type="text" value="'.$accountdetails->postalcode.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_city.'</label>
                <input name="accountdetails[city]" type="text" value="'.$accountdetails->city.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_state.'</label>
                <input name="accountdetails[state]" type="text" value="'.$accountdetails->state.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_country.'</label>
                <input name="accountdetails[country]" type="text" value="'.$accountdetails->country.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_vatnumber.'</label>
                <input name="accountdetails[vatnumber]" type="text" value="'.$accountdetails->vatnumber.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_loghandlername.'</label>
                <input name="accountdetails[loghandlername]" type="text" value="'.$accountdetails->loghandlername.'">
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$account_loghandleraccount.'</label>
                <input name="accountdetails[loghandleraccount]" type="text" value="'.$accountdetails->loghandleraccount.'">
            </div>
    </div>';

//Dropdown

$partner_data = json_decode($_SESSION['partnerhierarchy']);
$soldto_dropdown = listPartner('soldto',$_SESSION['permission'],$accounthierarchy->soldto,'');

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


$view .= '</form>';


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