<?php

namespace App\Imports;

use App\Models\Data;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
// use PhpOffice\PhpSpreadsheet\Shared\Date;
use App\Helpers\Phone;

class DataImport implements ToModel, WithBatchInserts, WithChunkReading
{
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    protected $sname;
    private $rows = 0;

    public function __construct($sname)
    {
        $this->sname = $sname;
    }

    public function model(array $row)
    {
        ++$this->rows;
        // if (!isset($row[1])) {
        //     $date = "";
        // } else {
        //     $date = Date::excelToDateTimeObject($row[1])->format('Y-m-d');
        // }

        return new Data([
            'phone' => $row[0],
            'service' => $this->sname,
            'operator' => Phone::getOperator($row[0]),
            'amount' => ((count($row) > 1) ? Phone::getAmount($row[1]) : 0),
            'date' => date("Y-m-d")

        ]);
    }

    public function getRowCount(): int
    {
        return $this->rows;
    }

    public function batchSize(): int
    {
        return 1000;
    }

    public function chunkSize(): int
    {
        return 1000;
    }
}
