<?php
//球头 Ball Joint
$maxInsertRowsCount = 0;
$hasCenterPro = false; //是否有中间产品

/*
// 控制臂衬套	Control Arm Bush
$filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($item, $skips) {
    return $v['pos1'] === $item['pos1']
        && in_array($v['ss_name'], [
            'Control Arm Bush',
            'Control Arm Bushing',
            'Control Arm Bush Kit',
            'Control Arm Bushing Kit',
        ])
        && $v['ssc_outnum'] !== $item['ssc_outnum']
        && !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' => 32,
            'height' => 80,
        ],
    ];
    $newItem = $this->posAddToName($value);
    $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
}
*/

// 查询是否有 控制臂
$findControlArm = false;

/*
//优先找父子关系
$sonOutnums = $this->findFatherOutnums($item['ssc_outnum']);
if ($sonOutnums) {
    foreach ($sonOutnums as $sonOutnum) {
        foreach ($items as $sonKey => $v) {
            if ($v['ssc_outnum'] == $sonOutnum && in_array($v['ss_name'], [
                'Track Control Arm',
                'Control Arm',
            ])) {
                $findControlArm = true;
                break;
            }
        }
        if ($findControlArm) break;
    }
}

if (!$findControlArm) {
    //找手动配对控制臂
    $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($item, $skips) {
        return $v['pos1'] === $item['pos1']
            && in_array($v['ss_name'], [
            'Track Control Arm',
            'Control Arm',
        ])
            && $v['pos3'] === $item['pos3']
            && $v['pos4'] === $item['pos4']
            && ($v['ss_remark'] === $item['ss_remark'] || $item['ss_remark'] == '')
            && !in_array($ak, $skips)
            && ($v['ssc_outnum'] == $item['ssc_pair'] || $v['ssc_pair'] == $item['ssc_outnum'])
            && $v['ssc_outnum'] !== $item['ssc_outnum'];
    }, ARRAY_FILTER_USE_BOTH);

    //找上下,内外,备注 都相同 或者 备注为空
    if (!$filteredItemsWithKeys) {
        $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($item, $skips) {
            return $v['pos1'] === $item['pos1']
                && in_array($v['ss_name'], [
                'Track Control Arm',
                'Control Arm',
            ])
                && $v['pos3'] === $item['pos3']
                && $v['pos4'] === $item['pos4']
                && ($v['ss_remark'] === $item['ss_remark'] || $item['ss_remark'] == '')
                && !in_array($ak, $skips)
                && $v['ssc_pair'] == ''
                && $v['ssc_outnum'] !== $item['ssc_outnum'];
        }, ARRAY_FILTER_USE_BOTH);
    }

    //仅找上下,内外为空, 备注相同 或者 备注为空
    if (!$filteredItemsWithKeys) {
        $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($items, $item, $skips) {
            return $v['pos1'] === $item['pos1']
                && in_array($v['ss_name'], [
                'Track Control Arm',
                'Control Arm',
            ])
                && $v['pos3'] == ''
                && $v['pos4'] === $item['pos4']
                && ($v['ss_remark'] === $item['ss_remark'] || $item['ss_remark'] == '')
                && !in_array($ak, $skips)
                && $v['ssc_pair'] == ''
                && array_search($v['ssc_outnum'], array_column($items, 'ssc_pair')) === false
                && $v['ssc_outnum'] !== $item['ssc_outnum'];
        }, ARRAY_FILTER_USE_BOTH);
    }
    if ($filteredItemsWithKeys) {
        $findControlArm = true;
        foreach ($filteredItemsWithKeys as $key => $value) {
            $items[$key]['ssc_pair'] = $item['ssc_outnum'];
            break;
        }
    }
} // $find

*/

// 当没有成对的 控制臂 时
if (!$findControlArm) {
    $col_L = $hasCenterPro ? 1 : 3;
    $col_R = $hasCenterPro ? 9 : 7;

    if ($item['pos2'] == 'L/R' || $item['pos2'] == '') {
        $style = [
            'image' => [
                'col' => $col_L,
                'top' => true,
                'offsetX' => 0,
                'height' => 80,
            ],
        ];
        $skips[] = $currentKey;
        $newItem = $this->posAddToName($item);
        $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
        if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;    
        $style = [
            'image' => [
                'col' => false,
                'top' => true,
                'height' => 80,

                'flip' => true,
                'flipCol' => $col_R,
                'flipOffsetX' => -10,
            ],
        ];
        $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' => 0,
                '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,
                ],
            ];
            $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
            if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
        }
    }
}