@@ -5,58 +5,119 @@ document.addEventListener("DOMContentLoaded", () => {
55
66 async function loadReport ( ) {
77 const payment_type = paymentTypeSelect . value ;
8+
9+ console . log ( `Loading report for payment_type: ${ payment_type } ` ) ;
810
9- const res = await fetch (
10- ` ${ CONFIG . basePath } /stay/late-checkout-fees?payment_type= ${ payment_type } ` ,
11- {
12- method : 'GET' ,
13- headers : {
14- 'Content-Type' : 'application/json' ,
15- Authorization : `Bearer ${ sessionStorage . getItem ( 'token' ) } `
11+ try {
12+ const res = await fetch (
13+ ` ${ CONFIG . basePath } /stay/late-checkout-fees?payment_type= ${ payment_type } ` ,
14+ {
15+ headers : {
16+ Authorization : `Bearer ${ sessionStorage . getItem ( "token" ) } `
17+ }
1618 }
17- }
18- ) ;
19+ ) ;
1920
20- const json = await res . json ( ) ;
21- if ( ! json . success ) return ;
21+ const json = await res . json ( ) ;
22+
23+ console . log ( 'Response:' , json ) ;
24+
25+ if ( ! json . success ) {
26+ console . error ( 'Failed to load report:' , json . message ) ;
27+ alert ( json . message || 'Failed to load report' ) ;
28+ return ;
29+ }
2230
23- renderTable ( json . data ) ;
31+ console . log ( `Loaded ${ json . data . length } records` ) ;
32+ renderTable ( json . data , payment_type ) ;
33+ } catch ( error ) {
34+ console . error ( 'Error loading report:' , error ) ;
35+ alert ( 'Error loading report. Please check console.' ) ;
36+ }
2437 }
2538
26- function renderTable ( rows ) {
39+ function renderTable ( rows , payment_type ) {
2740 tableBody . innerHTML = "" ;
2841
42+ if ( rows . length === 0 ) {
43+ tableBody . innerHTML = '<tr><td colspan="11" class="text-center">No records found</td></tr>' ;
44+ return ;
45+ }
46+
47+ console . log ( `Rendering ${ rows . length } rows for payment_type: ${ payment_type } ` ) ;
48+
2949 rows . forEach ( ( row , index ) => {
30- const tr = document . createElement ( "tr" ) ;
50+ let actionBtn = "—" ;
3151
52+ // Use row.id (this is the primary key from the database)
53+ if ( payment_type === "payment_pending" ) {
54+ actionBtn = `<button type="button" class="btn btn-danger btn-sm" onclick="toggleRevoke(${ row . id } ,'admin cancelled')">Revoke</button>` ;
55+ }
56+
57+ if ( payment_type === "fees_revoked" ) {
58+ actionBtn = `<button type="button" class="btn btn-success btn-sm" onclick="toggleRevoke(${ row . id } ,'cash pending')">Restore</button>` ;
59+ }
60+
61+ const tr = document . createElement ( "tr" ) ;
3262 tr . innerHTML = `
3363 <td>${ index + 1 } </td>
34- <td>₹${ row . amount } </td>
35- <td>${ row . bookingid } </td>
36- <td>${ row . guest_name } </td>
37- <td>${ row . mobile } </td>
38- <td>${ row . roomno } </td>
39- <td>${ row . roomtype } </td>
40- <td>${ formatDate ( row . checkin ) } </td>
41- <td>${ formatDate ( row . checkout ) } </td>
42- <td>${ row . nights } </td>
64+ <td>₹${ row . amount || 0 } </td>
65+ <td>${ actionBtn } </td>
66+ <td>${ row . bookingid || '-' } </td>
67+ <td>${ row . guest_name || '-' } </td>
68+ <td>${ row . mobile || '-' } </td>
69+ <td>${ row . roomno || '-' } </td>
70+ <td>${ row . roomtype || '-' } </td>
71+ <td>${ row . checkin ? formatDate ( row . checkin ) : '-' } </td>
72+ <td>${ row . checkout ? formatDate ( row . checkout ) : '-' } </td>
73+ <td>${ row . nights || '-' } </td>
4374 ` ;
44-
4575 tableBody . appendChild ( tr ) ;
4676 } ) ;
4777
48- // Reapply table enhance (sort, search, filters)
4978 enhanceTable ( "reportTable" , "tableSearch" ) ;
5079 }
5180
52- // Auto load on page load
53- loadReport ( ) ;
81+ window . toggleRevoke = async ( transactionId , status ) => {
82+ console . log ( `toggleRevoke called with: transactionId=${ transactionId } , status=${ status } ` ) ;
83+
84+ if ( ! confirm ( `Are you sure you want to change the status to "${ status } "?` ) ) {
85+ return ;
86+ }
5487
55- // Change when payment type dropdown changes
56- paymentTypeSelect . addEventListener ( "change" , loadReport ) ;
88+ try {
89+ const res = await fetch (
90+ `${ CONFIG . basePath } /stay/late-checkout-fees/revoke` ,
91+ {
92+ method : "PUT" ,
93+ headers : {
94+ "Content-Type" : "application/json" ,
95+ Authorization : `Bearer ${ sessionStorage . getItem ( "token" ) } `
96+ } ,
97+ body : JSON . stringify ( { transactionId, status } )
98+ }
99+ ) ;
57100
58- // Re-search
59- searchInput . addEventListener ( "input" , ( ) => {
60- enhanceTable ( "reportTable" , "tableSearch" ) ;
61- } ) ;
62- } ) ;
101+ const json = await res . json ( ) ;
102+
103+ console . log ( 'Update response:' , json ) ;
104+
105+ if ( json . success ) {
106+ alert ( 'Transaction updated successfully' ) ;
107+ loadReport ( ) ; // Reload the table
108+ } else {
109+ alert ( json . message || "Failed to update transaction" ) ;
110+ }
111+ } catch ( error ) {
112+ console . error ( 'Error updating transaction:' , error ) ;
113+ alert ( "An error occurred. Please check console for details." ) ;
114+ }
115+ } ;
116+
117+ // Initial load
118+ loadReport ( ) ;
119+
120+ // Event listeners
121+ paymentTypeSelect . addEventListener ( "change" , loadReport ) ;
122+ searchInput . addEventListener ( "input" , ( ) => enhanceTable ( "reportTable" , "tableSearch" ) ) ;
123+ } ) ;
0 commit comments