# Lesotho Animal Census & Livestock Tracking MVP

## What is included
- PHP/MySQL REST API for Android WebView and web dashboard
- Offline-first mobile capture page using browser/WebView local storage
- Sync endpoint that uploads queued JSON records into SQL
- Dashboard with district reporting and Google Map points
- SQL schema with Lesotho districts, animal types and demo user
- Android WebView shell skeleton

## Deploy steps
1. Create database: `mysql -u root -p < sql/schema.sql`
2. Update `config.php` database credentials.
3. Replace `GOOGLE_MAPS_API_KEY` in `config.php`.
4. Host folder on Apache/Nginx with PHP 8+.
5. Open `/login.php`.
6. Demo login: `admin@demo.local` / `Admin@12345`.

## API endpoints
- `POST api/login.php`
- `GET api/bootstrap.php`
- `POST api/sync_upload.php`
- `GET api/reports_summary.php`
- `GET api/map_points.php`

## Android WebView
Change `START_URL` in `MainActivity.java` to your HTTPS hosted `/login.php` URL.

## Sync debug update
This version adds:
- Visible per-record sync results on `capture.php`
- Local offline queue viewer from `localStorage.pending_records`
- Server-side `sync_error_logs` table
- `api/sync_errors.php` endpoint for recent sync failures
- Better sync validation messages for district, village, GPS and animal type errors

### Apply patch to an existing database
Run:

```bash
mysql -u root -p < sql/patch_sync_error_logs.sql
```

### Does offline storage keep failed records?
Yes. Records are stored on the Android WebView/browser in:

```text
localStorage.pending_records
```

When sync succeeds, records are removed from the queue. When sync fails, the failed record remains in the queue and now stores `last_sync_error` for display.
