'delete', 'update' => 'update', 'reload' => 'reload', ]; public function render() { return view('writeoff::livewire.branch.branch-modal'); } public function submit() { $this->validate(); // Validate the form input data DB::transaction(function () { // Prepare the data for creating a new user $data = [ 'kode' => $this->kode, 'name' => $this->name, 'status' => $this->status ? 1 : 0, ]; if ($this->edit_mode) { // Emit a success event with a message $branch = Branch::find($this->id); $data['updated_by'] = auth()->user()->id; $data['updated_at'] = now(); $approval = [ 'method' => 'update', 'menu' => 'Parameter Cabang', 'old_request' => json_encode($branch), 'new_request' => json_encode($data), 'description' => 'Update Parameter Cabang', 'status' => '0', 'ref' => $this->kode ]; $is_approval = Approval::where('menu', 'Parameter Cabang') ->where('ref', $this->kode) ->where('status', '0') ->where('method', 'update') ->get() ->first(); if ($is_approval) { $this->dispatch('error', 'Data Sedang Menunggu Approval'); } else { Approval::create($approval); $this->dispatch('success', 'Data Berhasil Di Update, Menunggu Approval'); } } else { // Emit a success event with a message $data['created_by'] = auth()->user()->id; $data['created_at'] = now(); $approval = [ 'method' => 'create', 'menu' => 'Parameter Cabang', 'new_request' => json_encode($data), 'description' => 'Create Parameter Cabang', 'status' => '0', 'ref' => $this->kode ]; $is_approval = Approval::where('menu', 'Parameter Cabang') ->where('ref', $this->kode) ->where('status', '0') ->where('method', 'create') ->get() ->first(); if ($is_approval) { $this->dispatch('error', 'Data Sedang Menunggu Approval'); $this->reset(); } else { Approval::create($approval); $this->dispatch('success', 'Data Berhasil Di Input, Menunggu Approval'); } } }); // Reset the form fields after successful submission $this->reset(); } public function update($id) { $this->edit_mode = true; $branch = Branch::find($id); $this->id = $branch->id; $this->kode = $branch->kode; $this->name = $branch->name; $this->status = $branch->status ? true : false; } public function reload() { $this->edit_mode = false; $this->reset(); } public function delete($id) { $branch = Branch::find($id); $old = $branch; unset($old->deleted_at); unset($old->deleted_by); $new = Branch::find($id); $new->deleted_by = auth()->user()->id; $new->deleted_at = now(); $approval = [ 'method' => 'delete', 'menu' => 'Parameter Cabang', 'old_request' => json_encode($old), 'new_request' => json_encode($new), 'description' => 'Delete Parameter Cabang', 'status' => '0', 'ref' => $branch->kode ]; $is_approval = Approval::where('menu', 'Parameter Cabang') ->where('ref', $branch->kode) ->where('status', '0') ->where('method', 'delete') ->get() ->first(); if ($is_approval) { $this->dispatch('error', 'Data Sedang Menunggu Approval'); } else { Approval::create($approval); $this->dispatch('success', 'Data Berhasil Di Hapus, Menunggu Approval'); } } public function hydrate() { $this->resetErrorBag(); $this->resetValidation(); } protected function rules() { if ($this->edit_mode) { $request = new UpdateBranchRequest(); } else { $request = new StoreBranchRequest(); } return $request->rules(); } }