<?php
//平衡杆 Rod/Strut,stabiliser
$maxInsertRowsCount = 0;
if ($item['pos2'] == 'L/R' || $item['pos2'] == '') {
    $style = [
        'image' => [
            'col' => 1,
            'top' => true,
            'offsetX' => 0,
            'height' => 80,
        ],
    ];
    $skips[] = $currentKey;
    $back = $this->insertAny($sheet, $this->rowNum, $item, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    $style = [
        'image' => [
            'col' => false,
            'top' => true,
            'height' => 80,

            'flip' => true,
            'flipCol' => 9,
            'flipOffsetX' => -30,
        ],
    ];
    $back = $this->insertAny($sheet, $this->rowNum, $item, $style);
    if($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
}
if ($item['pos2'] == 'L') {
    $style = [
        'image' => [
            'col' => 1,
            'top' => true,
            'offsetX' => 0,
            'height' => 80,
        ],
    ];
    $skips[] = $currentKey;
    $back = $this->insertAny($sheet, $this->rowNum, $item, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    //找 右
    if ($newItem = $this->findRight($items, $item, $skips)) {
        $style = [
            'image' => [
                'col' => 9,
                'top' => true,
                'offsetX' => 0,
                'height' => 80,
            ],
        ];
        $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
        if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    }
}

if ($item['pos2'] == 'L' || $item['pos2'] == 'L/R' || $item['pos2'] == '') {
    // 平衡/稳定杆衬套 Stabiliser Bushing
    $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($item, $skips) {
    return isset($v['pos1']) && $v['pos1'] === $item['pos1']
        && ($v['pos2'] == 'L/R' || $v['pos2'] == '')
        && isset($v['ss_name']) && ($v['ss_name'] === 'Stabiliser Bushing' || $v['ss_name'] === 'Stabiliser Bushing Kit')
        && isset($v['ssc_outnum']) && $v['ssc_outnum'] !== $item['ssc_outnum']
        && !in_array($ak, $skips);
    }, ARRAY_FILTER_USE_BOTH);
    if ($filteredItemsWithKeys) {
        foreach ($filteredItemsWithKeys as $key => $value) {;
            $skips[] = $key;
            break;
        }
        $style = [
            'image' => [
                'col' => 5,
                'top' => true,
                'offsetX' => -20,
                'height' => 80,

                'flip' => true,
                'flipCol' => 6,
                'flipOffsetX' => -0,
            ],
        ];
        $newItem = $this->posAddToName($value);
        $this->insertAny($sheet, $this->rowNum, $newItem, $style);
    }
}
