<?php

namespace App\Imports;

use App\Models\DataImportModel;
use App\Models\ProvinceAndCity\CityModel;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithStartRow;
class DataImport implements ToModel, WithHeadingRow, WithStartRow {
    /**
     * Define the starting row to read data from.
     *
     * @return int
     */
    public function startRow(): int
    {
        return 0; // Start reading from the second row
    }

    /**
     * Process the row data from Excel.
     *
     * @param array $row
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function model(array $row)
    {

        $cities = [];
        foreach ($row as $key => $value) {
            if (isset($value)) {
                $cities[] = $value;
              //  CityModel::where("id", '!=', 221)->where('id', '!=', 222)->where("id", '!=', 223)->where('id', '!=', 225)->delete();
              $findCity_name = CityModel::where("name",$value)->first();
                if (!$findCity_name) {
                    CityModel::insert([
                        'province_id' => 32,
                        'uc' => 192921,
                        'name'=> $value,
                        'isActivate'=> 1,
                        'ip_address' => '127.0.0.1',
                        'username'=> 'administrator',
                        'password'=> 'administrator',
                        'merchant_id'=> null
                    ]);      
    
                }
            }
        }
    }
}
