74 lines
2.1 KiB
PHP
74 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Livewire;
|
||
|
|
|
||
|
|
use App\Models\PlatformAccount;
|
||
|
|
use App\Models\PlatformChannel;
|
||
|
|
use Livewire\Component;
|
||
|
|
|
||
|
|
class Channels extends Component
|
||
|
|
{
|
||
|
|
public ?int $managingChannelId = null;
|
||
|
|
|
||
|
|
public function toggle(int $channelId): void
|
||
|
|
{
|
||
|
|
$channel = PlatformChannel::findOrFail($channelId);
|
||
|
|
$channel->is_active = !$channel->is_active;
|
||
|
|
$channel->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function openAccountModal(int $channelId): void
|
||
|
|
{
|
||
|
|
$this->managingChannelId = $channelId;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function closeAccountModal(): void
|
||
|
|
{
|
||
|
|
$this->managingChannelId = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function attachAccount(int $accountId): void
|
||
|
|
{
|
||
|
|
if (!$this->managingChannelId) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$channel = PlatformChannel::findOrFail($this->managingChannelId);
|
||
|
|
|
||
|
|
if (!$channel->platformAccounts()->where('platform_account_id', $accountId)->exists()) {
|
||
|
|
$channel->platformAccounts()->attach($accountId, [
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 1,
|
||
|
|
'created_at' => now(),
|
||
|
|
'updated_at' => now(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function detachAccount(int $channelId, int $accountId): void
|
||
|
|
{
|
||
|
|
$channel = PlatformChannel::findOrFail($channelId);
|
||
|
|
$channel->platformAccounts()->detach($accountId);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
$channels = PlatformChannel::with(['platformInstance', 'platformAccounts'])->orderBy('name')->get();
|
||
|
|
$allAccounts = PlatformAccount::where('is_active', true)->get();
|
||
|
|
|
||
|
|
$managingChannel = $this->managingChannelId
|
||
|
|
? PlatformChannel::with('platformAccounts')->find($this->managingChannelId)
|
||
|
|
: null;
|
||
|
|
|
||
|
|
$availableAccounts = $managingChannel
|
||
|
|
? $allAccounts->filter(fn($account) => !$managingChannel->platformAccounts->contains('id', $account->id))
|
||
|
|
: collect();
|
||
|
|
|
||
|
|
return view('livewire.channels', [
|
||
|
|
'channels' => $channels,
|
||
|
|
'managingChannel' => $managingChannel,
|
||
|
|
'availableAccounts' => $availableAccounts,
|
||
|
|
])->layout('layouts.app');
|
||
|
|
}
|
||
|
|
}
|