215 lines
9.5 KiB
PHP
215 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use App\Services\ApiService;
|
|
use App\Services\CookieService;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class Create extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public $logo = null;
|
|
public $gps = '';
|
|
public $contact_email_address_mobile = '';
|
|
public $contact_number_mobile = '';
|
|
public $contact_details = '';
|
|
public $information_guide_details = '';
|
|
public $android_version = '';
|
|
public $android_update_message = '';
|
|
public $android_action = '';
|
|
public $ios_version = '';
|
|
public $ios_update_message = '';
|
|
public $ios_action = '';
|
|
public $systemPreference = null;
|
|
public $loading = false;
|
|
public $mounted = false;
|
|
public $errors = [];
|
|
|
|
public $actionAndroidTypeOptions = [
|
|
['label' => 'Update', 'value' => '0'],
|
|
['label' => 'Force Update', 'value' => '1'],
|
|
['label' => 'Silent Update', 'value' => '2'],
|
|
];
|
|
public $actionIosTypeOptions = [
|
|
['label' => 'Update', 'value' => '0'],
|
|
['label' => 'Force Update', 'value' => '1'],
|
|
['label' => 'Silent Update', 'value' => '2'],
|
|
];
|
|
|
|
protected $apiService;
|
|
protected $cookieService;
|
|
|
|
public function boot(ApiService $apiService, CookieService $cookieService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
$this->cookieService = $cookieService;
|
|
}
|
|
|
|
public function mount()
|
|
{
|
|
// Check role-based access
|
|
$userInfo = Session::get('userInfo');
|
|
if (!$userInfo || $userInfo['role'] != 1) {
|
|
return redirect()->route('404');
|
|
}
|
|
|
|
$this->loading = true;
|
|
|
|
try {
|
|
$response = $this->apiService->get('systemPreference');
|
|
if ($response && isset($response['data'])) {
|
|
$this->systemPreference = $response['data'];
|
|
$this->gps = $this->systemPreference['gps'] ?? '';
|
|
$this->contact_email_address_mobile = $this->systemPreference['contact_email_address_mobile'] ?? '';
|
|
$this->contact_number_mobile = $this->systemPreference['contact_number_mobile'] ?? '';
|
|
$this->contact_details = $this->systemPreference['contact_details'] ?? '';
|
|
$this->information_guide_details = $this->systemPreference['information_guide_details'] ?? '';
|
|
$this->android_version = $this->systemPreference['android_version'] ?? '';
|
|
$this->android_update_message = $this->systemPreference['android_update_message'] ?? '';
|
|
$this->android_action = (string)($this->systemPreference['android_action'] ?? '');
|
|
$this->ios_version = $this->systemPreference['ios_version'] ?? '';
|
|
$this->ios_update_message = $this->systemPreference['ios_update_message'] ?? '';
|
|
$this->ios_action = (string)($this->systemPreference['ios_action'] ?? '');
|
|
$this->mounted = true;
|
|
Log::info('System preferences loaded', ['source' => 'SystemPreferencesCreate']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Session::flash('error', 'Something went wrong loading data: ' . $e->getMessage());
|
|
Log::error('Failed to load system preferences', ['error' => $e->getMessage(), 'source' => 'SystemPreferencesCreate']);
|
|
$this->mounted = false;
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$this->loading = true;
|
|
$this->errors = [];
|
|
|
|
try {
|
|
$this->validate([
|
|
'logo' => ['nullable', 'image', 'mimes:jpg,png', 'max:2048'], // 2MB max
|
|
'gps' => ['required', 'max:128'],
|
|
'contact_email_address_mobile' => ['required', 'email', 'max:128'],
|
|
'contact_number_mobile' => ['required'],
|
|
'contact_details' => ['required', 'email', 'max:128'],
|
|
'information_guide_details' => ['nullable', 'max:100'],
|
|
'android_version' => ['nullable', 'max:20'],
|
|
'android_update_message' => ['nullable', 'max:100'],
|
|
'android_action' => ['nullable'],
|
|
'ios_version' => ['nullable', 'max:20'],
|
|
'ios_update_message' => ['nullable', 'max:100'],
|
|
'ios_action' => ['nullable'],
|
|
], [
|
|
'logo.image' => 'Logo must be an image.',
|
|
'logo.mimes' => 'Logo must be a JPG or PNG file.',
|
|
'logo.max' => 'Logo must not exceed 2MB.',
|
|
'gps.required' => 'GPS Radius is required!',
|
|
'gps.max' => 'Maximum character is 128.',
|
|
'contact_email_address_mobile.required' => 'Contact Email Address is required!',
|
|
'contact_email_address_mobile.email' => 'Invalid Contact Email Address.',
|
|
'contact_email_address_mobile.max' => 'Maximum character is 128.',
|
|
'contact_number_mobile.required' => 'Contact Number is required!',
|
|
'contact_details.required' => 'Contact Details is required!',
|
|
'contact_details.email' => 'Invalid Contact Details.',
|
|
'contact_details.max' => 'Maximum character is 128.',
|
|
'information_guide_details.max' => 'Maximum character is 100.',
|
|
'android_version.max' => 'Maximum character is 20.',
|
|
'android_update_message.max' => 'Maximum character is 100.',
|
|
'ios_version.max' => 'Maximum character is 20.',
|
|
'ios_update_message.max' => 'Maximum character is 100.',
|
|
]);
|
|
|
|
$formData = [
|
|
'gps' => $this->gps,
|
|
'contact_email_address_mobile' => $this->contact_email_address_mobile,
|
|
'contact_number_mobile' => str_replace(' ', '', $this->contact_number_mobile),
|
|
'contact_details' => $this->contact_details,
|
|
'information_guide_details' => $this->information_guide_details,
|
|
'android_version' => $this->android_version,
|
|
'android_update_message' => $this->android_update_message,
|
|
'android_action' => $this->android_action,
|
|
'ios_version' => $this->ios_version,
|
|
'ios_update_message' => $this->ios_update_message,
|
|
'ios_action' => $this->ios_action,
|
|
];
|
|
|
|
if ($this->logo) {
|
|
$formData['logo'] = $this->logo;
|
|
}
|
|
|
|
$response = $this->apiService->postMultipart('systemPreference', $formData);
|
|
|
|
if ($response && isset($response['status']) && $response['status'] === 200) {
|
|
Session::flash('success', 'Record was successfully updated.');
|
|
Log::info('System preferences updated successfully', ['source' => 'SystemPreferencesCreate']);
|
|
}
|
|
} catch (\Illuminate\Validation\ValidationException $e) {
|
|
$this->errors = $e->errors();
|
|
Session::flash('error', 'Something went wrong creating new record: ' . implode(', ', array_merge(...array_values($e->errors()))));
|
|
Log::error('Validation failed', ['errors' => $e->errors(), 'source' => 'SystemPreferencesCreate']);
|
|
} catch (\Exception $e) {
|
|
$errorMessage = 'Something went wrong creating new record.';
|
|
if ($e->getCode() === 422) {
|
|
$errors = json_decode($e->getMessage(), true)['data'] ?? [];
|
|
if (!empty($errors['contact_email_address_mobile'])) {
|
|
$errorMessage .= ' ' . $errors['contact_email_address_mobile'][0];
|
|
}
|
|
if (!empty($errors['contact_number_mobile'])) {
|
|
$errorMessage .= ' ' . $errors['contact_number_mobile'][0];
|
|
}
|
|
} else {
|
|
$errorMessage .= ' ' . $e->getMessage();
|
|
}
|
|
Session::flash('error', $errorMessage);
|
|
Log::error('Failed to update system preferences', ['error' => $e->getMessage(), 'source' => 'SystemPreferencesCreate']);
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function syncStratuscast()
|
|
{
|
|
$this->loading = true;
|
|
|
|
try {
|
|
$token = $this->cookieService->getCookie('TOKEN')['token'] ?? '';
|
|
$syncApiBaseUrl = env('SYNC_API_BASE_URL', 'https://your-sync-api.com/api/'); // Add to .env
|
|
|
|
// Sync stations
|
|
$response = Http::get("{$syncApiBaseUrl}syncstations");
|
|
if ($response->successful()) {
|
|
Session::flash('success', 'Successfully Sync Data.');
|
|
} else {
|
|
throw new \Exception('Failed to sync stations.');
|
|
}
|
|
|
|
// Sync cities
|
|
$response = Http::withHeaders(['Authorization' => "Bearer {$token}"])->get("{$syncApiBaseUrl}synccities");
|
|
if ($response->successful()) {
|
|
Session::flash('success', 'Successfully Sync Cities.');
|
|
} else {
|
|
throw new \Exception('Failed to sync cities.');
|
|
}
|
|
|
|
Log::info('Stratuscast sync successful', ['source' => 'SystemPreferencesCreate']);
|
|
} catch (\Exception $e) {
|
|
Session::flash('error', 'Something went wrong. Please try again: ' . $e->getMessage());
|
|
Log::error('Stratuscast sync failed', ['error' => $e->getMessage(), 'source' => 'SystemPreferencesCreate']);
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.system-preferences.create')->layout('layouts.app', ['title' => 'System Parameters']);
|
|
}
|
|
} |