# API Integration Complete

## ✅ API Configuration Updated

All API endpoints and authentication have been updated to match the actual backend API from `/lib/docs/api.sh`.

### 🔗 API Base URL
```
Base URL: https://dotnet.elidev.info:8157/ws
App ID: Minhthu2016
```

### 🔐 Authentication Updates

#### Headers Changed:
- ❌ Old: `Authorization: Bearer {token}`
- ✅ New: `AccessToken: {token}`
- ✅ Added: `AppID: Minhthu2016`

#### Login Request Format:
- ❌ Old fields: `username`, `password`
- ✅ New fields: `EmailPhone`, `Password`

### 📍 API Endpoints Updated

| Feature | Endpoint | Method | Notes |
|---------|----------|--------|-------|
| Login | `/PortalAuth/Login` | POST | EmailPhone + Password |
| Warehouses | `/portalWareHouse/search` | POST | Pagination params required |
| Products | `/portalProduct/getAllProduct` | GET | Returns all products |

## 🛠️ Files Modified

### 1. **app_constants.dart**
```dart
static const String apiBaseUrl = 'https://dotnet.elidev.info:8157/ws';
static const String appId = 'Minhthu2016';
```

### 2. **api_endpoints.dart**
```dart
static const String login = '/PortalAuth/Login';
static const String warehouses = '/portalWareHouse/search';
static const String products = '/portalProduct/getAllProduct';
```

### 3. **api_client.dart**
```dart
// Changed from Authorization: Bearer to AccessToken
options.headers['AccessToken'] = token;
options.headers['AppID'] = AppConstants.appId;
```

### 4. **login_request_model.dart**
```dart
Map<String, dynamic> toJson() {
  return {
    'EmailPhone': username,  // Changed from 'username'
    'Password': password,     // Changed from 'password'
  };
}
```

### 5. **warehouse_remote_datasource.dart**
```dart
// Changed from GET to POST with pagination
final response = await apiClient.post(
  '/portalWareHouse/search',
  data: {
    'pageIndex': 0,
    'pageSize': 100,
    'Name': null,
    'Code': null,
    'sortExpression': null,
    'sortDirection': null,
  },
);
```

### 6. **products_remote_datasource.dart**
```dart
// Updated to use correct endpoint
final response = await apiClient.get('/portalProduct/getAllProduct');
```

## 🎯 Pre-filled Test Credentials

The login form is pre-filled with test credentials:
- **Email**: `yesterday305@gmail.com`
- **Password**: `123456`

## 🚀 Ready to Test

The app is now configured to connect to the actual backend API. You can:

1. **Run the app**:
   ```bash
   flutter run
   ```

2. **Test the flow**:
   - Login with pre-filled credentials
   - View warehouses list
   - Select a warehouse
   - Choose Import or Export
   - View products

## 📝 API Request Examples

### Login Request:
```bash
POST https://dotnet.elidev.info:8157/ws/PortalAuth/Login
Headers:
  Content-Type: application/json
  AppID: Minhthu2016
Body:
{
  "EmailPhone": "yesterday305@gmail.com",
  "Password": "123456"
}
```

### Get Warehouses Request:
```bash
POST https://dotnet.elidev.info:8157/ws/portalWareHouse/search
Headers:
  Content-Type: application/json
  AppID: Minhthu2016
  AccessToken: {token_from_login}
Body:
{
  "pageIndex": 0,
  "pageSize": 100,
  "Name": null,
  "Code": null,
  "sortExpression": null,
  "sortDirection": null
}
```

### Get Products Request:
```bash
GET https://dotnet.elidev.info:8157/ws/portalProduct/getAllProduct
Headers:
  AppID: Minhthu2016
  AccessToken: {token_from_login}
```

## ⚠️ Important Notes

1. **HTTPS Certificate**: The API uses a self-signed certificate. You may need to handle SSL certificate validation in production.

2. **CORS**: Make sure CORS is properly configured on the backend for mobile apps.

3. **Token Storage**: Access tokens are securely stored using `flutter_secure_storage`.

4. **Error Handling**: All API errors are properly handled and displayed to users.

5. **Logging**: API requests and responses are logged in debug mode for troubleshooting.

## 🔍 Testing Checklist

- [ ] Login with test credentials works
- [ ] Access token is saved in secure storage
- [ ] Warehouses list loads successfully
- [ ] Warehouse selection works
- [ ] Navigation to operations page works
- [ ] Products list loads successfully
- [ ] All UI states work (loading, error, success, empty)
- [ ] Refresh functionality works
- [ ] Logout clears the token

## 🐛 Troubleshooting

### If login fails:
1. Check internet connection
2. Verify API is accessible at `https://dotnet.elidev.info:8157`
3. Check credentials are correct
4. Look at debug logs for detailed error messages

### If API calls fail after login:
1. Verify access token is being saved
2. Check that AccessToken and AppID headers are being sent
3. Verify token hasn't expired
4. Check API logs for detailed error information

## 📚 Related Files

- `/lib/docs/api.sh` - Original curl commands
- `/lib/core/constants/app_constants.dart` - API configuration
- `/lib/core/constants/api_endpoints.dart` - Endpoint definitions
- `/lib/core/network/api_client.dart` - HTTP client configuration
- `/lib/features/auth/data/models/login_request_model.dart` - Login request format

---

**Status**: ✅ Ready for testing with production API
**Last Updated**: $(date)
