<?php

namespace Koneko\VuexyAdminModule\App\Http\Controllers;

use Koneko\VuexyAdminModule\App\Imports\SatCatalogsImport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

use App\Http\Controllers\Controller;

class SatCatalogController extends Controller
{

    /**
     * @return \Illuminate\Support\Collection
     */
    public function import(Request $request)
    {
        // Si es una solicitud POST, procesa la importación
        if ($request->isMethod('post')) {
            $validator = Validator::make($request->all(), [
                'file' => ['required', 'mimes:xls,xlsx'],
            ]);

            if ($validator->fails())
                return back()->withInput()->withErrors($validator);

            $time_start = microtime(true);
            $results    = [
                'details'       => [],
                'total_rows'    => 0,
                'total_created' => 0,
                'total_updated' => 0,
                'total_time'    => 0,
            ];

            $import   = new SatCatalogsImport();
            $filePath = request()->file('file');


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_FormaPago');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Forma de pago',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_Moneda');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Moneda',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_CodigoPostal_Parte_1');
            Excel::import($import, $filePath);
            $import->onlySheets('c_CodigoPostal_Parte_2');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Código postal',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_RegimenFiscal');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Regimen fiscal',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_Pais');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'País',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_UsoCFDI');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Uso de CFDI',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('C_Colonia_1');
            Excel::import($import, $filePath);
            $time_sheet_start = microtime(true);
            $import->onlySheets('C_Colonia_2');
            Excel::import($import, $filePath);
            $time_sheet_start = microtime(true);
            $import->onlySheets('C_Colonia_3');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Colonia',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('c_Estado');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Estado',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('C_Localidad');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Localidad',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $time_sheet_start = microtime(true);
            $import->onlySheets('C_Municipio');
            Excel::import($import, $filePath);
            $results['details'][] = [
                'name' => 'Municipio',
                'time' => round(microtime(true) - $time_sheet_start, 2),
                'created' => session('created'),
                'updated' => session('updated'),
            ];
            $results['total_rows'] += session('created') + session('updated');
            $results['total_created'] += session('created');
            $results['total_updated'] += session('updated');


            $results['total_time'] = round(microtime(true) - $time_start, 2);

            return back()->with('results', $results);
        }

        return view('system::import-sat.index');
    }
}
