File size: 31,442 Bytes
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
e9d97c1
8d099ce
 
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
 
 
 
8d099ce
e9d97c1
 
e80faca
e9d97c1
 
8d099ce
e80faca
 
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
 
 
e80faca
e9d97c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d099ce
 
 
 
e9d97c1
8d099ce
e9d97c1
 
 
 
8d099ce
e9d97c1
 
8d099ce
e9d97c1
8d099ce
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
8d099ce
e80faca
 
 
 
 
 
8d099ce
 
e9d97c1
 
8d099ce
e9d97c1
8d099ce
 
 
 
 
 
e9d97c1
8d099ce
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
 
8d099ce
e9d97c1
 
 
 
8d099ce
 
 
 
 
 
 
 
e9d97c1
 
8d099ce
 
e9d97c1
 
 
 
8d099ce
e9d97c1
 
 
 
 
 
 
 
 
8d099ce
e9d97c1
8d099ce
e9d97c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d099ce
 
 
e9d97c1
8d099ce
e9d97c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d099ce
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
aa6098f
8d099ce
 
 
e9d97c1
8d099ce
 
e9d97c1
 
 
8d099ce
 
 
 
 
 
 
 
 
e9d97c1
 
 
8d099ce
 
e9d97c1
8d099ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d97c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d099ce
 
 
f127b6f
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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<!DOCTYPE html>
<html>

<head>
    <title>Prompt Template</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Include AdminLTE CSS -->
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/adminlte.min.css">
    <!-- Include DataTables CSS -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap4.min.css">
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" />
    <style>
        .header-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card {
            padding: 0;
            margin-top: -15px;
            margin-left: -40px;
        }

        .table-container {
            width: 100%;
            overflow-x: auto;

        }

        .wrapper {
            display: flex;
            flex-direction: column;
            height: 100vh;
        }

        .content-wrapper {
            flex: 1;
            overflow-y: auto;
        }

        .content-header {
            padding: 5px;

        }

        div.dataTables_wrapper div.dataTables_length select {
            width: 60px;
            display: inline-block;
        }

        th,
        td {
            white-space: nowrap;
        }

        th:nth-child(1),
        td:nth-child(1) {
            /* Sno column */
            width: 1%;
        }

        th:nth-child(2),
        td:nth-child(2) {
            /* API Name column */
            width: 20%;
        }

        th:nth-child(3),
        td:nth-child(3) {
            /* API Endpoint column */
            width: 45%;
        }

        th:nth-child(4),
        td:nth-child(4) {
            /* view column*/
            width: 5%;
        }

        th:nth-child(5),
        td:nth-child(5) {
            /* view column*/
            width: 5%;
        }

        th:nth-child(6),
        td:nth-child(6) {
            /* view column*/
            width: 5%;
        }

        .reduced-width {
            width: 25%;
        }

        .viewButton {
            size: 2px;
        }

        div.dataTables_wrapper div.dataTables_length select {
            width: 60px;
            display: inline-block;
        }
    </style>
</head>

