File size: 10,071 Bytes
4a97c42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
document.addEventListener("DOMContentLoaded", async function () {
    const resultList = document.getElementById("result-list");
    const urlParams = new URLSearchParams(window.location.search);
    const productQuery = urlParams.get("products");
    const transactionQuery = urlParams.get("transactions");

    const createHeader = (text) => {
        const header = document.createElement("h3");
        header.textContent = text;
        header.className = "col-12 mt-4";
        resultList.appendChild(header);
    };

    if (!productQuery && !transactionQuery) {
        const messageElem = document.createElement("p");
        messageElem.textContent =
            "Tidak menemukan hasil pencarian yang sesuai.";
        messageElem.className = "text-center";
        resultList.appendChild(messageElem);
        return;
    }

    document.getElementById("searchInput").value =
        productQuery && transactionQuery
            ? productQuery + " " + transactionQuery
            : productQuery
            ? productQuery
            : transactionQuery;

    if (productQuery) {
        const products = await fetchProductsData();

        const filteredProducts = products.filter((product) => {
            const lowerQuery = productQuery.toLowerCase();
            return (
                product.name.toLowerCase().includes(lowerQuery) ||
                (product.description &&
                    product.description.toLowerCase().includes(lowerQuery))
            );
        });

        if (filteredProducts.length > 0) {
            createHeader("Produk");
            filteredProducts.forEach((product) => {
                const col = document.createElement("div");
                col.className = "col-lg-3 col-md-4 col-sm-6";

                const card = document.createElement("div");
                card.className = "card h-100";

                let thumbnail = "assets/";
                if (product.files && product.files.length > 0) {
                    thumbnail += product.files[0];
                }
                let thumbHTML = "";
                if (thumbnail.match(/\.(jpg|jpeg|png|gif)$/i)) {
                    thumbHTML = `<img src="${thumbnail}" class="card-img-top" alt="${product.name}">`;
                } else {
                    thumbHTML = `<img src="img/placeholder.jpg" class="card-img-top" alt="${product.name}">`;
                }

                const cardBody = document.createElement("div");
                cardBody.className = "card-body d-flex flex-column";

                const title = document.createElement("h5");
                title.className = "card-title";
                title.innerText = product.name;

                const desc = document.createElement("p");
                desc.className = "card-text";
                desc.innerText = product.description;

                const price = document.createElement("p");
                price.className = "card-text fw-bold";
                price.innerText = formatRupiah(product.price);

                const detailLink = document.createElement("a");
                detailLink.href = "product.html?id=" + product.id;
                detailLink.className = "btn btn-primary btn-lg mt-auto";
                detailLink.innerText = "Lihat Detail";

                const cartBtn = document.createElement("button");
                cartBtn.className = "btn btn-success btn-lg mt-2";
                cartBtn.innerText = "Tambah Keranjang";
                cartBtn.onclick = async function () {
                    await promptQuantityAndAdd(product.id);
                };

                card.innerHTML = thumbHTML;
                cardBody.appendChild(title);
                cardBody.appendChild(desc);
                cardBody.appendChild(price);
                cardBody.appendChild(detailLink);
                cardBody.appendChild(cartBtn);
                card.appendChild(cardBody);
                col.appendChild(card);
                resultList.appendChild(col);
            });
        } else {
            const messageElem = document.createElement("p");
            messageElem.textContent = "Produk tidak ditemukan.";
            messageElem.className = "text-center";
            resultList.appendChild(messageElem);
        }
    }

    if (productQuery && transactionQuery) {
        const hrElement = document.createElement("hr");
        resultList.appendChild(hrElement);
    }

    if (transactionQuery) {
        const userData = await getUserData();
        const transactions = userData.transactions ? userData.transactions : {};

        if (transactions && Object.keys(transactions).length !== 0) {
            const entries = Object.entries(transactions).sort(
                (a, b) => {
                    const parseDateFromKey = (key) => {
                        const [year, month, day, hour, minute, second, ms] =
                            key.split("_");
                        return new Date(
                            year,
                            month - 1,
                            day,
                            hour,
                            minute,
                            second,
                            ms
                        );
                    };
                    return parseDateFromKey(b[0]) - parseDateFromKey(a[0]);
                }
            );

            const filteredTransactions = entries.filter(
                ([key, transaction]) => {
                    const td = formatTransactionDate(key);
                    const pm = transaction.payment_method;
                    const total = transaction.total.toString();

                    return (
                        key.includes(transactionQuery) ||
                        td[0].includes(transactionQuery) ||
                        td[1].includes(transactionQuery) ||
                        pm.includes(transactionQuery) ||
                        total.includes(transactionQuery) ||
                        Object.values(productCount)
                            .join(",")
                            .includes(transactionQuery)
                    );
                }
            );

            if (filteredTransactions.length > 0) {
                createHeader("Transaksi");

                filteredTransactions.forEach(
                    ([transactionKey, transaction]) => {
                        const col = document.createElement("div");
                        col.className =
                            "transactions-body col-lg-4 col-md-6 col-sm-12 mt-4";

                        const card = document.createElement("div");
                        card.className = "card h-100";

                        const cardBody = document.createElement("div");
                        cardBody.className = "card-body d-flex flex-column";
                        cardBody.style.position = "relative";

                        let total = 0;

                        transaction.products.forEach((product) => {
                            total += product.price * product.total;
                        });

                        const td = formatTransactionDate(transactionKey);

                        let paymentIcon = "";
                        switch (transaction.payment_method) {
                            case "bank":
                                paymentIcon = "bi-building";
                                break;
                            case "qr":
                                paymentIcon = "bi-qr-code";
                                break;
                            case "card":
                                paymentIcon = "bi-credit-card";
                                break;
                            default:
                                paymentIcon = "bi-currency-dollar";
                        }

                        cardBody.innerHTML = `

            <div class="payment-icon-bg">

              <i class="bi ${paymentIcon}" style="font-size: 2.5rem;"></i>

              <span class="m-2">Metode: ${

                  transaction.payment_method

                      ? transaction.payment_method

                      : "N/A"

              }</span>

            </div>

            <div class="content">

              <div class="d-flex justify-content-between align-items-center">

                <div class="transaction-total fw-bold">

                  Total: ${formatRupiah(total)}

                </div>

              </div>

              <div class="d-flex justify-content-between align-items-center mt-auto">

                <div class="transaction-date">

                  Tanggal: ${td[0]} <br> Waktu: ${td[1]}

                </div>

              </div>

            </div>

          `;

                        card.appendChild(cardBody);
                        col.appendChild(card);
                        resultList.appendChild(col);

                        col.addEventListener("click", () => {
                            window.location.href = `transaction.html?date=${transactionKey}`;
                        });
                    }
                );
            } else {
                const messageElem = document.createElement("p");
                messageElem.textContent = "Transaksi tidak ditemukan.";
                messageElem.className = "text-center";
                alert(messageElem.textContent, false);
                resultList.appendChild(messageElem);
            }
        } else {
            const messageElem = document.createElement("p");
            messageElem.textContent = "Anda belum melakukan transaksi apapun. Silahkan beli produk terlebih dahulu.";
            messageElem.className = "text-center";
            alert(messageElem.textContent, false);
            resultList.appendChild(messageElem);
        }
    }
});