From 65377952fbd0721ce1550c42384d723ccd7b9b1a Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Fri, 1 Mar 2024 09:20:11 +0700
Subject: generate recomendation

---
 .../components/products-recomendatison.jsx         | 65 ++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 src/pages/my/recomendation/components/products-recomendatison.jsx

(limited to 'src/pages/my/recomendation/components/products-recomendatison.jsx')

diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx
new file mode 100644
index 00000000..9438eb46
--- /dev/null
+++ b/src/pages/my/recomendation/components/products-recomendatison.jsx
@@ -0,0 +1,65 @@
+import Menu from '@/lib/auth/components/Menu';
+import { cons } from 'lodash-contrib';
+
+const ProductsRecomendation = ({ id }) => {
+  const handleSubmit = () => {}
+  const handleFileChange = () => {}
+  return (
+    <div className='container mx-auto flex py-10'>
+      <div className='w-3/12 pr-4'>
+        <Menu />
+      </div>
+      <div className='w-9/12 p-4 bg-white border border-gray_r-6 rounded'>
+        <div className='flex mb-6 items-center justify-between'>
+          <h1 className='text-title-sm font-semibold'>
+            Generate Recomendation
+          </h1>
+        </div>
+        <div className='group'>
+          <h1 className='text-sm  font-semibold'>Contoh Excel</h1>
+          <table className='table-data'>
+            <thead>
+              <tr>
+                <th>Product</th>
+                <th>Qty</th>
+              </tr>
+            </thead>
+            <tbody>
+              <tr>
+                <td>Tekiro Long Nose Pliers Tang Lancip</td>
+                <td>10</td>
+              </tr>
+            </tbody>
+          </table>
+        </div>
+        <div className='container mx-auto mt-8'>
+          <form onSubmit={handleSubmit} >
+            <div className='mb-4'>
+              <label
+                htmlFor='excelFile'
+                className='text-sm  font-semibold'
+              >
+                Upload Excel File (.xlsx)
+              </label>
+              <input
+                type='file'
+                id='excelFile'
+                accept='.xlsx'
+                onChange={handleFileChange}
+                className='mt-1 p-2 block w-full border border-gray-300 rounded-md focus:outline-none focus:border-blue-500'
+              />
+            </div>
+            <button
+              type='submit'
+              className='bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600'
+            >
+              Generate
+            </button>
+          </form>
+        </div>
+      </div>
+    </div>
+  );
+};
+
+export default ProductsRecomendation;
-- 
cgit v1.2.3


From d0d38a82bb3d368632243ffb1cd317bb41cb13a3 Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Wed, 6 Mar 2024 11:01:09 +0700
Subject: import excel

---
 .../components/products-recomendatison.jsx         | 36 +++++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

(limited to 'src/pages/my/recomendation/components/products-recomendatison.jsx')

diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx
index 9438eb46..c20aaa5b 100644
--- a/src/pages/my/recomendation/components/products-recomendatison.jsx
+++ b/src/pages/my/recomendation/components/products-recomendatison.jsx
@@ -1,9 +1,32 @@
 import Menu from '@/lib/auth/components/Menu';
