<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Imports\CommonImport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use App\Helpers\Helper;
use App\Models\CommonArt;

class CommonArtController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('Admin.commonArt.import');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    
    public function import(Request $request) 
    {   
        // dd($request);
        $validator = Validator::make($request->all(), [
            'employee_data' => 'required',
        ]);
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $excel = Excel::toArray(new CommonImport,request()->file('employee_data'));
        $excel = isset($excel[0])?$excel[0]:array();
        // dd($excel);
        
        $keyExcel = array_filter($excel[0]);
        $notFoundfiles = 0;
        
        if(count($excel) > 0){

            for ($i=1; $i < count($excel); $i++) { 
                
                $excelRow = $excel[$i];
                $filterRow = array();
                if(!is_null($excelRow)){ 

                    //find Image
                    $file = isset($excelRow[1])?$excelRow[1]:"";
                    // dump(file_exists(public_path("CommonArt/".$file)));
                    if(strpos($file, "://") !== false) {
                        // full url
                        $filterRow['name'] = isset($excelRow[0])?$excelRow[0]:"";
                        $filterRow['upload'] = isset($excelRow[1])?$excelRow[1]:"";

                        CommonArt::updateOrCreate(["name"=>$filterRow['name']], $filterRow);
                    }elseif(file_exists(public_path("CommonArt/".$file))){
                        $filterRow['name'] = isset($excelRow[0])?$excelRow[0]:"";
                        $filterRow['upload'] = isset($excelRow[1])?$excelRow[1]:"";

                        CommonArt::updateOrCreate(["name"=>$filterRow['name']], $filterRow);
                    }else{
                        $notFoundfiles++;
                    }

                }
            }
        }
        // dd($notFoundfiles);
        if($notFoundfiles > 0){
            Helper::successMsg("custom", "Common art Imported and some art files are not found, Failed : ".$notFoundfiles);
        }else{
            Helper::successMsg("custom", "Common Art Imported successfully");
        }
        
        return redirect()->route('common-art.index');
    }
}
