<?php

namespace App\Imports;

use App\Owner;
use App\PaperBox;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

HeadingRowFormatter::default('none');
class PaperBoxesImport implements ToCollection,WithHeadingRow
{
    protected $isOverride;
    public function __construct($isOverride)
    {
        if ($isOverride==1){
            $this->isOverride=true;
        }
    }


    /**
     * @param Collection $collection
     */
    public function Collection(Collection $collection)
    {
        $endIs=false;
        $cityIs=true;
        if (isset($collection->toArray()[0]['货主'])&&isset($collection->toArray()[0]['纸箱名称'])&&isset($collection->toArray()[0]['长'])
            &&isset($collection->toArray()[0]['宽'])&&isset($collection->toArray()[0]['高'])){
                $endIs=true;
        }else{
                Cache::put('error','请检查您第一行标题是否存在货主，纸箱名称，长，宽，高',86400);
                $endIs=false;
        }
        $exception=[];
        $sum=2;
        if ($endIs) {
            foreach ($collection as $row) {
                if (!$row['货主']) {
                    array_push($exception, ['第' . $sum . '行数据货主为空！']);
                    $sum++;
                    continue;
                }
                if (!$row['纸箱名称']) {
                    array_push($exception, ['第' . $sum . '行数据纸箱名称为空！']);
                    $sum++;
                    continue;
                }
                if (!$row['长'] || preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['长']) == 0) {
                    array_push($exception, ['第' . $sum . '行数据长为空或不为数字！']);
                    $sum++;
                    continue;
                }
                if (!$row['宽'] || preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['宽']) == 0) {
                    array_push($exception, ['第' . $sum . '行数据宽为空或不为数字！']);
                    $sum++;
                    continue;
                }
                if (!$row['高'] || preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['高']) == 0) {
                    array_push($exception, ['第' . $sum . '行数据高为空或不为数字！']);
                    $sum++;
                    continue;
                }
                $owner = Owner::where("name", $row['货主'])->first();
                if (!$owner) {
                    array_push($exception, ['第' . $sum . '行数据货主在系统中未找到！']);
                    $sum++;
                    continue;
                }
                $length = $row['长'];
                $width = $row['宽'];
                $height = $row['高'];
                $max = ($length >= ($width >= $height ? $width : $height) ? $length : ($width >= $height ? $width : $height));
                if ($max == $length) {
                    $centre = $width >= $height ? $width : $height;
                    $min = $width < $height ? $width : $height;
                } elseif ($max == $width) {
                    $centre = $length >= $height ? $length : $height;
                    $min = $length < $height ? $length : $height;
                } else {
                    $centre = $width >= $length ? $width : $length;
                    $min = $width < $length ? $width : $length;
                }
                $paperBox = PaperBox::with('owners')->where('model', $row['纸箱名称'])->first();
                if ($paperBox) {
                    if ($this->isOverride) {
                        $paperBox->length = $max;
                        $paperBox->width = $centre;
                        $paperBox->height = $min;
                        $paperBox->save();
                        array_push($exception, ['第' . $sum . '行数据已覆盖！']);
                    } else {
                        array_push($exception, ['第' . $sum . '行数据已存在！']);
                    }
                    if ($paperBox->owners){
                        $isOwner = true;
                        foreach ($paperBox->owners as $o){
                            if ($o->id == $owner->id){
                                $isOwner = false;
                                break;
                            }
                        }
                        if ($isOwner) $paperBox->owners()->syncWithoutDetaching($owner->id);
                    }
                } else {
                    $paperBox = new PaperBox([
                        'model' => $row['纸箱名称'],
                        'length' => $max,
                        'width' => $centre,
                        'height' => $min,
                    ]);
                    $paperBox->save();
                    $paperBox->owners()->sync($owner->id);
                }
                $sum++;
            }
            Cache::put('exception', $exception, 86400);
        }
    }
}
