1 | // FINES
|
---|
2 |
|
---|
3 | function showIssueFineContent() {
|
---|
4 | const contentArea = document.getElementById('content-area');
|
---|
5 |
|
---|
6 | contentArea.innerHTML = `
|
---|
7 | <section class="home-section" id="issue-fine-section">
|
---|
8 | <div class="issue-fine-container">
|
---|
9 | <h2 class="form-header">Issue a Fine</h2>
|
---|
10 | <form id="issueFine" onsubmit="handleSubmit(event)">
|
---|
11 | <!-- User ID Field -->
|
---|
12 | <div class="form-row">
|
---|
13 | <div class="form-group">
|
---|
14 | <label for="loan-id">Loan ID</label>
|
---|
15 | <input type="text" id="loan-id" name="loan-id" required>
|
---|
16 | <div class="error-message" id="loanIdError">Please enter a valid User ID</div>
|
---|
17 | </div>
|
---|
18 | </div>
|
---|
19 |
|
---|
20 | <!-- Fine Amount Field -->
|
---|
21 | <div class="form-group">
|
---|
22 | <label for="fine-amount">Amount</label>
|
---|
23 | <input type="number" id="fine-amount" name="fine-amount" required>
|
---|
24 | <div class="error-message" id="fineAmountError">Please enter a valid fine amount</div>
|
---|
25 | </div>
|
---|
26 |
|
---|
27 | <!-- Fine Reason Field (Select Dropdown) -->
|
---|
28 | <div class="form-group">
|
---|
29 | <label for="fine-reason">Fine Reason</label>
|
---|
30 | <select id="fine-reason" name="fine-reason" required>
|
---|
31 | <option value="">Select Reason</option>
|
---|
32 | <option value="damaged-book">Damaged Book</option>
|
---|
33 | <option value="lost-book">Lost Book</option>
|
---|
34 | </select>
|
---|
35 | <div class="error-message" id="fineReasonError">Please select a reason</div>
|
---|
36 | </div>
|
---|
37 |
|
---|
38 | <div class="btn-container">
|
---|
39 | <button type="button" class="btn btn-secondary" onclick="clearForm()">Clear Form</button>
|
---|
40 | <button type="submit" class="btn btn-primary">Issue Fine</button>
|
---|
41 | </div>
|
---|
42 | </form>
|
---|
43 | </div>
|
---|
44 | </section>
|
---|
45 | `;
|
---|
46 | }
|
---|
47 |
|
---|
48 | function handleSubmit(event) {
|
---|
49 | event.preventDefault();
|
---|
50 |
|
---|
51 | // Handle form submission
|
---|
52 | const loanId = document.getElementById('loan-id').value;
|
---|
53 | const fineAmount = document.getElementById('fine-amount').value;
|
---|
54 | const fineReason = document.getElementById('fine-reason').value;
|
---|
55 |
|
---|
56 | // Add your form submission logic here (e.g., send to the server)
|
---|
57 | console.log(`Loan ID: ${loanId}, Fine Amount: ${fineAmount}, Reason: ${fineReason}`);
|
---|
58 | }
|
---|
59 |
|
---|
60 | function clearForm() {
|
---|
61 | document.getElementById('issueFine').reset();
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | function searchFines(){
|
---|
66 | const searchInput = document.querySelector('.search-input').value.toLowerCase();
|
---|
67 | const tableRows = document.querySelectorAll('#finesTableBody tr');
|
---|
68 |
|
---|
69 | tableRows.forEach(row => {
|
---|
70 | const loanid = row.cells[0].textContent.toLowerCase();
|
---|
71 | const fineid = row.cells[1].textContent.toLowerCase();
|
---|
72 | const amount = row.cells[2].textContent.toLowerCase();
|
---|
73 |
|
---|
74 | if (loanid.includes(searchInput) || fineid.includes(searchInput) || amount.includes(searchInput)) {
|
---|
75 | row.style.display = '';
|
---|
76 | } else {
|
---|
77 | row.style.display = 'none';
|
---|
78 | }
|
---|
79 | });
|
---|
80 | document.querySelector('.search-input').addEventListener('keyup', searchFines);
|
---|
81 | }
|
---|
82 |
|
---|
83 | function clearForm() {
|
---|
84 | // Get the form element by its ID
|
---|
85 | const form = document.getElementById("issueFine");
|
---|
86 |
|
---|
87 | // Reset the form fields
|
---|
88 | form.reset();
|
---|
89 |
|
---|
90 | // Optionally, hide any error messages (if any)
|
---|
91 | const errorMessages = document.querySelectorAll(".error-message");
|
---|
92 | errorMessages.forEach((message) => {
|
---|
93 | message.style.display = "none"; // Hide the error messages
|
---|
94 | });
|
---|
95 | }
|
---|
96 |
|
---|
97 | //Issue
|
---|
98 | function handleSubmit(event) {
|
---|
99 | event.preventDefault(); // Prevent form from submitting the default way
|
---|
100 |
|
---|
101 | // Get form values
|
---|
102 | const loanId = event.target['loan-id'].value;
|
---|
103 | const fineAmount = event.target['fine-amount'].value;
|
---|
104 |
|
---|
105 | // Send data to the backend
|
---|
106 | fetch('../Admin Actions/IssueFine.php', {
|
---|
107 | method: 'POST',
|
---|
108 | headers: {
|
---|
109 | 'Content-Type': 'application/x-www-form-urlencoded',
|
---|
110 | },
|
---|
111 | body: `loan-id=${loanId}&fine-amount=${fineAmount}`
|
---|
112 | })
|
---|
113 | .then(response => response.json()) // Expecting a JSON response
|
---|
114 | .then(data => {
|
---|
115 | if (data.success) {
|
---|
116 | alert('Fine issued successfully!');
|
---|
117 | location.reload(); // Refresh the page to reflect the changes
|
---|
118 | } else {
|
---|
119 | alert('Error issuing fine: ' + data.message); // Display error message if any
|
---|
120 | }
|
---|
121 | })
|
---|
122 | .catch(error => {
|
---|
123 | console.error('Error:', error);
|
---|
124 | alert('An error occurred while issuing the fine.');
|
---|
125 | });
|
---|
126 |
|
---|
127 | }
|
---|