<?php

namespace App\Imports;

use App\Exceptions\ImportError;
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 InnerDeviceImport implements ToCollection
{
    protected $model;

    protected $zone;

    protected $road;

    protected $project_zone;

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

    function excelTime($date, $time = false) {
        if(function_exists('GregorianToJD')){
            if (is_numeric( $date )) {
                $jd = GregorianToJD( 1, 1, 1970 );
                $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
                $date = explode( '/', $gregorian );
                $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
                    ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
                    ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
                    . ($time ? " 00:00:00" : '');
                return $date_str;
            }
        }else{
            $date=$date>25568?$date+1:25569;
            /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
            $ofs=(70 * 365 + 17+2) * 86400;
            $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
        }
//        dd($date);
        return $date;
    }

    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])&&empty($row[2]))
            {
                break;
            }
            //设备名称
            if ($row[1])
            {
                $device_name = InnerDeviceNamesModel::firstOrCreate([
                    'name' => $row[1],
                    'sort' => 1,
                    'status' => 1
                ]);
            }
            $spec_name = $row[2];
            $project = null;
            $work_point = null;
            //项目
            if($row[7]) {
                $project = Project::firstOrCreate([
                    'name' => $row[7],
                    'active' => 1
                ]);
            }
//            //工点
//            if($project && $row[8]) {
//                $work_point = WorkPoint::firstOrCreate([
//                    'project_id' => $project->id,
//                    'name' => $row[8]
//                ]);
//            }

            $status = Option::where([
                ['table', '=', 'inner_devices'],
                ['column', '=', 'status'],
                ['name', '=', $row[8]]
            ])->first();

            $data = [
                'number' => $row[0],
                'device_name_id' => $device_name ? $device_name->id : '',
                'spec_name' => $spec_name ? $spec_name : '',
                'produce_date' => $this->excelTime($row[3]),
                'buy_origin' => $row[4],
                'manufacturer' => $row[5],
                'shape' => $row[6],
                'project_id' => $project ? $project->id : '',
//                'work_point_id' => $work_point ? $work_point->id : '',
                'status' => $status ? $status->id : ''
            ];
            $this->model->create($data);
        }
        return true;
    }
}
