<?php

namespace App\Livewire\Modals;

use App\Models\Branch;
use Livewire\Component;
use App\Jobs\ExportSqliteBranchJob;
use App\Jobs\ExportSqliteSongJob;
use App\Jobs\ExportSqliteFavoriteJob;
use App\Jobs\ExportSqliteTextAdJob;
use App\Jobs\ExportSqliteUserJob;
use WireUi\Traits\WireUiActions;

class BranchSyncModal extends Component
{
    use WireUiActions;

    protected $listeners = [
        'openModal','closeModal'
    ];

    public int $branch_id;
    public string $branch_name;
    public bool $syncBranch = false;
    public bool $syncSong = false;
    public bool $syncFavorite = false;
    public bool $syncTextAds = false;
    public bool $syncUser = false;

    public bool $showModal = false;

    public function openModal($branch_id)
    {
        $this->branch_id = $branch_id;
        $this->branch_name = Branch::find($branch_id)->name;
        $this->reset('syncBranch', 'syncSong', 'syncFavorite', 'syncTextAds', 'syncUser');
        $this->showModal = true;
    }
    public function closeModal()
    {
        $this->showModal = false;
    }

    public function save()
    {
        if ($this->syncBranch) {
            ExportSqliteBranchJob::dispatch($this->branch_id);
        }
        if ($this->syncSong) {
            ExportSqliteSongJob::dispatch($this->branch_id);
        }
        if ($this->syncFavorite) {
            ExportSqliteFavoriteJob::dispatch($this->branch_id);
        }
        if($this->syncTextAds){
            ExportSqliteTextAdJob::dispatch($this->branch_id);
        }
        if ($this->syncUser) {
            ExportSqliteUserJob::dispatch(true, $this->branch_id);
        }

        $this->notification()->send([
            'icon' => 'success',
            'title' => '同步任務',
            'description' => '已加入排程',
        ]);

        $this->showModal = false;

    }

    public function render()
    {
        return view('livewire.modals.branch-sync-modal');
    }
}
