<?php

namespace App\Imports;

use App\Models\CompteComptable;
use App\Models\Test;
use Maatwebsite\Excel\Concerns\ToModel;
// use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\RemembersRowNumber;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Support\Facades\DB;

class CompteComptableImport implements ToModel, WithHeadingRow 
{   

    use RemembersRowNumber;
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        
       // dd($row);
        $data = array(
            'num_compte'  => $row['compte'],
            'type_compte' => $row['nature'],
            'nom_compte'  => $row['intitule'],
            'id_app'      => get_id_app(),
            'user'        => get_pseudo()
        );

       // dd($this->getRowNumber());


        $response = DB::select('call CPT_SAVE_COMPTE_COMPTABLE(:num_compte,:nom_compte,:type_compte,:id_app,:user)'
        , $data);

        if(isset($response[0]->Message) && $response[0]->Message <> 1){
             dd('ligne n°'.$this->getRowNumber().' '.$response[0]->Message.' '.$row['compte']) ;
        }
       

        return new Test() ;
    }
}