-import { cons } from 'lodash-contrib';
+import { useState } from 'react';
+import * as XLSX from 'xlsx'; 
 
 const ProductsRecomendation = ({ id }) => {
-  const handleSubmit = () => {}
-  const handleFileChange = () => {}
+  const [excelData, setExcelData] = useState(null);
+
+  const handleSubmit = (e) => {
+    e.preventDefault();
+    if (excelData) {
+      // Lakukan operasi pencarian atau operasi lainnya di sini
+      console.log('ini data excel',excelData); // Contoh: Menampilkan data excel ke konsol
+    } else {
+      console.log('No excel data available');
+    }
+  };
+  const handleFileChange = (e) => {
+    const file = e.target.files[0];
+    const reader = new FileReader();
+    reader.onload = (event) => {
+      const data = new Uint8Array(event.target.result);
+      const workbook = XLSX.read(data, { type: 'array' });
+      const sheetName = workbook.SheetNames[0];
+      const sheet = workbook.Sheets[sheetName];
+      const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1, range: 1 });
+      setExcelData(jsonData);
+    };
+    reader.readAsArrayBuffer(file);
+  };
   return (
     <div className='container mx-auto flex py-10'>
       <div className='w-3/12 pr-4'>
@@ -33,12 +56,9 @@ const ProductsRecomendation = ({ id }) => {
           </table>
         </div>
         <div className='container mx-auto mt-8'>
-          <form onSubmit={handleSubmit} >
+          <form onSubmit={handleSubmit}>
             <div className='mb-4'>
-              <label
-                htmlFor='excelFile'
-                className='text-sm  font-semibold'
-              >
+              <label htmlFor='excelFile' className='text-sm  font-semibold'>
                 Upload Excel File (.xlsx)
               </label>
               <input
-- 
cgit v1.2.3


From 60497cfbdf29df106d3d4270bfd480f270f16098 Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Mon, 25 Mar 2024 16:52:42 +0700
Subject: generate recomendation

---
 .../components/products-recomendatison.jsx         | 278 ++++++++++++++++++---
 1 file changed, 247 insertions(+), 31 deletions(-)

(limited to 'src/pages/my/recomendation/components/products-recomendatison.jsx')

diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx
index c20aaa5b..72b858e8 100644
--- a/src/pages/my/recomendation/components/products-recomendatison.jsx
+++ b/src/pages/my/recomendation/components/products-recomendatison.jsx
@@ -1,19 +1,79 @@
 import Menu from '@/lib/auth/components/Menu';
-import { useState } from 'react';
-import * as XLSX from 'xlsx'; 
+import { useEffect, useState } from 'react';
+import * as XLSX from 'xlsx';
+import GenerateRecomendations from '../api/recomendation';
+import axios from 'axios';
+import { Button, Link } from '@chakra-ui/react';
+import Image from 'next/image';
+import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
+import formatCurrency from '~/libs/formatCurrency';
 
 const ProductsRecomendation = ({ id }) => {
   const [excelData, setExcelData] = useState(null);
+  const [products, setProducts] = useState([]);
+  const [isLoading, setIsLoading] = useState(false);
+  const [isOpen, setIsOpen] = useState(false);
+  const [variantsOpen, setVariantsOpen] = useState([]);
 
-  const handleSubmit = (e) => {
+  const mappingProducts = async ({ index, product, result, variants }) => {
+    const resultMapping = {
+      index : index,
+      product: product,
+      result: {
+        id: result?.id || '-',
+        name: result?.displayName || '-',
+        code: result?.code || '-',
+        image: result?.image || '-',
+        price: result?.lowestPrice.priceDiscount || '-',
+        manufacture: result?.manufacture.name || '-',
+        variantTotal: result?.variantTotal || '-',
+        variants: variants || '-',
+      },
+    };
+
+    return resultMapping;
+  };
+
+  const searchRecomendation = async ({ product, index }) => {
+    let variants = [];
+    const searchProduct = await axios.post(
+      `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}`
+    );
+
+    const result =
+      searchProduct.data.response.numFound > 0
+        ? searchProduct.data.response.products[0]
+        : null;
+
+    if (result?.variantTotal > 1) {
+      const searchVariants = await axios.post(
+        `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${result.id}`
+      );
+      variants = searchVariants.data[0].variants;
+    }
+
+    const resultMapping = await mappingProducts({ index, product, result, variants });
+
+    return resultMapping;
+  };
+
+  const handleSubmit = async (e) => {
+    setIsLoading(true);
     e.preventDefault();
     if (excelData) {
-      // Lakukan operasi pencarian atau operasi lainnya di sini
-      console.log('ini data excel',excelData); // Contoh: Menampilkan data excel ke konsol
+      const results = await Promise.all(
+        excelData.map(async (row, i) => {
+          const index = i + 1;
+          const product = row[0];
+          return await searchRecomendation({ product, index });
+        })
+      ).then((results) => setProducts(results));
+      setIsLoading(false);
     } else {
       console.log('No excel data available');
     }
   };
+
   const handleFileChange = (e) => {
     const file = e.target.files[0];
     const reader = new FileReader();
@@ -27,36 +87,107 @@ const ProductsRecomendation = ({ id }) => {
     };
     reader.readAsArrayBuffer(file);
   };
+
+  const handleVariantsOpen = ({ variants }) => {
+    setVariantsOpen(variants);
+    setIsOpen(true);
+  };
+  const hadnliChooseVariants = ({ id, variant }) => {
+    let foundIndex = products.findIndex((item) => item.result.id === id);
+    if (foundIndex !== -1) {
+      products[foundIndex].result.code = variant?.code;
+      products[foundIndex].result.name = variant?.name;
+    } else {
+      console.log('Data not found.');
+    }
+    setIsOpen(false);
+  };
   return (
-    <div className='container mx-auto flex py-10'>
-      <div className='w-3/12 pr-4'>
-        <Menu />
-      </div>
-      <div className='w-9/12 p-4 bg-white border border-gray_r-6 rounded'>
-        <div className='flex mb-6 items-center justify-between'>
-          <h1 className='text-title-sm font-semibold'>
-            Generate Recomendation
-          </h1>
-        </div>
-        <div className='group'>
-          <h1 className='text-sm  font-semibold'>Contoh Excel</h1>
+    <>
+      <BottomPopup
+        active={isOpen}
+        close={() => setIsOpen(false)}
+        className='w-full md:!w-[60%]'
+        title='List Variants'
+      >
+        <div className='container'>
           <table className='table-data'>
             <thead>
               <tr>
-                <th>Product</th>
-                <th>Qty</th>
+                <th>Part Number</th>
+                <th>Variants </th>
+                <th>Harga </th>
+                <th></th>
               </tr>
             </thead>
             <tbody>
-              <tr>
-                <td>Tekiro Long Nose Pliers Tang Lancip</td>
-                <td>10</td>
-              </tr>
+              {variantsOpen?.map((variant, index) => (
+                <tr key={index}>
+                  <td>{variant.code}</td>
+                  <td>{variant.attributes.join(', ') || '-'}</td>
+                  <td>
+                    {variant.price.discountPercentage > 0 && (
+                      <div className='flex items-center gap-x-1'>
+                        <div className={style['disc-badge']}>
+                          {Math.floor(variant.price.discountPercentage)}%
+                        </div>
+                        <div className={style['disc-price']}>
+                          Rp {formatCurrency(variant.price.price)}
+                        </div>
+                      </div>
+                    )}
+                    {variant.price.priceDiscount > 0 &&
+                      `Rp ${formatCurrency(variant.price.priceDiscount)}`}
+                    {variant.price.priceDiscount === 0 && '-'}
+                  </td>
+                  <td>
+                    <Button
+                      size='sm'
+                      w='100%'
+                      onClick={() =>
+                        hadnliChooseVariants({
+                          id: variant.parent.id,
+                          variant: variant,
+                        })
+                      }
+                    >
+                      Pilih
+                    </Button>
+                  </td>
+                </tr>
+              ))}
             </tbody>
           </table>
         </div>
-        <div className='container mx-auto mt-8'>
-          <form onSubmit={handleSubmit}>
+      </BottomPopup>
+      <div className='container mx-auto flex py-10'>
+        <div className='w-3/12 pr-4'>
+          <Menu />
+        </div>
+        <div className='w-9/12 p-4 bg-white border border-gray_r-6 rounded'>
+          <div className='flex mb-6 items-center justify-between'>
+            <h1 className='text-title-sm font-semibold'>
+              Generate Recomendation
+            </h1>
+          </div>
+          <div className='group'>
+            <h1 className='text-sm  font-semibold'>Contoh Excel</h1>
+            <table className='table-data'>
+              <thead>
+                <tr>
+                  <th>Product</th>
+                  <th>Qty</th>
+                </tr>
+              </thead>
+              <tbody>
+                <tr>
+                  <td>Tekiro Long Nose Pliers Tang Lancip</td>
+                  <td>10</td>
+                </tr>
+              </tbody>
+            </table>
+          </div>
+          <div className='container mx-auto mt-8'>
             <div className='mb-4'>
               <label htmlFor='excelFile' className='text-sm  font-semibold'>
                 Upload Excel File (.xlsx)
@@ -69,16 +200,101 @@ const ProductsRecomendation = ({ id }) => {
                 className='mt-1 p-2 block w-full border border-gray-300 rounded-md focus:outline-none focus:border-blue-500'
               />
             </div>
-            <button
-              type='submit'
-              className='bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600'
+            <Button
+              colorScheme='red'
+              w='l'
+              isDisabled={isLoading}
+              onClick={handleSubmit}
             >
               Generate
-            </button>
-          </form>
+            </Button>
+          </div>
+          <div className='grup mt-8'>
+            {products && products.length > 0 && (
+              <div className='group'>
+                <table className='table-data'>
+                  <thead>
+                    <tr>
+                      <th>Product</th>
+                      <th>Item code</th>
+                      <th>Keterangan</th>
+                      <th>Brand</th>
+                      <th>Harga</th>
+                      <th>Image</th>
+                      <th>lainnya</th>
+                    </tr>
+                  </thead>
+                  <tbody>
+                    {products.map((product, index) => (
+                      <tr key={index}>
+                        <td>{product?.product}</td>
+                        <td>
+                          {product?.result?.code === '-' &&
+                            product.result.variantTotal > 1 && (
+                              <Button
+                                border='2px'
+                                borderColor='yellow.300'
+                                size='sm'
+                                onClick={() =>
+                                  handleVariantsOpen({
+                                    variants: product?.result?.variants,
+                                  })
+                                }
+                              >
+                                Lihat Variants
+                              </Button>
+                            )}
+                          {product?.result.code !== '-' &&
+                          product?.result.variantTotal > 1 ? (
+                            <>
+                              {product?.result.code}
+                              <Button
+                                variant='link'
+                                colorScheme='yellow'
+                                size='sm'
+                                onClick={() =>
+                                  handleVariantsOpen({
+                                    variants: product?.result?.variants,
+                                  })
+                                }
+                              >
+                                Variants lainya
+                              </Button>
+                            </>
+                          ) : (
+                            <>{product?.result.code}</>
+                          )}
+                        </td>
+                        <td>{product?.result.name}</td>
+                        <td>{product?.result.manufacture}</td>
+                        <td>
+                          {product?.result.price !== '-'
+                            ? `Rp ${formatCurrency(product?.result.price)}`
+                            : '-'}
+                        </td>
+                        <td>
+                          {product?.result.image !== '-' ? (
+                            <Image
+                              src={product?.result.image}
+                              width={100}
+                              height={100}
+                              alt={product?.result.name}
+                            />
+                          ) : (
+                            '-'
+                          )}
+                        </td>
+                        <td></td>
+                      </tr>
+                    ))}
+                  </tbody>
+                </table>
+              </div>
+            )}
+          </div>
         </div>
       </div>
-    </div>
+    </>
   );
 };
 
-- 
cgit v1.2.3


From b7def06cf32bf86036056718ece2350b4da72d23 Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Fri, 5 Apr 2024 15:43:24 +0700
Subject: recomendation

---
 .../components/products-recomendatison.jsx         | 141 ++++++++++++++++++---
 1 file changed, 123 insertions(+), 18 deletions(-)

(limited to 'src/pages/my/recomendation/components/products-recomendatison.jsx')

diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx
index 72b858e8..bb5950c8 100644
--- a/src/pages/my/recomendation/components/products-recomendatison.jsx
+++ b/src/pages/my/recomendation/components/products-recomendatison.jsx
@@ -14,10 +14,11 @@ const ProductsRecomendation = ({ id }) => {
   const [isLoading, setIsLoading] = useState(false);
   const [isOpen, setIsOpen] = useState(false);
   const [variantsOpen, setVariantsOpen] = useState([]);
+  const [otherRec, setOtherRec] = useState(false);
 
   const mappingProducts = async ({ index, product, result, variants }) => {
     const resultMapping = {
-      index : index,
+      index: index,
       product: product,
       result: {
         id: result?.id || '-',
@@ -34,25 +35,51 @@ const ProductsRecomendation = ({ id }) => {
     return resultMapping;
   };
 
-  const searchRecomendation = async ({ product, index }) => {
+  const searchRecomendation = async ({ product, index, operator = 'AND' }) => {
     let variants = [];
+    let resultMapping = {};
     const searchProduct = await axios.post(
-      `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}`
+      `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}&op=${operator}`
     );
 
-    const result =
-      searchProduct.data.response.numFound > 0
-        ? searchProduct.data.response.products[0]
-        : null;
+    if (operator === 'AND') {
+      const result =
+        searchProduct.data.response.numFound > 0
+          ? searchProduct.data.response.products[0]
+          : null;
 
-    if (result?.variantTotal > 1) {
-      const searchVariants = await axios.post(
-        `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${result.id}`
-      );
-      variants = searchVariants.data[0].variants;
-    }
+      if (result?.variantTotal > 1) {
+        const searchVariants = await axios.post(
+          `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${result.id}`
+        );
+        variants = searchVariants.data[0].variants;
+      }
+
+      resultMapping = await mappingProducts({
+        index,
+        product,
+        result,
+        variants,
+      });
+    } else {
+      const result =
+        searchProduct.data.response.numFound > 0
+          ? searchProduct.data.response.products
+          : null;
+
+      result.map((item) => {
+        if (item.variantTotal > 1) {
+          const searchVariants = axios.post(
+            `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${item.id}`
+          );
+          variants = searchVariants.data[0].variants;
+        }
+      });
 
-    const resultMapping = await mappingProducts({ index, product, result, variants });
+      console.log('ini result',  searchProduct.data.response);
+
+      
+    }
 
     return resultMapping;
   };
@@ -102,6 +129,14 @@ const ProductsRecomendation = ({ id }) => {
     }
     setIsOpen(false);
   };
+
+  const handlingOtherRec = ({ product }) => {
+    console.log('ini product', product);
+    const result = async () =>
+      await searchRecomendation({ product, index: 0, operator: 'OR' });
+
+    result();
+  };
   return (
     <>
       <BottomPopup
@@ -160,6 +195,64 @@ const ProductsRecomendation = ({ id }) => {
           </table>
         </div>
       </BottomPopup>
+      <BottomPopup
+        active={otherRec}
+        close={() => setOtherRec(false)}
+        className='w-full md:!w-[60%]'
+        title='Other Recomendations'
+      >
+        <div className='container'>
+          <table className='table-data'>
+            <thead>
+              <tr>
+                <th>Product</th>
+                <th>Item Code </th>
+                <th>Description</th>
+                <th>Brand</th>
+                <th>Price</th>
+                <th>Image</th>
+              </tr>
+            </thead>
+            <tbody>
+              {variantsOpen?.map((variant, index) => (
+                <tr key={index}>
+                  <td>{variant.code}</td>
+                  <td>{variant.attributes.join(', ') || '-'}</td>
+                  <td>
+                    {variant.price.discountPercentage > 0 && (
+                      <div className='flex items-center gap-x-1'>
+                        <div className={style['disc-badge']}>
+                          {Math.floor(variant.price.discountPercentage)}%
+                        </div>
+                        <div className={style['disc-price']}>
+                          Rp {formatCurrency(variant.price.price)}
+                        </div>
+                      </div>
+                    )}
+                    {variant.price.priceDiscount > 0 &&
+                      `Rp ${formatCurrency(variant.price.priceDiscount)}`}
+                    {variant.price.priceDiscount === 0 && '-'}
+                  </td>
+                  <td>
+                    <Button
+                      size='sm'
+                      w='100%'
+                      onClick={() =>
+                        hadnliChooseVariants({
+                          id: variant.parent.id,
+                          variant: variant,
+                        })
+                      }
+                    >
+                      Pilih
+                    </Button>
+                  </td>
+                </tr>
+              ))}
+            </tbody>
+          </table>
+        </div>
+      </BottomPopup>
       <div className='container mx-auto flex py-10'>
         <div className='w-3/12 pr-4'>
           <Menu />
@@ -216,10 +309,10 @@ const ProductsRecomendation = ({ id }) => {
                   <thead>
                     <tr>
                       <th>Product</th>
-                      <th>Item code</th>
-                      <th>Keterangan</th>
+                      <th>Item Code </th>
+                      <th>Description</th>
                       <th>Brand</th>
-                      <th>Harga</th>
+                      <th>Price</th>
                       <th>Image</th>
                       <th>lainnya</th>
                     </tr>
@@ -284,7 +377,19 @@ const ProductsRecomendation = ({ id }) => {
                             '-'
                           )}
                         </td>
-                        <td></td>
+                        <td>
+                          {' '}
+                          <Button
+                            border='2px'
+                            borderColor='red.500'
+                            size='sm'
+                            onClick={() =>
+                              handlingOtherRec({ product: product.product })
+                            }
+                          >
+                            Other
+                          </Button>
+                        </td>
                       </tr>
                     ))}
                   </tbody>
-- 
cgit v1.2.3


From f559326406175b18f75d2dd42c8bd2f0d901acbf Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Fri, 21 Jun 2024 15:09:57 +0700
Subject: recomendation product for sales

---
 .../components/products-recomendatison.jsx         | 107 +++++++++++++++++----
 1 file changed, 89 insertions(+), 18 deletions(-)

(limited to 'src/pages/my/recomendation/components/products-recomendatison.jsx')

diff --git a/src/pages/my/recomendation/components/products-recomendatison.jsx b/src/pages/my/recomendation/components/products-recomendatison.jsx
index bb5950c8..d39d2a99 100644
--- a/src/pages/my/recomendation/components/products-recomendatison.jsx
+++ b/src/pages/my/recomendation/components/products-recomendatison.jsx
@@ -8,6 +8,15 @@ import Image from 'next/image';
 import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
 import formatCurrency from '~/libs/formatCurrency';
 
+const exportToExcel = (data) => {
+  const worksheet = XLSX.utils.json_to_sheet(data);
+  const workbook = XLSX.utils.book_new();
+  XLSX.utils.book_append_sheet(workbook, worksheet, 'Results');
+
+  // Generate Excel file and trigger download in the browser
+  XLSX.writeFile(workbook, 'ProductRecommendations.xlsx');
+};
+
 const ProductsRecomendation = ({ id }) => {
   const [excelData, setExcelData] = useState(null);
   const [products, setProducts] = useState([]);
@@ -22,13 +31,8 @@ const ProductsRecomendation = ({ id }) => {
       product: product,
       result: {
         id: result?.id || '-',
-        name: result?.displayName || '-',
-        code: result?.code || '-',
-        image: result?.image || '-',
-        price: result?.lowestPrice.priceDiscount || '-',
-        manufacture: result?.manufacture.name || '-',
-        variantTotal: result?.variantTotal || '-',
-        variants: variants || '-',
+        name: result?.nameS || '-',
+        code: result?.defaultCodeS || '-',
       },
     };
 
@@ -76,9 +80,7 @@ const ProductsRecomendation = ({ id }) => {
         }
       });
 
-      console.log('ini result',  searchProduct.data.response);
-
-      
+      console.log('ini result', searchProduct.data.response);
     }
 
     return resultMapping;
@@ -91,26 +93,45 @@ const ProductsRecomendation = ({ id }) => {
       const results = await Promise.all(
         excelData.map(async (row, i) => {
           const index = i + 1;
-          const product = row[0];
-          return await searchRecomendation({ product, index });
+          const product = row['product'];
+          return await generateProductRecomendation({ product, index });
         })
-      ).then((results) => setProducts(results));
+      );
+
+      const formattedResults = results.map((result) => {
+        const formattedResult = { product: result.product };
+        for (let i = 0; i <= 5; i++) {
+          formattedResult[`recomendation product ${i + 1} - code`] = result.result[i] == null ? '-' :  result.result[i]?.code;
+          formattedResult[`recomendation product ${i + 1} - name`] = result.result[i] == null ? '-' : result.result[i]?.name ;
+        }
+        return formattedResult;
+      });
+
+      exportToExcel(formattedResults);
+      setProducts(results);
       setIsLoading(false);
     } else {
+      setIsLoading(false);
       console.log('No excel data available');
     }
   };
 
   const handleFileChange = (e) => {
+    setIsLoading(true);
     const file = e.target.files[0];
     const reader = new FileReader();
     reader.onload = (event) => {
       const data = new Uint8Array(event.target.result);
       const workbook = XLSX.read(data, { type: 'array' });
-      const sheetName = workbook.SheetNames[0];
-      const sheet = workbook.Sheets[sheetName];
-      const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1, range: 1 });
+
+      const firstSheetName = workbook.SheetNames[0];
+      const worksheet = workbook.Sheets[firstSheetName];
+      const jsonData = XLSX.utils.sheet_to_json(worksheet);
+
       setExcelData(jsonData);
+      console.log('ini json data', jsonData);
+
+      setIsLoading(false);
     };
     reader.readAsArrayBuffer(file);
   };
@@ -137,6 +158,56 @@ const ProductsRecomendation = ({ id }) => {
 
     result();
   };
+
+  const generateProductRecomendation = async ({ product, index }) => {
+    let variants = [];
+    let resultMapping = {};
+    const searchProduct = await axios.post(
+      `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}&op=AND`
+    );
+    const searchProductOR = await axios.post(
+      `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/generate-recomendation?q=${product}&op=OR`
+    );
+    const resultAND =
+      searchProduct.data.response.numFound > 0
+        ? searchProduct.data.response.docs[0]
+        : null; // hasil satu
+    const resultOR =
+      searchProductOR.data.response.numFound > 0
+        ? searchProductOR.data.response.docs
+        : []; // hasil 5
+
+    resultMapping = {
+      index: index,
+      product: product,
+      result: {},
+    };
+
+    // Add resultAND to resultMapping if it exists
+    resultMapping.result[0] = resultAND
+      ? {
+          id: resultAND?.id || '-',
+          name: resultAND?.nameS || '-',
+          code: resultAND?.defaultCodeS || '-',
+        }
+      : null;
+
+    // Add resultOR to resultMapping
+    if (resultOR.length > 0) {
+      resultOR.forEach((item, idx) => {
+        resultMapping.result[idx + 1] = {
+          id: item?.id || '-',
+          name: item?.nameS || '-',
+          code: item?.defaultCodeS || '-',
+        };
+      });
+    } else {
+      for (let i = 0; i <= 5; i++) {
+        resultMapping.result[i + 1] = null;
+      }
+    }
+    return resultMapping;
+  };
   return (
     <>
       <BottomPopup
@@ -302,7 +373,7 @@ const ProductsRecomendation = ({ id }) => {
               Generate
             </Button>
           </div>
-          <div className='grup mt-8'>
+          {/* <div className='grup mt-8'>
             {products && products.length > 0 && (
               <div className='group'>
                 <table className='table-data'>
@@ -396,7 +467,7 @@ const ProductsRecomendation = ({ id }) => {
                 </table>
               </div>
             )}
-          </div>
+          </div> */}
         </div>
       </div>
     </>
-- 
cgit v1.2.3

