From b10bf1eed809f439c4b520592277378ec8c46b0f Mon Sep 17 00:00:00 2001
From: Erik Thuning <boooink@gmail.com>
Date: Wed, 11 Jan 2023 11:11:42 +0100
Subject: [PATCH] Added ability to check out a product from the product page
 directly

---
 html/fragments.html     | 42 ++++++++++++++++++++++++++++++++++++++---
 include/ProductPage.php | 11 ++++++++---
 2 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/html/fragments.html b/html/fragments.html
index 761a059..3fade23 100644
--- a/html/fragments.html
+++ b/html/fragments.html
@@ -306,16 +306,52 @@
     <button>Ladda upp</button>
   </form>
 </div>
-<div id="product-history"
-     class="¤hidden¤">
+<div id="product-history">
   <h2>Artikelhistorik</h2>
   ¤history¤
 </div>
 <div id="product-label"
-     class="¤hidden¤">
+     class="¤label_hidden¤">
   <h2>Etikett</h2>
   ¤label¤
 </div>
+<div id="product-direct-checkout" class="¤checkout_hidden¤">
+  <h2>Låna ut</h2>
+  <form class="light"
+        onSubmit="JavaScript:checkoutProduct(event)">
+    <datalist id="user_suggest"></datalist>
+    <input type="hidden"
+           name="page"
+           value="checkout" />
+    <label for="user">Användarnamn:</label>
+    <input onFocus="JavaScript:suggest(this, 'user')"
+           type="text"
+           name="user"
+           list="user_suggest"
+           autocomplete="off"
+           placeholder="Användarnamn"
+           required />
+    <input type="hidden"
+           id="product"
+           name="product"
+           value="¤serial¤" />
+    <button>
+      Låna ut
+    </button>
+    <br/>
+    <label>Löptid:</label>
+    <button onClick="JavaScript:loanLength(event, 7, 'day')">1 vecka</button>
+    <button onClick="JavaScript:loanLength(event, 1, 'year')">1 år</button>
+    <button onClick="JavaScript:loanLength(event, 3, 'year')">3 år</button>
+    <br/>
+    <label for="end">Slutdatum:</label>
+    <input type="text"
+           id="end"
+           onClick="JavaScript:calendar(event)"
+           name="end"
+           value="¤end¤" />
+  </form>
+</div>
 
 ¤¤ attachment_list ¤¤
 <ul class="attachment-list">
diff --git a/include/ProductPage.php b/include/ProductPage.php
index afb25b1..b0fefb4 100644
--- a/include/ProductPage.php
+++ b/include/ProductPage.php
@@ -67,19 +67,24 @@ class ProductPage extends Page {
                         'tags'        => $tags,
                         'info'        => $info,
                         'label'       => '',
-                        'hidden'      => 'hidden',
+                        'label_hidden' => 'hidden',
+                        'checkout_hidden'      => 'hidden',
                         'service'     => 'Starta service',
                         'history'     => $history,
-                        'attachments' => $attachments);
+                        'attachments' => $attachments,
+                        'end'         => format_date(default_loan_end(time())));
         if(class_exists('QRcode')) {
             $fields['label'] = replace($fields,
                                        $this->fragments['product_label']);
         }
         if(!$this->product->get_discardtime()) {
-            $fields['hidden'] = '';
+            $fields['label_hidden'] = '';
             if($this->product->get_status() == 'service') {
                 $fields['service'] = 'Avsluta service';
             }
+            if($this->product->get_status() == 'available') {
+                $fields['checkout_hidden'] = '';
+            }
         }
         return replace($fields, $this->fragments['product_form']);
     }