<head>
    <title>Invoice Lookup for Payment</title>
    <link rel="stylesheet" href="../style/style.css">

</head>
<?php
// echo($_SERVER['HTTP_REFERER']);
// ini_set('display_errors', 1);
// error_reporting(E_ALL);

$invoice_number = $_GET["inv_number"];
// $invoice_number = "INV00005756";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://hook.integromat.com/64t1hg8206toohee2r59lhshv6fdtwf1?invoice_number=" . $invoice_number);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch); 


if($return == "Failed") {
    die('Field');
}

$return = explode('|*|*|*|*|*|*|*', $return);
$invoice_details = json_decode($return[1], true);
$other_invoices = json_decode($return[0], true);
// var_dump($invoice_details);
// var_dump($other_invoices);



// Customer Profile Custom Fields Maping
$customer_custom_fields = array();
    
foreach($invoice_details[0]["customer_custom_fields"] as $field) {
    $customer_custom_fields[$field["placeholder"]] = $field["value"];
}
$customer_custom_fields["cf_moneris_profile_id"] = (isset($customer_custom_fields["cf_moneris_profile_id"])) ? $customer_custom_fields["cf_moneris_profile_id"] : "";
$customer_custom_fields["cf_moneris_card_details"] = (isset($customer_custom_fields["cf_moneris_card_details"])) ? $customer_custom_fields["cf_moneris_card_details"] : "";
$customer_custom_fields["cf_moneris_issuer_id"] = (isset($customer_custom_fields["cf_moneris_issuer_id"])) ? $customer_custom_fields["cf_moneris_issuer_id"] : "";
// Customer details
echo('
<h1>' . $invoice_details[0]["invoice_number"] . '</h1>

<div class="main_controls">
<form action="index.php" onclick="showDiv()" style="display: inline-block;">
    <input type="submit" value="New search">
</form>
<form action="search.php" onclick="showDiv()" style="display: inline-block;">
    <input type="hidden" value="' . $invoice_details[0]["invoice_number"] . '" name="inv_number">
    <input type="submit" value="Refresh">
</form>
<form action="https://team.helpmobility.ca/payment_processing/mon_res.php" onclick="showDiv()" style="display: inline-block;">
    <input type="hidden" name="invoice_button" value="">
    <input type="hidden" name="rvar_process_payment_on_invoice" value="true">
    <input type="hidden" name="rvar_customer_id" value="' . $invoice_details[0]["customer_id"] . '">
    <input type="hidden" name="rvar_invoice_id" value="' . $invoice_details[0]["invoice_id"] . '">
    <input type="hidden" name="rvar_invoice_number" value="' . $invoice_details[0]["invoice_number"] . '">
    <input type="hidden" name="moneris_profile" value="' . $customer_custom_fields["cf_moneris_profile_id"] . '">
    <input type="hidden" name="amount" value="' . $invoice_details[0]["balance"] . '">
    <input type="hidden" name="rvar_amount" value="' . $invoice_details[0]["balance"] . '">
    <input type="hidden" name="card" value="' . $customer_custom_fields["cf_moneris_card_details"] . '">
    <input type="hidden" name="issuerID" value="' . $customer_custom_fields["cf_moneris_issuer_id"] . '">
    <input type="submit" value="Process Payment">
</form>
</div>
<div class="row">
<div class="info_box" style="display: inline-block; width: calc(100% / 3 - 32px);">
    <p class="title">Customer Information</p>
    <p>' . $invoice_details[0]["contact_name"] . ' (Type: ' . $invoice_details[0]["contact_type"] . ')</p>
    <p>Email: ' . $invoice_details[0]["email"] . '</p>
    <p>Phone: ' . $invoice_details[0]["phone"] . ' Mobile: ' . $invoice_details[0]["mobile"] . '</p>
</div>
');

// Billing info.
echo('
<div class="info_box" style="display: inline-block; width: calc(100% / 3 - 32px);">
    <p class="title">Billing Address</p>
    <p>' . $invoice_details[0]["billing_address"]["street"] . '</p>
    <p>' . $invoice_details[0]["billing_address"]["city"] . ', ' . $invoice_details[0]["billing_address"]["state"] . '</p>
    <p>' . $invoice_details[0]["billing_address"]["zip"] . '</p>
    
</div>
');


// Shipping info.
echo('
<div class="info_box" style="display: inline-block; width: calc(100% / 3 - 22px); margin: 0;">
    <p class="title">Shipping Address</p>
    <p>' . $invoice_details[0]["shipping_address"]["street"] . '</p>
    <p>' . $invoice_details[0]["shipping_address"]["city"] . ', ' . $invoice_details[0]["shipping_address"]["state"] . '</p>
    <p>' . $invoice_details[0]["shipping_address"]["zip"] . '</p>
    
</div>

</div> 
');
// ini_set('display_startup_errors', 1);

// Other info
echo('
<div class="row">
<div  class="info_box"  style="display: inline-block; width: calc(100% / 2 - 32px);">
    <p class="title">Delivery Management</p>

    ');
    // Invoice Custom Fields Mapping
$custom_fields = array();
    
foreach($invoice_details[0]["custom_fields"] as $field) {
    $custom_fields[$field["placeholder"]] = $field["value"];
    
    // Skip fields
    $to_show_ids = array('2212827000000204031', '2212827000000069182', '2212827000000113259', '2212827000000207786');
    if(!in_array($field["customfield_id"], $to_show_ids)) { continue; }
    
    echo('<p>' . $field["label"] . ': ' . $field["value_formatted"] . '</p>');
    // echo('<p>' . $field["label"] . ': <input value="' . $field["value"] . '" name="' . $field["customfield_id"] . '"</p>');
}
    
echo('<br><b>Pickup requests</b><br>');
// var_dump($custom_fields);
if(isset($custom_fields["cf_rental_is_ended"]) && $custom_fields["cf_rental_is_ended"] == 'true') {
    echo('<span style="color: #e91e63">Rental has ended on this invoice. Pickup cannot be scheduled.</span>');
}

else if(isset($custom_fields["cf_delivery_type"]) && $custom_fields["cf_delivery_type"] == 'Pickup' && isset($custom_fields["cf_delivery_date"])) {
    echo('<span style="color: #e91e63">Pickup has been requested. Pickup date has not been determined yet.</span>');
}

else{
    echo('
<form name="request_pickup" action="https://flow.zoho.com/715021179/flow/webhook/incoming" method="get" target="_blank">
    <input type="hidden" name="inv_id" value="' . $invoice_details[0]["invoice_id"] . '">
    <input type="hidden" name="zapikey" value="1001.e26fd917ea44d642a87306f01a97a7e6.46b4bd509e9f85f11ddd89cc6fd155a4">
    <input type="hidden" name="isdebug" value="false">
    Notes: <input value="" name="request_pickup_notes">
    <input type="submit" name="inv_request_pickup" value="Request Pickup">
');
}

echo('</div>');

echo('
<div class="items info_box" style="display: inline-block; width: calc(100% / 2 - 32px);">
<h3>ITEMS</h3>
<table>
    <tr>
        <th>Name & Description</th>
        <th>Quantity</th>
    </tr>
');
foreach($invoice_details[0]["items"] as $item) {
    echo('
    <tr>
        <td>' . $item["name"] . ': ' . $item["description"] . '</td>
        <td>' . $item["quantity"] . '</td>
    </tr>
    ');
}

echo('
</table>
</div>

</div>
');


echo('
<div class="other_invoices info_box" style="width: calc(100% - 20px); padding: 5px 10px; margin: 10px 10px 10px 0;">
<h3>OTHER INVOICES</h3>
<table>
    <tr>
        <th>Invoice number</th>
        <th>Date</th>
        <th>Status</th>
        <th>Sales person</th>
        <th>Created by</th>
    </tr>
');
foreach($other_invoices as $invoice) {
    echo('
    <tr>
        <td><a  onclick="showDiv()" href="search.php?inv_number=' . $invoice["invoice_number"] . '">' . $invoice["invoice_number"] . '</a></td>
        <td>' . $invoice["date"] . '</td>
        <td>' . $invoice["status"] . '</td>
        <td>' . $invoice["salesperson_name"] . '</td>
        <td>' . $invoice["created_by"] . '</td>
    </tr>
    ');
}

echo('
</table>

</div>
');
?>
<script>
    function showDiv() {
      document.getElementById('loadingDIV').style.display = "flex";
       
    }
</script>
<div id="loadingDIV" style="background-color: white; position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; display: none; justify-content: center; align-items: center; font-size: 1.5em;">
    <p>Loading, please wait...</p>
</div>