-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.edit-airport-location.php
151 lines (133 loc) · 5.17 KB
/
section.edit-airport-location.php
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
<?php
require_once 'autoload.php';
use Transport\{Airline, Airport, AirportLocation};
use Generic\Utils;
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$id = $id === false ? null : $id;
$airportLocation = new AirportLocation($id);
if (!is_null($id) && !$airportLocation->getId())
{
exit(Utils::showResourceNotFound());
}
$airportLocationId = $airportLocation->getId();
?>
<!-- Back button -->
<div class="d-flex justify-content-between top-page-buttons mb-2">
<button class="btn btn-sm btn-outline-primary px-2 me-auto" onclick="$(document).trigger('airportLocations:reloadList');">
<i class="fa-solid fa-chevron-left"></i>
List
</button>
</div>
<?php if ($airportLocation->getId()): ?>
<h2>Edit Airport Location</h2>
<input type="hidden" id="airport-location-id" value="<?= $airportLocationId ?>">
<?php else: ?>
<h2>Add Airport Location</h2>
<input type="hidden" id="airport-location-id" value="">
<?php endif; ?>
<div class="row">
<div class="col-12">
<label for="_airport" class="form-label">Airport</label>
<select class="form-select" id="_airport">
<option value="">Select Airport</option>
<?php foreach (Airport::getAll() as $row): ?>
<option value="<?= $row->id ?>" <?= ($row->id == $airportLocation->airportId) ? 'selected' : '' ?>><?= $row->name ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12">
<label for="_airline" class="form-label">Airline</label>
<select class="form-select" id="_airline">
<option value="">Select Airline</option>
<?php foreach (Airline::getAll() as $row): ?>
<option value="<?= $row->id ?>" <?= ($row->id == $airportLocation->airlineId) ? 'selected' : '' ?>><?= $row->name ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12">
<label for="_type" class="form-label">Type</label>
<select class="form-select" id="_type">
<option value="">Select Type</option>
<option value="Arrival" <?= ($airportLocation->type == 'Arrival') ? 'selected' : '' ?>>Arrival</option>
<option value="Departure" <?= ($airportLocation->type == 'Departure') ? 'selected' : '' ?>>Departure</option>
</select>
</div>
<div class="col-12">
<label for="_location" class="form-label">Location</label>
<input
type="text"
class="form-control"
id="_location"
placeholder="Location"
value="<?= $airportLocation->location->name ?>"
data-id="<?= $airportLocation->locationId ?>"
data-value="<?= $airportLocation->location->name ?>">
</div>
</div>
<div class="d-flex justify-content-between mt-3">
<?php if ($airportLocationId): ?>
<button class="btn btn-outline-danger" onclick="$(document).trigger('buttonDelete:airportLocation', <?= $airportLocationId ?>)">Delete</button>
<?php endif; ?>
<button class="btn btn-outline-primary" onclick="$(document).trigger('buttonSave:airportLocation', '<?= $airportLocationId ?>')">Save</button>
</div>
<script>
$(async ƒ => {
function backToList() {
$(document).trigger('loadMainSection', {
sectionId: 'airportLocations',
url: 'section.list-airport-locations.php',
forceReload: true
});
}
buildAutoComplete({
selector: '_location',
apiUrl: '/api/get.autocomplete-locations.php',
searchFields: ['label', 'short_name'],
});
async function getData() {
const airportLocationId = $('#airport-location-id').val();
const data = {};
if (airportLocationId) data.id = airportLocationId;
data.airportId = $('#_airport').val();
data.airlineId = $('#_airline').val();
data.type = $('#_type').val();
data.locationId = $('#_location').data('id');
return data;
}
if (!documentEventExists('buttonSave:airportLocation')) {
$(document).on('buttonSave:airportLocation', async (e, id) => {
const airportLocationId = id;
const data = await getData();
const resp = await net.post('/api/post.save-airport-location.php', data);
if (resp?.result) {
$(document).trigger('airportLocationChange');
if (airportLocationId) {
ui.toastr.success('Airport Location saved.', 'Success');
return backToList();
}
ui.toastr.success('Airport Location added.', 'Success');
return backToList();
}
ui.toastr.error(resp.result.errors[2], 'Error');
console.error(resp);
});
}
if (!documentEventExists('buttonDelete:airportLocation')) {
$(document).on('buttonDelete:airportLocation', async (e, id) => {
const airportLocationId = id;
if (await ui.ask('Are you sure you want to delete this airport location?')) {
const resp = await net.get('/api/get.delete-airport-location.php', {
id: airportLocationId
});
if (resp?.result) {
$(document).trigger('airportLocationChange');
ui.toastr.success('Airport Location deleted.', 'Success');
return backToList();
}
console.error(resp);
ui.toastr.error('There seems to be a problem deleting airport location.', 'Error');
}
});
}
});
</script>