<?php

namespace App\Imports;

use DB;
use App\Models\Customer;
use Maatwebsite\Excel\Concerns\ToModel;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class CustomerImport implements ToModel
{
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function model(array $row)
    {
        $branch = \DB::table('branch')->where('cabang', $row[5])->value('id');
        $category = \DB::table('member_categories')->where('category', $row[6])->value('id');
        return new Customer([
            'nama' => $row[0],
            'email' => $row[1],
            'phone' => $row[2],
            'poin' => $row[3],
            'tgl_lahir' => Date::excelToDateTimeObject($row[4]),
            'id_branch' => $branch ?? 1,
            'category_id' => $category ?? 1,
            'created_at' => $row[7]
        ]);
    }
}
