diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 460ec35..e81430f 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -37,6 +37,7 @@ $routes->group('api', function ($routes) { $routes->get('entry/controls', 'Api\EntryApiController::getControls'); $routes->get('entry/tests', 'Api\EntryApiController::getTests'); + $routes->get('entry/monthly', 'Api\EntryApiController::getMonthlyData'); $routes->post('entry/daily', 'Api\EntryApiController::saveDaily'); $routes->post('entry/monthly', 'Api\EntryApiController::saveMonthly'); $routes->post('entry/comment', 'Api\EntryApiController::saveComment'); diff --git a/app/Controllers/Api/EntryApiController.php b/app/Controllers/Api/EntryApiController.php index 3e3ce13..a8d56c5 100644 --- a/app/Controllers/Api/EntryApiController.php +++ b/app/Controllers/Api/EntryApiController.php @@ -135,4 +135,33 @@ class EntryApiController extends BaseController return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } + + public function getMonthlyData() + { + $controlId = $this->request->getGet('controlId'); + $testId = $this->request->getGet('testId'); + $yearMonth = $this->request->getGet('yearMonth'); + + try { + $results = $this->resultModel->getByMonth($controlId, $testId, $yearMonth); + $comment = $this->commentModel->getByControlTestMonth($controlId, $testId, $yearMonth); + + $formValues = []; + foreach ($results as $row) { + $day = (int)date('j', strtotime($row['resdate'])); + $formValues[$day] = $row['resvalue']; + } + + return $this->respond([ + 'status' => 'success', + 'message' => 'fetch success', + 'data' => [ + 'formValues' => $formValues, + 'comment' => $comment ? $comment['comtext'] : '' + ] + ], 200); + } catch (\Exception $e) { + return $this->failServerError('Something went wrong: ' . $e->getMessage()); + } + } } diff --git a/app/Views/entry/monthly.php b/app/Views/entry/monthly.php index ab6cd19..9ab938d 100644 --- a/app/Views/entry/monthly.php +++ b/app/Views/entry/monthly.php @@ -1,6 +1,6 @@ extend("layout/main_layout"); ?> section("content") ?> -
+
@@ -59,7 +59,7 @@
- @@ -101,13 +101,19 @@
@@ -148,6 +154,7 @@ document.addEventListener('alpine:init', () => { errors: {}, error: '', formValues: {}, + comment: '', init() { this.loadDraft(); @@ -222,6 +229,29 @@ document.addEventListener('alpine:init', () => { return Object.keys(this.errors).length === 0; }, + async openEntry() { + this.formValues = {}; + this.comment = ''; + this.loading = true; + this.showEntry = true; + + try { + const response = await fetch(`${window.BASEURL}/api/entry/monthly?controlid=${this.control}&testid=${this.test}&yearmonth=${this.date}`); + const data = await response.json(); + if (data.status === 'success') { + this.formValues = data.data.formValues || {}; + this.comment = data.data.comment || ''; + } else { + this.error = data.message || 'Failed to load existing data'; + } + } catch (error) { + console.error(error); + this.error = 'Failed to load existing data'; + } finally { + this.loading = false; + } + }, + async saveData() { if (!this.validate()) { App.showToast('Please fill all required fields', 'error'); @@ -247,8 +277,8 @@ document.addEventListener('alpine:init', () => { } } - if (!hasData) { - App.showToast('Please enter at least one value', 'warning'); + if (!hasData && !this.comment) { + App.showToast('Please enter at least one value or comment', 'warning'); this.loading = false; return; } @@ -260,9 +290,13 @@ document.addEventListener('alpine:init', () => { }); const data = await response.json(); if (data.status === 'success') { + if (this.comment) { + await this.saveComment(); + } App.showToast('Data saved successfully!'); this.showEntry = false; this.formValues = {}; + this.comment = ''; this.saveDraft(); } else { this.error = data.message || 'Failed to save data'; @@ -275,6 +309,23 @@ document.addEventListener('alpine:init', () => { } }, + async saveComment() { + const formData = new FormData(); + formData.append('controlid', this.control); + formData.append('testid', this.test); + formData.append('commonth', this.date); + formData.append('comtext', this.comment); + + try { + await fetch(`${window.BASEURL}/api/entry/comment`, { + method: 'POST', + body: formData + }); + } catch (error) { + console.error('Failed to save comment:', error); + } + }, + saveDraft() { localStorage.setItem('monthlyEntry', JSON.stringify({ dept: this.dept,