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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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

import flet as ft
import json
import pandas as pd
import Bot_Page
import Bot_Page_ٍSummary
import time
import utils

df_laws = pd.read_excel("Data/UAE_Laws_LC_April_3.xlsx")

def unique_ordered_list(law_ids):
    seen = set()
    unique_list = []
    for item in law_ids:
        if item not in seen:
            seen.add(item)
            unique_list.append(item)
    return unique_list

def mawad_func(law_id):
    return [json.loads(item) for item in json_nodes if json.loads(item).get("File_ID") == str(law_id)]

def main(page: ft.Page):
    page.theme = ft.Theme(
        color_scheme=ft.ColorScheme(
            primary=ft.colors.BLUE,
            secondary=ft.colors.CYAN,
        ),
        font_family="Arial"
    )

    page.bgcolor = ft.colors.WHITE
    page.window_maximized = True    
    page.horizontal_alignment = ft.CrossAxisAlignment.STRETCH
    page.vertical_alignment = ft.MainAxisAlignment.START

    relevant_matches_active = False  
    current_file_id = None  
    current_hits = None  

    def create_table(books, pages, hits):
        def create_cell(content, h, wid, is_header=False):
            return ft.Container(
                content=ft.Text(content, weight="bold" if is_header else None),
                border=ft.border.all(1, "cyan"),
                padding=ft.padding.all(8),
                border_radius=2,
                alignment=ft.alignment.center,
                width=wid,
                height=h
            )

        headers_height = 40
        headers_width = 250

        header = ft.Row(
            controls=[
                create_cell("الموضوع", headers_height, 500, is_header=True),
                create_cell("تاريخ الاصدار", headers_height, headers_width, is_header=True),
                create_cell("التشريع", headers_height, headers_width, is_header=True)
            ],
            alignment="center",
            spacing=0
        )

        values_height = 90
        values = ft.Row(
            controls=[
                create_cell(hits, values_height, 500),
                create_cell(pages, values_height, headers_width),
                create_cell(books, values_height, headers_width)
            ],
            alignment="center",
            spacing=0
        )

        table = ft.Column(
            controls=[
                header,
                values
            ],
            alignment="center",
            spacing=0
        )

        return table

    def on_search(e):
        loading_indicator.visible = True
        law_info_table.visible = False

        page.update()

        query = search_field.value

        if  current_search_mode.value == "Mode 1 is selected":
            
            res = utils.Get_Azure_AI_RAG_Json(query,20)

            law_ids = [int(id.get("File_ID")) for id in res]

            unique_law_ids = unique_ordered_list(law_ids)
            
            results = df_laws[df_laws['Id'].isin(unique_law_ids)].reset_index()

            results['Id'] = pd.Categorical(results['Id'], categories=unique_law_ids, ordered=True)

            results = results.sort_values('Id').reset_index(drop=True)

            results = results.head(10) 

        elif  current_search_mode.value == "Mode 2 is selected":
            
            res = utils.by_law_title(query,20)

            law_ids = [int(id.get("File_ID")) for id in res]

            unique_law_ids = unique_ordered_list(law_ids)
            
            results = df_laws[df_laws['Id'].isin(unique_law_ids)].reset_index()

            results['Id'] = pd.Categorical(results['Id'], categories=unique_law_ids, ordered=True)

            results = results.sort_values('Id').reset_index(drop=True)

            results = results.head(10) 

        table.rows.clear()
        for i in range(results.shape[0]):
            
            curr_id = int(results['Id'][i])
            count_df = (df_laws[df_laws['Id']== curr_id]).reset_index()['Subjects Count'][0]            
            
            articles_count = int(count_df)
            table.rows.append(
                ft.DataRow(cells=[
                    ft.DataCell(ft.IconButton(ft.icons.LINK, on_click=lambda e, link=results['short_url'][i]: page.launch_url(link))),
                    ft.DataCell(ft.Text(articles_count, text_align=ft.TextAlign.RIGHT)),
                    ft.DataCell(ft.Container(
                        ft.Text(results['Description'][i], text_align=ft.TextAlign.RIGHT, rtl=True),
                        alignment=ft.alignment.center_right
                    )),
                    ft.DataCell(ft.Container(
                        ft.Text(results['Issue Date'][i], text_align=ft.TextAlign.RIGHT, rtl=True),
                        alignment=ft.alignment.center_right
                    )),
                    ft.DataCell(ft.Container(
                        ft.TextButton(results['Topic'][i], on_click=lambda e, file_id=results['Id'][i]: show_topic_details(file_id, res)),
                        alignment=ft.alignment.center_right
                    )),
                ])
            )


        loading_indicator.visible = False
        table.visible = True
        page.update()

    loading_indicator = ft.ProgressRing(visible=False)

    def show_topic_details(file_id, res):
        dialog_button.on_click = lambda e, fid=file_id :open_chat_dlg(e, fid) 
        summary_button.on_click = lambda e, fid=file_id :open_summary_dlg(e, fid) 

        nonlocal current_file_id, current_hits
        current_file_id = file_id
        current_hits = res
        table.visible = False
        law_info_table.visible = True
        info_df = df_laws[df_laws['Id'] == file_id].reset_index()

        description = info_df['Description'][0]
        issue_date = info_df['Issue Date'][0]
        link = info_df['short_url'][0]
        topic = info_df['Topic'][0]    

        law_info_table.controls = [create_table(topic, issue_date, description)]

        mada_df = pd.read_csv(f"Data/Topics_CSV/{file_id}.csv")
        mada_df = mada_df[mada_df['Header'] != "Desc"]
        
        mawad_details = mada_df['Header'].to_list()

        if len(res) == 0:
            ordered_details = mawad_details
            hits = []
        
        else:

            hits = [item for item in res if item['File_ID'] == str(file_id)]
            hits = [item['Row_Header'] for item in hits] 
                        
            if relevant_matches_active:
                
                ordered_details = {header: index for index, header in enumerate(hits)}

                ordered_details = sorted(mawad_details, key=lambda x: ordered_details.get(x, float('inf')))

            else:

                ordered_details = mawad_details
                        
        details_list.controls.clear()
        for item in ordered_details:
            if not relevant_matches_active or item in hits:
                
                bc = ft.colors.YELLOW if item in hits else ft.colors.WHITE

                curr_mada_text =(mada_df[mada_df['Header'] == item]).reset_index()['Header'][0] + " * " + (mada_df[mada_df['Header'] == item]).reset_index()['Text'][0]
                
                details_list.controls.append(
                    ft.Container(
                        content=ft.ElevatedButton(
                            text=item,
                            on_click=lambda e, text=curr_mada_text: show_mada_text(text),
                            bgcolor=ft.colors.CYAN,
                            color=bc
                        ),
                        alignment=ft.alignment.center_right,
                        padding=ft.padding.all(10),
                        border_radius=ft.border_radius.all(5),
                        shadow=ft.BoxShadow(
                            blur_radius=5,
                            spread_radius=1,
                            color=ft.colors.GREY_100,
                            offset=ft.Offset(2, 2)
                        )
                    )
                )
        details_view.visible = True
        search_view.visible = False
        page.update()

    def show_mada_text(text):

        try:
            header, article = text.split('*', 1)
            
            header_text = ft.Text(header, weight=ft.FontWeight.BOLD, rtl=True, text_align=ft.TextAlign.CENTER)

            article_text = ft.Text(article.strip(), rtl=True, text_align=ft.TextAlign.RIGHT)
            
            column_controls = [header_text, article_text]

        except:
            column_controls = [ft.Text(text,rtl=True)]
                
        mada_column.content.controls = column_controls

        page.update()

    def go_back_to_search(e):
        details_view.visible = False
        search_view.visible = True
        table.visible = True
        page.update()

    def toggle_relevant_matches(e):
        nonlocal relevant_matches_active
        relevant_matches_active = not relevant_matches_active
        relevant_matches_button.bgcolor = ft.colors.YELLOW if relevant_matches_active else ft.colors.CYAN
        page.update()
        if current_file_id is not None:
            show_topic_details(current_file_id, current_hits)

    search_field = ft.TextField(label="أدخل أستفسارك", text_align=ft.TextAlign.RIGHT, expand=True)
    search_button = ft.ElevatedButton("بحث", icon=ft.icons.SEND, on_click=on_search, bgcolor=ft.colors.BLUE, color=ft.colors.WHITE)

    table = ft.DataTable(
        border=ft.border.all(2, "cyan"),
        border_radius=10,
        vertical_lines=ft.BorderSide(1, color="F6F5F5"),
        horizontal_lines=ft.BorderSide(1, "F6F5F5"),                
        columns=[
            ft.DataColumn(label=ft.Text("رابط القانون", text_align=ft.TextAlign.RIGHT)),
            ft.DataColumn(label=ft.Text("المواد", text_align=ft.TextAlign.RIGHT)),
            ft.DataColumn(label=ft.Text("الموضوع", text_align=ft.TextAlign.RIGHT, rtl=True)),
            ft.DataColumn(label=ft.Text("تاريخ الإصدار", text_align=ft.TextAlign.RIGHT, rtl=True)),
            ft.DataColumn(label=ft.Text("التشريع", text_align=ft.TextAlign.RIGHT, rtl=True, width = 350)),
        ],
        rows=[]
    )

    details_list = ft.Column(scroll=ft.ScrollMode.AUTO)
    container_style = {
        "width": page.window_width / 2 - 20,
        "height": page.window_height - 250,
        "padding": 20,
        "bgcolor": ft.colors.WHITE,
        "border": ft.border.all(1, color=ft.colors.GREY_200),
        "border_radius": ft.border_radius.all(15),
        "shadow": ft.BoxShadow(
            blur_radius=20,
            spread_radius=2,
            color=ft.colors.BLACK12,
            offset=ft.Offset(4, 4)
        )
    }

    details_column = ft.Container(
        content=details_list,
        **container_style
    )


    mada_column = ft.Container(
        content=ft.Column(controls=[], scroll=ft.ScrollMode.AUTO, alignment=ft.MainAxisAlignment.CENTER,
                                                                            horizontal_alignment=ft.CrossAxisAlignment.CENTER),
        **container_style
    )


    def radio_mode(radio_group):
        if radio_group.value == "في محتوى التشريعات":
            current_search_mode.value = ("Mode 1 is selected")
        elif radio_group.value == "في عناوين التشريعات":
            current_search_mode.value = ("Mode 2 is selected")

    def on_radio_change(e):
        radio_mode(e.control)

    search_mode_radios = ft.RadioGroup(
        on_change=on_radio_change,
        value="في محتوى التشريعات",
        content=ft.Row(
            [
                ft.Row([ft.Text("في عناوين التشريعات", weight=ft.FontWeight.BOLD), ft.Radio(value="في عناوين التشريعات")]),
                ft.Row([ft.Text("في محتوى التشريعات", weight=ft.FontWeight.BOLD), ft.Radio(value="في محتوى التشريعات")]),
            ],
        alignment=ft.MainAxisAlignment.CENTER,
        spacing=80  
        )
    )

    current_search_mode = ft.Text("", visible = False)

    back_button = ft.ElevatedButton(
        text="عودة",
        on_click=go_back_to_search,
        icon=ft.icons.ARROW_BACK_IOS_NEW_SHARP,
        bgcolor=ft.colors.CYAN,
        color=ft.colors.WHITE
    )

    relevant_matches_button = ft.ElevatedButton(
        text="نتائج البحث",
        on_click=toggle_relevant_matches,
        bgcolor=ft.colors.CYAN,
        color=ft.colors.WHITE,
        style=ft.ButtonStyle(
            shape=ft.RoundedRectangleBorder(radius=8),
            elevation=2,
        )
    )

    table_container = ft.Container(
        content=table,
        width=page.width,
    )
    search_view = ft.Column([
        ft.Row([search_field, search_button, loading_indicator], alignment=ft.MainAxisAlignment.END, spacing=10),
        search_mode_radios,
        current_search_mode,
    ], alignment=ft.MainAxisAlignment.START, spacing=20)

    dlg = ft.AlertDialog(
        modal=True,
        content = ft.Text(),

    )

    def open_summary_dlg(e, fid):

        bot_page = Bot_Page_ٍSummary.main(ft.Page, fid, df_laws)
        bot_page.controls[0].content.controls[0].content.controls[2].controls[0].on_click = lambda e: page.close(dlg)

        dlg.content = bot_page
        dlg.open = True
        dlg.update()
        
        time.sleep(1)
        bot_page.controls[1].on_click(None)
        time.sleep(1)
        bot_page.controls[2].on_click(None)
        page.update()

    def open_chat_dlg(e, fid):

        bot_page = Bot_Page.main(ft, ft.Page, utils.by_law_id, fid, df_laws)
        bot_page.controls[0].content.controls[0].content.controls[2].controls[0].on_click = lambda e: page.close(dlg)

        dlg.content = bot_page
        dlg.open = True
        dlg.update()

    dialog_button = ft.ElevatedButton(
        text="س , ج حول التشريع",
        icon=ft.icons.CHAT,
        tooltip="Required",
        on_click=open_chat_dlg,
        style=ft.ButtonStyle(
            bgcolor=ft.colors.WHITE,
            color=ft.colors.BLACK,
            shape=ft.RoundedRectangleBorder(radius=15),
            elevation=5,  
            side=ft.BorderSide(
                color=ft.colors.GREY,
                width=1
            )
        )
    )

    summary_button = ft.ElevatedButton(
        text="ملخص التشريع",
        icon=ft.icons.MENU_BOOK,
        tooltip="ملخص",
        on_click=open_summary_dlg,
        style=ft.ButtonStyle(
            bgcolor=ft.colors.WHITE,
            color=ft.colors.BLACK,
            shape=ft.RoundedRectangleBorder(radius=15),
            elevation=5,
            side=ft.BorderSide(
                color=ft.colors.GREY,
                width=1
            )
        )
    )        

    
    page.add(dlg)


    left_buttons_row = ft.Row(
        controls=[back_button, relevant_matches_button],
        alignment=ft.MainAxisAlignment.CENTER,
        spacing=20
    )

    right_buttons_row = ft.Row(
        controls=[dialog_button, summary_button],
        alignment=ft.MainAxisAlignment.CENTER,
        spacing=20
    )    

    detailed_buttons_row = ft.Row(
        controls=[left_buttons_row, right_buttons_row],
        alignment=ft.MainAxisAlignment.SPACE_AROUND,
        spacing=20
    )    


    law_info_table = ft.Row([ft.Text("")], alignment=ft.MainAxisAlignment.CENTER)

    details_view = ft.Column(
        [
            ft.Row(
                [
                    law_info_table
                ],
                alignment=ft.MainAxisAlignment.CENTER  
            ),
            
            detailed_buttons_row,

            ft.Row(
                [
                    #ft.Text(" "),
                    mada_column,
                    details_column
                ],
                alignment=ft.MainAxisAlignment.CENTER, 
                spacing=20
            )
        ],
        alignment=ft.MainAxisAlignment.CENTER,  
        spacing=20,
        visible=False
    )


    law_info_table.visible = False
    page.add(ft.Column([search_view, table_container, details_view]))
    table.visible = False

    current_search_mode.value = "Mode 1 is selected"


    page.update()

ft.app(target=main, assets_dir='assets')