<body>
    {% include 'sidepane.html' %}

    <div class="wrapper">
        <div class="main-header" style="border-bottom: none;">
            <!-- Content Header (Page header) -->
            <div class="content-header">
                <div class="container-fluid">
                    <div class="container mt-2">
                        <div class="form-group left-align" id="company-select">
                            <!-- <label for="company" class="mr-1" style="margin-left:-60px;margin-top:-11px;">Company Name
                                <span class="text-danger">*</span></label>
                            <select type="text" id="company" name="company" class="form-control "
                                style="width: 20%;margin-left:-60px;" required>
                                <option value="" selected>Select</option>
                            </select> -->
                            <h4 id="selectedCompany" style="margin-left: 300px;"></h4>
                            <!-- <div class="col-12 d-flex justify-content-end mb-2">
                                <button class="btn btn-primary"
                                    style="margin-top:-40px;margin-right: -25px;position: fixed;" id="add">Add</button>

                              
                            </div> -->
                        </div>
                        <div class="row">
                            <div id="message-container" style="margin-left: 150px;width:500px;"></div>
                        </div>
                    </div>
                </div>
            </div>
            <section class="content" id="contentSection" style="display: none;">
                <div class="container-fluid">
                    <div class="form-group">
                        <!-- <label for="company_id">company_id</label>--->
                        <input type="hidden" id="company_id" name="company_id" class="form-control" required>
                    </div>
                </div>
                

                <!-- Main content-->
                <section class="content">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-body table-container">
                                        <table id="promptTable" class="table table-bordered table-striped">
                                            <thead>
                                                <tr>
                                                    <th>Sno</th>
                                                    <th>Datasource</th>
                                                    <th>Sample Prompt</th>
                                                    <th>View</th>
                                                    <th>Edit</th>
                                                    <th>Delete</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <!--    <tr>
                                                <td>1</td>
                                                <td>API</td>
                                                <td>What are the details of ASN24080200002?</td>
                                                <td><button class="btn btn-primary viewButton">View</button></td>
                                            </tr>
                                            <tr>
                                                <td>2</td>
                                                <td>API</td>
                                                <td>What is the expected receiving date of ASN24080200002</td>
                                                <td><button class="btn btn-primary viewButton">View</button></td>
                                            </tr>
                                            <tr>
                                                <td>3</td>
                                                <td>Document</td>
                                                <td>Explain Pre-Receiving Yard Management</td>
                                                <td><button class="btn btn-primary viewButton">View</button></td>
                                            </tr>
                                            <tr>
                                                <td>4</td>
                                                <td>Document</td>
                                                <td>Can you explain the process of unloading a vehicle in more detail?</td>
                                                 <td><button class="btn btn-primary viewButton">View</button></td>
                                            </tr>
                                            <tr>
                                                <td>5</td>
                                                <td>Visual Analysis</td>
                                                <td>I want to analyze item name and quantity in a bar chart in warehouse Allcargo Logistics</td>
                                                 <td><button class="btn btn-primary viewButton">View</button></td>
                                            </tr>
                                            <tr>
                                                <td>6</td>
                                                <td>Visual Analysis</td>
                                                <td>Can you generate a pie chart with item names and quantities in warehouse Allcargo Logistics</td>
                                                <td><button class="btn btn-primary viewButton">View</button></td>
                                            </tr>
                                                 <tr>
                                                    <td>7</td>
                                                    <td>Database</td>
                                                    <td>What are the active warehouses available</td>
                                                     <td><button class="btn btn-primary viewButton">View</button></td>
                                                </tr>
                                                <tr>
                                                    <td>8</td>
                                                    <td>Database</td>
                                                    <td>What are the warehouses available</td>
                                                     <td><button class="btn btn-primary viewButton">View</button></td>
                                                </tr>
                                                -->
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>
            </section>
        </div>

    </div>

    <!-- Modal -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addModalLabel">Add Prompt templates</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                    <span aria-hidden="true" data-backdrop="static" data-keyboard="false">&times;</span>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <!-- <label for="company_id">company_id</label>--->
                        <input type="hidden" id="company_id" name="company_id" class="form-control" required>
                    </div>
                    <form id="addPromptForm">
                        <div class="form-group">
                            <label for="scenario">Scenario <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="scenario" name="scenario" required>
                        </div>
                        <div class="form-group">
                            <label for="prompt">Sample Prompt <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="prompt" name="sampleprompt" required>
                        </div>
                        <div class="form-group">
                            <label for="comments">Comments <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="comments" name="comments" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button>
                    <!-- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> -->
                    <button type="button" id="save" onclick="save_file()" class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>

    { %include 'footer.html'%}
    <!-- Include DataTables JS and your custom script -->
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script>
    <script>

            var table = $('#promptTable').DataTable();
            const role = sessionStorage.getItem('userRole');
            console.log('Current role:', role); // Debug statement to check the role
            const passedCompanyName = sessionStorage.getItem('company_name')
            document.getElementById('selectedCompany').innerText = `Company Name: ${passedCompanyName}`;
            const company_id = sessionStorage.getItem('company_id');
            document.getElementById("company_id").value = company_id;

            data_get_from_db(company_id);

            // if (role === 'superadmin') {
            //     const storecompany_id = sessionStorage.getItem('company_id');
            //     data_get_from_db(storecompany_id);
            //     document.getElementById('company-select').style.display = 'block';
            //     //var companyId = getCookie('company_id')
            //     //console.log("company id for superadmin",storecompany_id);
            //     fetchCompanies();

            // } else {
            //     document.getElementById('company-select').style.display = 'block';
            //     const storecompany_id = sessionStorage.getItem('company_id');
            //     console.log('Current company_id:', storecompany_id); // Debug statement to check the role
            //     data_get_from_db(storecompany_id);
            // }

            function moveToNextInput(inputs, currentIndex) {
                const nextInput = inputs[currentIndex + 1];
                if (nextInput) {
                    nextInput.focus();
                } else {
                    // Optionally, submit the form or trigger the save button
                    document.getElementById("save").focus();
                }
            }

            const companySelect = document.getElementById('company');
            const addPromptForm = document.getElementById('addPromptForm');

       

  
        async function data_get_from_db(companyId) {
            
            if (companyId) {
                            try {
                                const Response = await fetch(`/api/get_prompt_templates?company_id=${companyId}`);
                                console.log("responce from api :==> ", Response)
                                const connectorsResponse = await Response.json();
                                console.log("knowledge data table after connecting to table", connectorsResponse);
                                console.log("prompt template data table after connecting to table", connectorsResponse);
                                const table = $('#promptTable').DataTable();  // Initialize DataTable at the start
                                table.clear();
                                if (!Array.isArray(connectorsResponse)) {
                                    throw new TypeError('Expected an array of companies');
                                    displayErrorMessage("prompt template details do not exist for this company. Please fill in the details.");
                                    contentSection.style.display = 'none';
                                } else {

                                    connectorsResponse.forEach((company, index) => {
                                        table.row.add([
                                            index + 1,
                                            company.scenario,
                                            company.prompt,
                                            `<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`,
                                            `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`,
                                            `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`,
                                            // "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye' fa-eye'></i></a>",
                                            // "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>",
                                            // "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></button>"
                                        ]).draw(false);
                                    });
                                    contentSection.style.display = 'block';
                                }

                            } catch (error) {
                                console.error('Error fetching company documents:', error);
                                //displayEmptyTable();

                                displayErrorMessage("Prompt template do not exist for this company. Please fill in the details by clicking add button.");
                                contentSection.style.display = 'none';
                            }
                        }
                        else {
                            // displayEmptyTable();

                            displayErrorMessage("Company id  do not exist for this company. Please fill in the details by clicking add button.");
                        }
            
        }

        function displayErrorMessage(message) {
                const messageContainer = document.getElementById('message-container');
                if (messageContainer) {
                    messageContainer.innerHTML = `<div class='alert alert-danger'>${message}</div>`;
                }
            }


        // Delete company
        function deleteCompany(button) {
            var companyId = $(button).data('kid-id');
            console.log("k_id deleted", companyId);
            $.ajax({
                type: "DELETE",
                url: `/api/prompt_template_for_del/${companyId}`,
                success: function () {
                    var table = $('#promptTable').DataTable();
                    table.row($(button).closest('tr')).remove().draw();

                    //alert("Company deleted successfully.");
                    const messageContainer = document.getElementById('message-container');
                    if (messageContainer)
                        messageContainer.innerHTML = `
                <div class='alert alert-danger'>
                    Prompt details deleted successfully
                    <button class='close' onclick='dismissMessage()'>OK</button>
                </div>`;
                    fetchCompanies(); // Refresh the table
                },
                error: function (xhr) {
                    alert("Error deleting company: " + xhr.responseJSON.detail);
                }
            });
        }
        // view company ! ...
        function viewCompany(button) {
            var companyId = $(button).data('kid-id');
            const modalTitle = document.getElementById('addModalLabel');
            modalTitle.textContent = 'View Prompt Template Details';
            console.log("view promt id (Rowid) ===> ", companyId)
            if(companyId)
        {
            $.getJSON(`/api/getpromttemplate/${companyId}`, function (company) {
                console.log("view data scenario  =====>  ", company.scenario)
                console.log("view data prompt    =====>  ", company.prompt)
                console.log("view data comments  =====>  ", company.comments)
                $('#scenario').val(company.scenario).attr('readonly', 'readonly');
                $('#prompt').val(company.prompts).attr('readonly', 'readonly');
                $('#comments').val(company.comments).attr('readonly', 'readonly');
                $('#save').hide();// Disable the "Save" button when the modal opens
                $('#addModal').modal('show');
                $('#saveupdate').hide();
            }).fail(function () {
                alert("Error retrieving company details.");
            });
        }
    }
        // Edit company details

        function editCompany(button) {
            var companyId = $(button).data('kid-id');
            const modalTitle = document.getElementById('addModalLabel');
            modalTitle.textContent = 'Edit Prompt Templates Details';

            console.info("company id ====#>  ", companyId);
            if(companyId)
        {
            $.getJSON(`/api/getpromttemplate/${companyId}`, function (company) {
                $('#scenario').val(company.scenario).removeAttr('readonly');
                $('#prompt').val(company.prompts).removeAttr('readonly');
                $('#comments').val(company.comments).removeAttr('readonly');
                $('#save').hide();
                $('#saveupdate').show();

                $('#addModal').modal('show');

                // Update the save button to save the edited company
                $('#saveupdate').off('click').on('click', function () {
                    saveupdate(companyId); // Pass the company ID for updating
                });
                console.log(" edit process completed !")
            }).fail(function () {
                alert("Error retrieving company details.");
            });
        }
    }

        // Save the updated details
        function saveupdate(companyId) {
            console.log("k_id for updating process _ id ! ..,..", companyId);
            const company_id = document.getElementById("company_id").value;
            var formData = new FormData();
            formData.append('scenario', $('#scenario').val());
            formData.append('prompt', $('#prompt').val());
            formData.append('comments', $('#comments').val());
            console.log("edited form data saved process begin ! ....", formData);

            $.ajax({
                url: `/api/putprompttemplates/${companyId}`,
                type: 'PUT',
                data: formData,
                processData: false, // Required for FormData
                contentType: false, // Required for FormData
                success: function (response) {
                    // alert('Prompt  details updated successfully.');
                    const messageContainer = document.getElementById('message-container');
                    if (messageContainer)
                        messageContainer.innerHTML = `
                <div class='alert alert-success'>
                  Prompt Data updated successfully
                    <button class='close' style='font-size:medium;margin-top:6px;' onclick='dismissMessage()'>OK</button>
                </div>`;
                    $('#addModal').modal('hide'); // Close the modal after saving
                    fetchUpdatedDocuments(company_id);
                },
                error: function () {
                    alert('Form has no changes to  update prompt details.');
                }
            });
        }
        function dismissMessage() {
            const messageContainer = document.getElementById('message-container');
            if (messageContainer) {
                messageContainer.innerHTML = '';
            }
        }
        async function fetchUpdatedDocuments(company_id) {
            console.log("company_id", company_id);
            var table = $('#promptTable').DataTable();
            const Response = await fetch(`/api/prompt_update?company_id=${company_id}`);
            const connectorsResponse = await Response.json();
            console.log("prompt  data table after connecting to table", connectorsResponse);
            table.clear();
            if (!Array.isArray(connectorsResponse) || connectorsResponse.length === 0) {
                //displayEmptyTable();

                displayErrorMessage("Promptdetails do not exist for this company. Please fill in the details.");
                contentSection.style.display = 'none';
            } else {

                connectorsResponse.forEach((company, index) => {
                    table.row.add([

                        index + 1,
                        company.scenario,
                        company.prompts,
                        `<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`,
                        `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`,
                        //`<a href='#' class='btn btn-danger btn-sm' onclick='deleteCompany(${company.company_id})'><i class='fas fa-trash'></i></a>`,
                        `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`,
                        company.id
                        // "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>",
                        // "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>",
                        // "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></button>"
                    ]).draw(false);
                });
                contentSection.style.display = 'block';
            }
        }
        function save_file() {
            const form = document.getElementById('addPromptForm');

            // Check if the form is valid
            if (!form.checkValidity()) {
                // If the form is invalid, show validation messages and stop the submission
                form.reportValidity();
                return;
            }
            const company_id = $('#company_id').val();
            const scenario = $('#scenario').val();
            const prompt = $('#prompt').val();
            const comments = $('#comments').val();

            // const view = "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>";
            // const edit = "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>";
            // const dele = "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></a>";

            var formData = new FormData($('#addPromptForm')[0]);
            const messageContainer = document.getElementById('message-container');
            formData.append("company_id", company_id),
                formData.append("scenario", scenario),
                formData.append("prompt", prompt),
                formData.append("comments", comments),

                //const formData = new FormData(); 
                fetch('/api/save_prompt_details', {
                    method: 'POST',
                    body: formData
                })
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('Network response was not ok');
                        }
                        return response.json();
                    })
                    .then(data => {
                        const row_id = data.row_id;
                        console.log("rowid", row_id);
                        var table = $('#promptTable').DataTable();
                        var rowCount = table.rows().count();
                        const view = `<a href='#' class='btn btn-info btn-sm'data-kid-id='${row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`;
                        const edit = `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`;
                        const dele = `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`;
                        table.row.add([
                            rowCount + 1,
                            scenario,
                            prompt,
                            view,
                            edit,
                            dele
                        ]).draw(false);

                        // const messageContainer = document.getElementById('message-container');
                        if (messageContainer)
                            messageContainer.innerHTML = `
                <div class='alert alert-success'>
                    Data saved successfully
                    <button class='close' style='font-size:medium;margin-top:6px;' onclick='dismissMessage()'>OK</button>
                </div>`;
                        $('#addModal').modal('hide');
                        document.getElementById('contentSection').style.display = 'block'; // Show the table section
                    })
                    .catch(error => console.error('Error:', error));
        }
        async function fetchCompanies() {
            try {
                const response = await fetch('/api/companies');
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const data = await response.json();
                displayCompanies(data.companies);
            } catch (error) {
                console.error('Error fetching companies:', error);
            }
        }

        function displayCompanies(companies) {
            const companySelect = document.getElementById('company');
            companySelect.innerHTML = '<option value="" selected>Select</option>'; // Reset the dropdown
            companies.forEach(company => {
                const option = document.createElement('option');
                option.value = company.name;
                option.textContent = company.name;
                companySelect.appendChild(option);
            });
        }
        function clearFormFields() {
            document.getElementById('scenario').value = "";
            document.getElementById('prompt').value = "";
            document.getElementById('comments').value = "";

        }


        $(document).ready(function () {
            $('#add').on('click', function () {
                const modalTitle = document.getElementById('addModalLabel');
                modalTitle.textContent = 'Add Prompt Template Details';
                $('#scenario').removeAttr('readonly');
                $('#prompt').removeAttr('readonly');
                $('#comments').removeAttr('readonly');
                $('#saveupdate').hide();
                $('#save').show();
                clearFormFields();
                $('#message-container').empty(); // Clear the message container
                $('#addModal').modal('show');
            });
            $('.modal-footer .btn-secondary, .modal-header .close').on('click', function () {
                $('#addModal').modal('hide');
            });

        });

    </script>
</body>

</html>