File size: 6,861 Bytes
02e4173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="UTF-8" />
  <title>مساعد أجريكاش</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      font-family: "Tahoma", "Cairo", sans-serif;
    }

    html, body {
      width: 100%;
      height: 100%;
      overflow: hidden;
      background: #f4f7f2;
    }

    /* موقع أجريكاش */
    .background-app {
      position: fixed;
      inset: 0;
      width: 100%;
      height: 100%;
      border: none;
      z-index: 1;
    }

    /* زر فتح الشات */
    .chat-toggle {
      position: fixed;
      bottom: 30px;
      right: 30px;
      width: 65px;
      height: 65px;
      border-radius: 50%;
      border: none;
      cursor: pointer;
      background: linear-gradient(135deg, #3b7f3b, #6abf4b);
      color: #fff;
      font-size: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
      z-index: 3;
    }

    /* صندوق الشات */
    .chat-container {
      position: fixed;
      bottom: 120px;
      right: 30px;
      width: 420px;
      max-height: 75vh;
      background: #ffffff;
      border-radius: 20px;
      box-shadow: 0 18px 40px rgba(0, 0, 0, 0.25);
      display: flex;
      flex-direction: column;
      overflow: hidden;
      z-index: 3;
      border: 1px solid #d8e6d2;
      display: none; /* سيتم فتحه تلقائياً من الجافاسكربت */
    }

    /* هيدر الشات */
    .chat-header {
      background: linear-gradient(135deg, #2f6f2f, #4fa953);
      color: #fff;
      padding: 14px 16px;
      display: flex;
      align-items: center;
      gap: 12px;
      font-size: 18px;
    }

    .chat-header-icon {
      font-size: 26px;
    }

    /* الرسائل */
    .chat-messages {
      padding: 14px;
      background: #f4f9f2;
      flex: 1;
      overflow-y: auto;
      font-size: 16px;
    }

    .message-row {
      margin-bottom: 12px;
      display: flex;
    }

    .message-row.user {
      justify-content: flex-end;
    }

    .message-row.bot {
      justify-content: flex-start;
    }

    .message-bubble {
      max-width: 80%;
      padding: 10px 14px;
      border-radius: 14px;
      white-space: pre-wrap;
      line-height: 1.5;
    }

    .message-row.user .message-bubble {
      background: #e6f5da;
      border: 1px solid #cde6bc;
      border-bottom-right-radius: 4px;
    }

    .message-row.bot .message-bubble {
      background: #ffffff;
      border: 1px solid #d9e1d3;
      border-bottom-left-radius: 4px;
    }

    /* حقل الإدخال */
    .chat-input-area {
      border-top: 1px solid #dfe8d8;
      background: #fdfefb;
      padding: 10px;
      display: flex;
      gap: 8px;
      align-items: center;
    }

    .chat-input {
      flex: 1;
      border-radius: 999px;
      border: 1px solid #cddac4;
      padding: 10px 16px;
      font-size: 16px;
      outline: none;
    }

    .chat-send-btn {
      border-radius: 999px;
      border: none;
      background: #6abf4b;
      color: #ffffff;
      padding: 10px 14px;
      font-size: 18px;
      cursor: pointer;
    }

    @media (max-width: 480px) {
      .chat-container {
        right: 10px;
        left: 10px;
        width: auto;
        max-height: 75vh;
      }

      .chat-toggle {
        right: 16px;
        bottom: 16px;
      }
    }
  </style>
</head>

<body>

  <!-- موقع أجريكاش -->
  <iframe
    src="https://agricash.app/"
    class="background-app"
    title="Agricash App"
  ></iframe>

  <!-- زر الشات -->
  <button class="chat-toggle" id="chatToggle">💬</button>

  <!-- صندوق الشات -->
  <div class="chat-container" id="chatContainer">
    <div class="chat-header">
      <div class="chat-header-icon">🌱</div>
      <div>مساعد أجريكاش الذكي</div>
    </div>

    <div class="chat-messages" id="chatMessages"></div>

    <div class="chat-input-area">
      <input
        id="chatInput"
        type="text"
        class="chat-input"
        placeholder="اكتب رسالتك هنا..."
      />
      <button class="chat-send-btn" id="chatSendBtn"></button>
    </div>
  </div>

  <script>
    const chatToggle = document.getElementById("chatToggle");
    const chatContainer = document.getElementById("chatContainer");
    const chatMessages = document.getElementById("chatMessages");
    const chatInput = document.getElementById("chatInput");
    const chatSendBtn = document.getElementById("chatSendBtn");

    let isOpen = false;
    let isSending = false;

    function toggleChat(forceOpen = false) {
      isOpen = forceOpen ? true : !isOpen;
      chatContainer.style.display = isOpen ? "flex" : "none";
    }

    chatToggle.addEventListener("click", () => toggleChat());

    function appendMessage(role, text) {
      const row = document.createElement("div");
      row.className = "message-row " + role;

      const bubble = document.createElement("div");
      bubble.className = "message-bubble";
      bubble.textContent = text;

      row.appendChild(bubble);
      chatMessages.appendChild(row);
      chatMessages.scrollTop = chatMessages.scrollHeight;
    }

    async function sendMessage() {
      const text = chatInput.value.trim();
      if (!text || isSending) return;

      appendMessage("user", text);
      chatInput.value = "";
      chatInput.focus();

      isSending = true;
      chatSendBtn.disabled = true;

      // thinking placeholder
      const thinkingRow = document.createElement("div");
      thinkingRow.className = "message-row bot";
      const thinkingBubble = document.createElement("div");
      thinkingBubble.className = "message-bubble";
      thinkingBubble.textContent = "🤖 جاري التفكير...";
      thinkingRow.appendChild(thinkingBubble);
      chatMessages.appendChild(thinkingRow);

      try {
        const res = await fetch("/chat", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ message: text }),
        });

        const data = await res.json();
        thinkingBubble.textContent = data.response || "لا توجد إجابة.";
      } catch (err) {
        thinkingBubble.textContent = "خطأ في الاتصال بالخادم.";
      }

      isSending = false;
      chatSendBtn.disabled = false;
    }

    chatSendBtn.addEventListener("click", sendMessage);

    chatInput.addEventListener("keydown", (e) => {
      if (e.key === "Enter") {
        e.preventDefault();
        sendMessage();
      }
    });

    // ⚡ OPEN CHAT AUTOMATICALLY ON PAGE LOAD
    window.onload = () => {
      toggleChat(true);
    };
  </script>
</body>
</html>