<?php

namespace App\Http\Controllers\EndoIndex;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Imports\icd9import;
use App\Imports\icd10import;
use App\Imports\billingimport;
use App\Imports\screening;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\User;

class ImportexcelController extends Controller
{
    // public function index(){
    //     return view('EndoINDEX.importexcel');
    // }
    public function index(){
        // return view('EndoINDEX.importexcel');
        Excel::import(new screening,"C:\\Users\\EndoIndex\\Documents\\temp\\patient.xlsx");
    }

    public function store(Request $r){
        if(isset($r->icd9)){
            Excel::import(new icd9import,$r->file('fileicd9')->store('temp'));
        }
        if(isset($r->icd10)){
            Excel::import(new icd10import,$r->file('fileicd10')->store('temp'));
        }
        if(isset($r->billing)){
            Excel::import(new billingimport,$r->file('filebilling')->store('temp'));
        }
        if(isset($r->user)){
            $file = $r->file('fileuser');
            if(isset($file)){
                $temppath = $file->getPathName();
                $user_data = Excel::toArray([], $temppath);
                foreach ($user_data[0] as $index=>$data) {
                    if($index != 0){
                        User::add_user_excel($data);
                    }
                }
            }
        }
        return redirect()->back();
    }
}
