From 8e21f898548c1f6ee6136d8398df132ee3caa268 Mon Sep 17 00:00:00 2001
From: Phan Tung Bui <phan-tung.bui@inlibro.com>
Date: Thu, 11 Apr 2024 16:02:27 -0400
Subject: [PATCH] Bug 36527: Patron category or item type not changing when
 editing another circulation rule

Plan test :
1. Go to Administration > Circulation and fine rules
2. Add a couple of rules with various patron category/item type
   combinations
3. Click on "Edit" next to one of the rules
   --> The line should become highlighted in yellow and the values
       should be copied in the very last row
4. Click on "Edit" next to another rule
5. Click OK in the browser dialog box to confirm you want to edit
   another rule
   --> Depending on the rules, the values for the patron category
       and/or item type might not change in the editing row
6. Repeat steps 4 and 5
   --> The patron category and item type do not always change
7. Apply the batch
8. Redo step from 3 to 6
9. Observe that category and item type change accordingly

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
---
 .../prog/en/modules/admin/smart-rules.tt          | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
index 1109b0e99ce..f5d32562e47 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
@@ -1498,6 +1498,7 @@
                 }
                 $('#default-circulation-rules td').removeClass('highlighted-row');
                 $(this).parent().parent().find("td").each(function (i) {
+
                     $(this).addClass('highlighted-row');
                     itm_code = $(this).data('code');
                     itm_text = $(this).text();
@@ -1537,11 +1538,19 @@
                         });
                         // select the corresponding option
                         $(current_column).find("select option").each(function(){
-                            opt = $(this).attr('value');
-                            if ( opt == itm_code ) {
-                                $(this).attr('selected', 'selected');
+                            // Reset selection status for all options
+                            $(this).prop('selected', false);
+                            // Declare opt here to ensure it is scoped correctly within the loop
+                            let opt = $(this).val();
+
+                            // Select the matching option
+                            if (opt == itm_code) {
+                                $(this).prop('selected', true);
                             }
                         });
+
+                        // After setting the correct option, update the select to reflect the change
+                        $(current_column).find('select').trigger('change');
                         var current_input_id = $(current_column).children('input').first().attr('id');
                         if ( i == 0 || i == 1 ) {
                             // Disable the 2 first columns, we cannot update them.
-- 
2.30.2