CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use Livewire\Component;
use App\Models\AccountType;
use Livewire\WithPagination;
class AccountTypeLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $account_type, $data;
public $modelid;
public $status=false;
public $showmodal = false;
public $successmsg = "The Account Type has been saved successfully.";
public $updatemsg = "The Account Type has been updated successfully.";
public $deletemsg = "The Account Type data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Account Type name has already exists in our database. Please Try Another Name";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'account_type' => 'required|max:25',
];
public $updatedrules = [
'account_type' => 'required',
'account_type' => 'required|max:25',
'status' => 'required',
];
public $messages = [
'account_type.required' => 'This field is required',
'account_type.max' => 'This field allows 25 charactr maximum',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->resetValidation();
$this->showmodal = true;
}
public function resetvalid()
{
$this->resetValidation();
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelid = $id;
$this->loadData($this->modelid);
$this->showmodal = true;
}
public function deleteclick($id)
{
$this->modelid = $id;
$this->dispatchBrowserEvent('confirmdelete', [
'message' => 'Are you sure want to delete this data ?',
'funcname' => 'delete'
]);
}
public function cleanVar()
{
$this->modelid = null;
$this->account_type = null;
$this->status = null;
$this->showmodal = false;
}
public function loadData($id)
{
$response = AccountType::find($id);
if ($response) {
$this->account_type = $response['account_type'];
$this->status = $response['status'] ==1?true:false;
}
}
public function submit()
{
$this->validate($this->rules, $this->messages);
$response = AccountType::where('account_type', $this->account_type)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->duplicateerrormsg
]);
} else {
$response = AccountType::create([
'account_type' => $this->account_type,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$response =AccountType::find($this->modelid);
if ($response) {
$response->update([
'account_type' => $this->account_type,
'status'=> $this->status == false?0:1,
]);
//dd($response);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
$this->render();
} else {
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = AccountType::find($this->modelid);
if ($response) {
if ($response->delete()) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
$this->render();
} else {
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
$cash_types = AccountType::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $cash_types;
}
public function render()
{
$datatable=$this->read();
return view('livewire.finance.account-type-livewire',compact('datatable'));
}
}