<?php
//拉杆球头, 单拉杆
$maxInsertRowsCount = 0;

$hasCenterPro = false; //是否有中间产品
if ($item['pos2'] == 'L' || $item['pos2'] == 'L/R' || $item['pos2'] == '') {

    // 拉杆球头Tie Rod Axle Joint
    //找手动配对中间产品
    $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($item, $skips) {
    return isset($v['pos1']) && $v['pos1'] === $item['pos1']
        && isset($v['ss_name']) && $v['ss_name'] === 'Tie Rod Axle Joint'
        && isset($v['ssc_outnum']) && $v['ssc_outnum'] !== $item['ssc_outnum']
        && ($v['ssc_outnum'] == $item['ssc_pair'] || $v['ssc_pair'] == $item['ssc_outnum'])
        && !in_array($ak, $skips);
    }, ARRAY_FILTER_USE_BOTH);

    //正常顺序找中间产品
    if (empty($filteredItemsWithKeys)) {
        $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($items, $item, $skips) {
            return isset($v['pos1']) && $v['pos1'] === $item['pos1']
                && ($v['pos2'] == 'L/R' || $v['pos2'] == '')
                && isset($v['ss_name']) && $v['ss_name'] === 'Tie Rod Axle Joint'
                && isset($v['ssc_outnum']) && $v['ssc_outnum'] !== $item['ssc_outnum']
                && $v['ssc_pair'] == ''
                && array_search($v['ssc_outnum'], array_column($items, 'ssc_pair')) === false
                && !in_array($ak, $skips);
        }, ARRAY_FILTER_USE_BOTH);
    }

    if ($filteredItemsWithKeys) {
        $hasCenterPro = true;
        foreach ($filteredItemsWithKeys as $key => $value) {
            $skips[] = $key;
            break;
        }
        $style = [
            'image' => [
                'col' => 5,
                'top' => true,
                'offsetX' => -20,
                'height' => 80,

                'flipRight' => true,
                'flip' => true,
                'flipCol' => 6,
                'flipOffsetX' => -0,
            ],
        ];
        $back = $this->insertAny($sheet, $this->rowNum, $value, $style);
        if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    }
}

$col_L = $hasCenterPro ? 1 : 3;
$col_R = $hasCenterPro ? 9 : 7;

//拉杆球头
if ($item['pos2'] == 'L/R') {
    $style = [
        'image' => [
            'col' => $col_L,
            'top' => true,
            'offsetX' => 32,
            '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' => $col_R,
            'flipOffsetX' => 32,
        ],
    ];
    $newItem = $this->posAddToName($item);
    $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
}
if ($item['pos2'] == 'L') {
    $style = [
        'image' => [
            'col' => $col_L,
            'top' => true,
            'offsetX' => 32,
            'height' => 80,
        ],
    ];
    $skips[] = $currentKey;
    $newItem = $this->posAddToName($item);
    $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    //找 右
    if ($newItem = $this->findRight($items, $item, $skips)) {
        $style = [
            'image' => [
                'col' => $col_R,
                'top' => true,
                'offsetX' => 0,
                'height' => 80,
            ],
        ];
        $newItem = $this->posAddToName($newItem);
        $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
        if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    }
}
