<?php

namespace App\Imports;

use App\Exceptions\ImportError;
use App\Models\Device;
use App\Models\DeviceName;
use App\Models\InnerDevice;
use App\Models\InnerDeviceNamesModel;
use App\Models\Option;
use App\Models\Project;
use App\Models\ProjectZone;
use App\Models\Road;
use App\Models\Spec;
use App\Models\WorkPoint;
use App\Models\Zone;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class WorkPointImport implements ToCollection
{
    protected $model;

    protected $zone;

    protected $road;

    protected $project_zone;

    public function __construct()
    {
        $this->model = new WorkPoint();
    }

    public function collection(Collection $rows)
    {
        if(count($rows) <= 1) {
            /** @noinspection PhpUnhandledExceptionInspection */
            return false;
        };
        foreach ($rows as $key => $row) {
            if($key == 0) continue;
            if(empty($row[0])&&empty($row[1]))
            {
                break;
            }
            $project_id = null;
            $work_point = null;
            //项目
            if ($row[0])
            {
                $project_id = Project::firstOrCreate([
                    'name' => $row[0],
                ],[
                    'name' => $row[0],
                    'active' => 1
                ]);
            }
            //项目挂钩工点
            if ($row[1])
            {
                if($project_id && $row[1]) {
                    $work_point = WorkPoint::firstOrCreate([
                        'project_id' => $project_id->id,
                        'name' => $row[1],
                    ],[
                        'project_id' => $project_id->id,
                        'name' => $row[1],
                        'status' => 1
                    ]);
                }
            }
        }
        return true;
    }
}
