# TinyLab View Rules This document defines the view patterns and conventions used in TinyLab. Follow these rules when creating new views or modifying existing ones. ## 1. Directory Structure ``` app/Views/ ├── layout/ │ ├── main_layout.php # Primary app layout (sidebar, navbar, dark mode) │ └── form_layout.php # Lightweight layout for standalone forms ├── [module_name]/ # Feature module (e.g., patients, requests) │ ├── index.php # Main list page │ ├── dialog_form.php # Modal form (Create/Edit) │ └── drawer_filter.php # Filter drawer (optional) ├── master/ │ └── [entity]/ # Master data entity (e.g., doctors, samples) │ ├── index.php │ └── dialog_form.php └── errors/ └── html/ ├── error_404.php └── error_exception.php ``` ## 2. Layout Extension All pages must extend the appropriate layout: ```php extend("layout/main_layout"); ?> section("content") ?> endSection(); ?> section("script") ?> endSection(); ?> ``` Use `form_layout.php` for standalone pages like login: ```php extend("layout/form_layout"); ?> ``` ## 3. Alpine.js Integration ### 3.1 Component Definition Always use `alpine:init` event listener: ```php section("script") ?> endSection(); ?> ``` ### 3.2 Alpine Data Attribute Wrap page content in the component: ```php
``` ## 4. Modal/Dialog Pattern ### 4.1 Standard Modal Structure ```php

``` ## 5. Page Structure Pattern ### 5.1 Standard CRUD Page ```php extend("layout/main_layout"); ?> section("content") ?>

Module Title

Module description

include('module/dialog_form'); ?>
endSection(); ?> ``` ### 5.2 Master-Detail Pattern ```php
``` ## 6. Form Field Patterns ### 6.1 Input with Icon ```php
``` ### 6.2 Select Dropdown ```php ``` ### 6.3 Checkbox ```php ``` ### 6.4 Radio Group ```php
``` ## 7. Status Badges ```php ``` ## 8. CSS Classes Reference ### Layout - `flex`, `flex-col`, `grid`, `grid-cols-2`, `gap-4` - `w-full`, `max-w-2xl`, `min-h-0` - `overflow-hidden`, `overflow-y-auto` ### Spacing - `p-4`, `px-6`, `py-3`, `m-4`, `mb-6` - `space-x-4`, `gap-3` ### Typography - `text-sm`, `text-xs`, `text-lg`, `text-2xl` - `font-medium`, `font-semibold`, `font-bold` - `text-slate-500`, `text-slate-800` ### Borders & Backgrounds - `bg-white`, `bg-slate-50`, `bg-blue-50` - `border`, `border-slate-100`, `border-red-300` - `rounded-lg`, `rounded-xl`, `rounded-2xl` ### Components - `btn btn-primary`, `btn btn-ghost` - `checkbox checkbox-sm` - `radio radio-primary` - `input`, `select`, `textarea` ### Icons FontAwesome 7 via CDN: `` ## 9. API Response Format Views expect API responses in this format: ```json { "status": "success", "message": "Operation successful", "data": [...] } ``` ## 10. Validation Pattern ```javascript validate() { this.errors = {}; if (!this.form.field1) { this.errors.field1 = 'Field 1 is required'; } if (!this.form.field2) { this.errors.field2 = 'Field 2 is required'; } if (this.form.field3 && !/^\d+$/.test(this.form.field3)) { this.errors.field3 = 'Must be numeric'; } return Object.keys(this.errors).length === 0; } ``` ## 11. Error Handling ```javascript async save() { this.loading = true; try { const res = await fetch(url, { ... }); const data = await res.json(); if (data.status === 'success') { this.showToast('Saved successfully'); this.fetchList(); } else { this.error = data.message || 'An error occurred'; } } catch (err) { this.error = 'Network error. Please try again.'; console.error(err); } finally { this.loading = false; } } ``` ## 12. Quick Reference: File Templates ### Standard List Page Template See: `app/Views/master/doctors/doctors_index.php` ### Modal Form Template See: `app/Views/master/doctors/dialog_doctors_form.php` ### Master-Detail Page Template See: `app/Views/patients/patients_index.php` ### Result Entry Table Template See: `app/Views/requests/dialog_result_entry.php` ## 13. Checklist for New Views - [ ] Create view file in correct directory - [ ] Extend appropriate layout (`main_layout` or `form_layout`) - [ ] Define `content` and `script` sections - [ ] Wrap content in `x-data` component - [ ] Include modal form if needed - [ ] Use DaisyUI components for buttons, inputs, selects - [ ] Add loading and empty states - [ ] Implement proper error handling - [ ] Use FontAwesome icons consistently - [ ] Follow naming conventions (snake_case for PHP, camelCase for JS) ## 14. Conventions Summary | Aspect | Convention | |--------|------------| | File naming | snake_case (e.g., `patients_index.php`) | | PHP variables | snake_case | | JavaScript variables | camelCase | | HTML classes | kebab-case (Tailwind) | | Icons | FontAwesome 7 | | UI Framework | TailwindCSS + DaisyUI | | State management | Alpine.js | | API format | JSON with `status`, `message`, `data` | | Primary key | `{table}_id` (e.g., `pat_id`) | | Soft deletes | All tables use `deleted_at` |