Commit 5e0dca88 authored by Thidaporn Laisan's avatar Thidaporn Laisan
Browse files

update parameter

parent 27020835
......@@ -3,15 +3,25 @@
namespace App\Http\Livewire\Pages\Parameter;
use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\ConfParameter;
use App\Models\TabParameterInfo;
use App\Models\TabSpnpage;
use App\Models\TabParameterImg;
use App\Models\TabUser;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
class ParameterEdit extends Component
{
use WithFileUploads;
public $parameterId;
public $name, $value, $description, $active, $detail, $pageCode = [];
public $searchByPage = [];
public $images = [];
public $newImage;
public $imgdetail, $category = '1';
protected $rules = [
'name' => 'required|string|max:30',
......@@ -19,7 +29,10 @@ class ParameterEdit extends Component
'description' => 'required|string|max:100',
'active' => 'required',
'detail' => 'required',
'pageCode' => 'required|array|min:1',
'pageCode' => 'required|array',
'newImage' => 'nullable|image|max:1024',
'imgdetail' => 'nullable|string|max:255',
'category' => 'nullable|string|max:100',
];
public function mount($editPid)
......@@ -40,6 +53,7 @@ class ParameterEdit extends Component
$this->pageCode = [];
}
$this->searchByPage = TabSpnpage::select('pagecode', 'pagename')->get()->toArray();
$this->images = TabParameterImg::where('parameterCode', $parameter->name)->get();
}
public function updateParameter()
......@@ -58,7 +72,7 @@ class ParameterEdit extends Component
$parameterInfo = TabParameterInfo::where('parameterName', ConfParameter::where('PID', $this->parameterId)->value('name'))->first();
if ($parameterInfo) {
$parameterInfo->update([
'parameterName' => $this->name, // อัปเดตชื่อด้วย
'parameterName' => $this->name,
'pageCode' => $pageCodesString,
'detail' => $this->detail,
]);
......@@ -70,17 +84,55 @@ class ParameterEdit extends Component
]);
}
if ($this->newImage) {
$this->uploadImage();
}
session()->flash('message', 'Parameter updated successfully!');
$this->emit('loadPage', 'list');
}
public function render()
public function uploadImage()
{
return view('livewire.pages.parameter.parameter-edit');
}
}
$this->validate([
'newImage' => 'nullable|image|max:1024',
'category' => 'nullable|string|max:100',
]);
$user = Auth::user();
if (!$user) {
session()->flash('error', 'User not authenticated.');
return;
}
if ($this->newImage) {
$imagePath = base64_encode(file_get_contents($this->newImage->getRealPath()));
TabParameterImg::create([
'parameterCode' => $this->name,
'imgdetail' => $imagePath,
'category' => $this->category,
'uploadBy' => $user->USERNAME,
'uploadTime' => now(),
]);
$this->images = TabParameterImg::where('parameterCode', $this->name)->get();
$this->reset(['newImage', 'category']); // Reset the input fields
$this->emit('clearFileInput');
}
}
public function deleteImage($imgid)
{
$image = TabParameterImg::where('imgid', $imgid)->first();
if ($image) {
$image->delete();
$this->images = TabParameterImg::where('parameterCode', $this->name)->get();
}
}
public function render()
{
return view('livewire.pages.parameter.parameter-edit');
}
}
......@@ -6,6 +6,9 @@ use Illuminate\Database\Eloquent\Model;
class TabParameterImg extends Model
{
public $timestamps = false;
protected $primaryKey = 'imgid';
protected $table = 'tab_parameter_img';
protected $fillable = [
'imgid',
......
......@@ -3,7 +3,7 @@
class="btn mx-auto m-3 text-white bg-primary px-3 py-2">Back</button>
<div class="max-w-full mx-auto p-6 bg-gray-100">
<div class="w-full px-6 mb-12 flex justify-center">
<div class="w-full px-6 mb-12 flex">
<div class="p-12 bg-white shadow-md rounded-lg w-1/2">
<h2 class="text-2xl font-bold mb-4">Edit Parameter</h2>
......@@ -13,6 +13,11 @@
{{ session('message') }}
</div>
@endif
@if (session()->has('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
<form wire:submit.prevent="updateParameter">
<div class="mb-4">
......@@ -75,7 +80,67 @@
</div>
</form>
</div>
</div>
<div class="p-12 bg-white shadow-md rounded-lg w-1/2 ml-4">
<h2 class="text-2xl font-bold mb-4">Upload Image</h2>
<form wire:submit.prevent="uploadImage">
<div class="mb-4">
<label for="newImage" class="block text-sm font-medium text-gray-700">Image</label>
<input type="file" wire:model="newImage" id="newImage"
class="w-full mt-1 p-2 border border-gray-300 rounded-md">
@error('newImage') <span class="text-red-500">{{ $message }}</span> @enderror
</div>
<div class="mb-4">
<label for="category" class="block text-sm font-medium text-gray-700">Parameter</label>
<select wire:model.defer="category" id="category"
class="mt-1 p-2 border border-gray-300 rounded-md">
<option value="1">Enable</option>
<option value="2">Disable</option>
</select>
@error('category') <span class="text-red-500">{{ $message }}</span> @enderror
</div>
<div class="flex items-center justify-center">
<button type="submit"
class="bg-primary inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Upload
</button>
</div>
</form>
<div class="mt-6">
<h3 class="text-lg font-medium text-gray-700 mb-2">Uploaded Images</h3>
<div class="flex flex-col gap-4">
@foreach ($images as $image)
<div class="relative">
<img src="data:image/jpeg;base64,{{ $image->imgdetail }}" alt="Image" class="w-full h-auto rounded-md">
<div class="mt-2 text-sm text-gray-700">
<p><strong>Parameter:</strong> {{ $image->category == 1 ? 'Enable' : ($image->category == 2 ? 'Disable' : '') }}</p>
<p><strong>Uploaded By:</strong> {{ $image->uploadBy }}</p>
<p><strong>Uploaded Time:</strong> {{ $image->uploadTime }}</p>
</div>
<button type="button" wire:click="deleteImage({{ $image->imgid }})"
class="absolute top-1 right-1 bg-red-500 text-white p-1 rounded-full hover:bg-red-700">
<i class="fa fa-trash"></i>
</button>
</div>
@endforeach
</div>
</div>
</div>
</div>
<!-- JavaScript to clear file input -->
<script>
document.addEventListener('livewire:load', function () {
@this.on('clearFileInput', function () {
let input = document.getElementById('newImage');
if (input) {
input.value = null;
}
});
});
</script>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment