<?php
//Track Control Arm  控制臂	
$maxInsertRowsCount = 0;
$controlArmHeight = 120;//106.67;
$hasCenterPro = false; //是否有中间产品
// 控制臂衬套	Control Arm Bush
$findControlArmBush = false;
if ($item['pos2'] == 'L' || $item['pos2'] == 'L/R' || $item['pos2'] == '') {
    //优先找父子关系
    $sonOutnums = $this->findSonOutnums($item['ssc_outnum']);
    if ($sonOutnums) {
        foreach ($sonOutnums as $sonOutnum) {
            foreach ($items as $sonKey => $v) {
                if ($v['ssc_outnum'] == $sonOutnum && in_array($v['ss_name'], [
                    'Ball Joint',
                    'Control Arm Bush',
                    'Control Arm Bushing',
                    'Control Arm Bush Kit',
                    'Control Arm Bushing Kit',
                ])) {
                    $skips[] = $sonKey;
                    $findControlArmBush = true;
                    break;
                }
            }
            if ($findControlArmBush) break;
        }
    }

    if ($findControlArmBush) {
        $hasCenterPro = true;
        $skips[] = $currentKey;
        $centerItem = $items[$sonKey];
        $centerItem = $this->posAddToName($centerItem);
    } else {
        //找手动配对中间产品
        $filteredItemsWithKeys = array_filter($items, function ($v, $ak) use ($item, $skips) {
            return $v['pos1'] === $item['pos1']
                && in_array($v['ss_name'], [
                    'Ball Joint',
                    'Control Arm Bush',
                    'Control Arm Bushing',
                    'Control Arm Bush Kit',
                    'Control Arm Bushing Kit',
                ])
                && ($v['pos2'] == 'L/R' || $v['pos2'] == '')
                && $v['pos3'] === $item['pos3']
                && $v['pos4'] === $item['pos4']
                && ($v['ss_remark'] === $item['ss_remark'] || $v['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'], [
                        'Ball Joint',
                        'Control Arm Bush',
                        'Control Arm Bushing',
                        'Control Arm Bush Kit',
                        'Control Arm Bushing Kit',
                    ])
                    && ($v['pos2'] == 'L/R' || $v['pos2'] == '')
                    && $v['pos3'] === $item['pos3']
                    && $v['pos4'] === $item['pos4']
                    && ($v['ss_remark'] === $item['ss_remark'] || $v['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'], [
                        'Ball Joint',
                        'Control Arm Bush',
                        'Control Arm Bushing',
                        'Control Arm Bush Kit',
                        'Control Arm Bushing Kit',
                    ])
                    && ($v['pos2'] == 'L/R' || $v['pos2'] == '')
                    && $v['pos3'] == ''
                    && $v['pos4'] === $item['pos4']
                    && ($v['ss_remark'] === $item['ss_remark'] || $v['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) {
            //dd($filteredItemsWithKeys, $item, $items);
            /*
            foreach ($filteredItemsWithKeys as $key => $value) {;
                $skips[] = $key;
                break;
            }
            */
            $hasCenterPro = true;
            $filteredItems = [];
            foreach ($filteredItemsWithKeys as $key => $value) {;
                $filteredItems[] = $items[$key];
            }
            $filteredItems = $this->sortItems($filteredItems);
            foreach ($filteredItems as $key => $value) {;
                $skips[] = $key;
                break;
            }
            $centerItem = $this->posAddToName($value);
        }
    } // $find
} // pos2 == L, L/R, ''


$col_L = $hasCenterPro ? 1 : 3;
$col_R = $hasCenterPro ? 9 : 7;

if ($item['pos2'] == 'L/R' || $item['pos2'] == '') {
    $style = [
        'image' => [
            'col' => $col_L,
            'top' => true,
            'height' => 120,
        ],
    ];
    $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' => $controlArmHeight,

            'flip' => true,
            'flipCol' => $col_R,
            'flipOffsetX' => 0,
        ],
    ];
    $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;

}
if ($item['pos2'] == 'L') {
    $style = [
        'image' => [
            'col' => $col_L,
            'top' => true,
            'height' => $controlArmHeight,
        ],
    ];
    $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,
                'height' => $controlArmHeight,
            ],
        ];
        $back = $this->insertAny($sheet, $this->rowNum, $newItem, $style);
        if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
    }
    //dd($item, $skips, $filteredItemsWithKeys, $items);
}


// 插入中间产品
if ($hasCenterPro) {
    /*
        $style = [
            'image' => [
                'col' => 5,
                'top' => true,
                'lineHeight' => $controlArmHeight,
                'height' => 80,
            ],
        ];
        $back = $this->insertAny($sheet, $this->rowNum, $centerItem, $style);
        if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
        */
    $style = [
        'image' => [
            'col' => 5,
            'top' => true,
            'offsetX' => -20,
            'height' => 80,
            'lineHeight' => $controlArmHeight,

            'flip' => true,
            'flipCol' => 6,
            'flipOffsetX' => -0,
        ],
    ];
    $back = $this->insertAny($sheet, $this->rowNum, $centerItem, $style);
    if ($back > $maxInsertRowsCount) $maxInsertRowsCount = $back;
}
