<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\User;
use Crypt;
use Hash;

class UsersImport implements ToCollection
{
    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
      $test =0;
      $mycnic = 1000000;
      $mycontact = 1000000;
      foreach($collection as $rows)
      {

        if($test > 0)
        {
          $mycnic= $mycnic+10;
          $mycontact= $mycontact+1;
        $user = new User();
        $user->branch_id = 1;
        $user->vendor_type_id = 1;
        $user->name = $rows[2];
        $user->fname = $rows[2];
        $user->relation = "S/O";
        $user->gender = "Male";
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
    for ($i = 0; $i <6; $i++) {
        $index = rand(0, strlen($characters) - 1);
        $randomString .= $characters[$index];
    }

        $user->email = $randomString."@mail.com";
        $user->password = Hash::make('12345678');
        $user->cnic = "00000-".$mycnic."-0";
        $user->contact = "0000-".$mycontact;
        $user->type = "Supplier";
        $user->status = "Active";
        $user->save();
        }
        $test++;
      }
        return 1;
    }
}
