<?php

namespace App\Admin\Imports;

use App\Models\User as UserModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class User implements ToCollection
{

    public function collection(Collection $rows)
    {

        $error = [];

        foreach ($rows as $row) {
            $user = UserModel::whereHas('info', function ($q) use ($row) {
                $q->where('nickname', $row[1]);
            })->whereNull('PaOutletId')->first();

            if ($user) {
                $user->PaOutletId = $row[2];
                $user->save();
            } else {
                $error[] = $row['1'];
            }
        }

        if (count($error) > 0) {
            dd($error);

            return implode(' ,', $error);
        }

        return true;
    }

}
