<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Maatwebsite\Excel\Facades\Excel;
use Livewire\WithFileUploads;
use App\BallotingMapFile;
use Maatwebsite\Excel\Sheet;
use App\Events\ImportProgressUpdated;
 
use App\Imports\ImportMapFileData;
 

class ImportExcel extends Component
{
    use WithFileUploads;

    public $progress = 0;
    public $file;
    public $mapfile_id;
    // protected $listeners = ['fileUploaded' => 'importData'];
    // protected $listeners = ['ImportProgressUpdated' => 'setProgress'];
    // protected $listeners = ['updateProgress' => 'updateProgress'];
    // protected $listeners = ['updateProgress' => 'handleUpdateProgress'];
    // protected $listeners = ['refreshComponent' => '$refresh'];
    protected $listeners = [
        'reportFilterUpdated' => 'handleUpdateProgress',
        'eventListened' => 'eventListened',
        'child-value-updated' => 'onChildValueUpdated',
        
        
    ];
    public $ballotingFileId;

public function mount($ballotingFileId)
{
    $this->ballotingFileId = $ballotingFileId;
}
    public function onChildValueUpdated($value)
    {
        dd('test');
        $this->progress = $value;
    }



    
    public function handle($progress)
    {
        //
        // $this->progress =$progress->percentage;
        // $this->emit('importProgressUpdate',$this->progress);
        //   $this->progress = 2;
           $this->emit('reportFilterUpdated');
        // exit;
        //  $this->emit('refreshComponent');
       
        
       
    }
    public function handleUpdateProgress($a)
{
       
    $this->progress = 100;
    

         
}
    public function setProgress($progress)
    {
        $this->progress = $progress;
        dd($progress);
       
    }
    public function updatedProgress($value)
{
    $this->progress = $value;
}
   
    // public function import()
    // {

    //     $balloting_file = BallotingMapFile::where('id',$this->mapfile_id)->first();

        
       
        

    //     Excel::import(new ImportMapFileData($balloting_file->id), storage_path('app/' . $balloting_file->path));
    //     // reset the progress
    //     $this->progress = 0;
    // }
    public function import()
    {
          
         // Get the total row count
        //  $this->emit('importProgressUpdate',$this->progress);
       
        $balloting_file = BallotingMapFile::where('id', $this->ballotingFileId )->first();
    //  $import =   new ImportMapFileData($balloting_file->id,0);
    //     $total_rows = $import->getTotalRows();
   
    $data = Excel::toArray([], storage_path('app/' . $balloting_file->path))[0];
      $total_row=sizeof($data);
        $path = storage_path('app/' . $balloting_file->path);
        
        // Excel::import(new ImportMapFileData($balloting_file->id,$total_row), storage_path('app/' . $balloting_file->path));
        // $balloting_file->publish = 1;
        // $balloting_file->save();
        // $this->emit('importComplete');

        // Reset the progress bar
        $this->progress = 100;
          $this->emit('show-swal',[
              'title' => 'Success!',
                'text' => 'Processing completed successfully!',
                'icon' => 'success'
            ]);
        //   $this->dispatchBrowserEvent('show-swal', [
        //     'title' => 'Success!',
        //     'text' => 'Processing completed successfully!',
        //     'icon' => 'success'
        // ]);
        //   return redirect()
        //                 ->route('balloting.setting.files')
        //                 ->with([
        //                     'message' => 'Map published successfully!',
        //                     'alert-type' => 'success'
        // ]);
    }
    public function render()
    {
        $ballotingFile = BallotingMapFile::find($this->ballotingFileId);

        return view('livewire.import-excel',[
            'ballotingFile' => $ballotingFile,
        ]);
    }
    // public function updateProgress($current_row, $total_rows)
    // {
    //     // Calculate the progress percentage
    //     $this->progress = round($current_row / $total_rows * 100);
    // }
    
    public function updateProgress($p)
    {
        // Calculate the progress percentage
        $this->progress = $p;
        dd($p);
        
    }
    
}
