<?php
ini_set('display_errors','on');
error_reporting(E_ERROR);

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

// 配置目录
const WEB_PATH = __DIR__;
require WEB_PATH . '/catalog.php';
$sComposer = WEB_PATH . '/resources/vendor/autoload.php';
if (is_file($sComposer)) {
    require $sComposer;
}

// 轮毂小样
// 水泵小样
// 减振器小样
// 传动轴小样
// 悬挂件小样
// 驱动轴小样
// 张紧轮轴承(板块写好了, 目录索引未处理)
// 自由版式(需求不确定)
$sMethod = $_GET['method'] ?? '';
if (!$sMethod)
{
    $aCommand = getopt('r:');
    $sCommand = $aCommand['r'] ?? '';
    if ($sCommand) {
        $sMethod = $sCommand;
    }
}
$aMethod = [
    'Wheel' => '轮毂小样',
    'WaterPump' => '水泵小样',
    'ShockAbsorber' => '减振器小样',
    'PropShaft' => '传动轴小样',
    'PropShaftAssembly' => '传动轴高端线总成小样',
    'Suspension' => '悬挂件小样',
    'Sensor' => '传感器小样',
    'Drive' => '驱动轴小样',
    'Bearing' => '轴承',
    'leaflet' => '宣传页',
    'tests' => '测试',
];
if ($sMethod)
{
    /**
     * 配置说明
     * @example
     * [
     *      'debug' => true, // 调试某块子版式的时候, 设置debug为FALSE
     *      'style' => 'Wheel', // [配置抽出, 亦无用]
     *      'remark' => '轮毂小样', // 浏览器调试模式标题头
     *
     *      'name' => 'wheel.pdf', // 文件名称
     *      'path' => '/home/path', // 存放目录
     *
     *      'browser' => false, // 是否浏览器调试模式 / 保存文件返回文件名, 默认保存文件
     *      'resources' => false, // 资源目录
     *
     *      'compute' => ['model', 'cross', 'new', 'vehicle'], // 需要计算页码的步骤
     *      'setup' => ['header', 'general', 'vehicle', 'rule', 'model', 'cross', 'photo', 'footer'], // 生成完整PDF的步骤
     * ]
     */
    $aSetup = [
        'Wheel' => [
            'compute' => [
                'rule',
                'param_hubAssembliesByWheel', 'tools_hubAssembliesByWheel',
                'param_brakeByWheel', 'tools_brakeByWheel',
                'param_driveShaftByWheel', 'tools_driveShaftByWheel',
                'param_bearingKitsByWheel', 'tools_bearingKitsByWheel',
                'param_stubAxleByWheel', 'tools_stubAxleByWheel',
                'model',
                'photo_hubAssembliesByWheel', 'photo_brakeByWheel', 'photo_driveShaftByWheel', 'photo_bearingKitsByWheel',
                'cross', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule', 'param_hubAssembliesByWheel', 'tools_hubAssembliesByWheel', 'param_brakeByWheel',
                'tools_brakeByWheel', 'param_driveShaftByWheel', 'tools_driveShaftByWheel',

                'param_bearingKitsByWheel', 'tools_bearingKitsByWheel',
                'param_stubAxleByWheel', 'tools_stubAxleByWheel',
                'model', 'photo_hubAssembliesByWheel', 'photo_brakeByWheel', 'photo_driveShaftByWheel', 'photo_bearingKitsByWheel',
                'cross', 'new', 'footer'

            ],
        ],
        'WaterPump' => [
            'compute' => [
                'model',
                'photo|Water pump|photoGuideByWaterPump', 'photo|Water pump 1|photoGuideByWaterPump',
                'cross',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule', 'model', 'photo|Water pump|photoGuideByWaterPump', 'photo|Water pump 1|photoGuideByWaterPump', 'cross', 'footer'
            ],
        ],
        'ShockAbsorber' => [
            'compute' => [
                'model', 'cross', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule', 'model', 'cross', 'new', 'footer'
            ],
        ],
        'PropShaft' => [
            'compute' => [
                'rule', 'model', 'cross', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule', 'model', 'cross', 'new', 'footer'
            ],
        ],
        'PropShaftAssembly' => [
            'compute' => [
                'rule', 'model', 'cross', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule', 'model', 'cross', 'new', 'footer'
            ],
        ],
        'Suspension' => [
            'compute' => [
                'rule', 'model', 'photo_Steering And Suspension Parts_photoGuideBySuspension', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule',
                'model',
                'photo_Steering And Suspension Parts_photoGuideBySuspension', 'new', 'footer'
            ],
        ],
        'Sensor' => [
            'compute' => [
                'rule', 'model', 'photo_Steering And Suspension Parts_photoGuideBySuspension', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule',
                'model',
                'photo_Steering And Suspension Parts_photoGuideBySuspension', 'new', 'footer'
            ],
        ],
        'Drive' => [
            'compute' => [
                'model', 'Drive Line Part|photoGuideBySuspension', 'param|Propshaft', 'param|Joint,Propeller Shaft', 'new',
                'vehicle',
            ],
            'setup' => [
                'header', 'general', 'vehicle', 'rule', 'model', 'Drive Line Part|photoGuideBySuspension', 'param|Propshaft', 'param|Joint,Propeller Shaft', 'new', 'footer'
            ],
        ],
        'Bearing' => [
            'compute' => [
                'Clutch Release Bearing|modelGuideByBearing', 'Clutch Release Bearing|vehicleGuide',
                'Clutch Release Bearing|clutchByBearing', 'Clutch Release Bearing|photoGuideByBearing', 'Clutch Release Bearing|cross',
                'Tensioner Bearing|modelGuideByBearing', 'Tensioner Bearing|tensionerByBearing', 'Tensioner Bearing|photoGuideByBearing', 'Tensioner Bearing|cross', 'Tensioner Bearing|vehicleGuide',
            ],
            'setup' => [
                'header', 'general',
                'Clutch Release Bearing|vehicleGuide',
                'Clutch Release Bearing|modelGuideByBearing', 'Clutch Release Bearing|clutchByBearing', 'Clutch Release Bearing|photoGuideByBearing', 'Clutch Release Bearing|cross',
                'Tensioner Bearing|vehicleGuide',
                'Tensioner Bearing|modelGuideByBearing', 'Tensioner Bearing|tensionerByBearing', 'Tensioner Bearing|photoGuideByBearing', 'Tensioner Bearing|cross'
            ],
        ],

        'leaflet' => [
            'compute' => [],
            'setup' => ['leaflet'],
        ],
        
        'tests' => require __DIR__ . '/storage/data/option.php'
    ];
    $aOption = array_merge($aSetup[$sMethod], [
        'debug' => true, // 调试某块子版式的时候, 设置debug为FALSE
        'style' => $sMethod, // 选择目录版式
        'remark' => $aMethod[$sMethod],

        'color' => 'blue', // 风格: 蓝色(blue) | 红色(red)
        'name' => sprintf('%s.pdf', time()), // 文件名称
        'path' => sprintf('%s/storage/pdf', __DIR__), // 存放目录
        'empty' => '', // 定义产品图空白图片
        
        'browser' => true, // 是否浏览器调试模式 / 保存文件返回文件名
        'fonts' => [
            'arial' => 'font/arial',
            'arial_bd' => 'font/arial_bd',
            'arial_black' => 'font/arial_black',
        ], // 传递字体, 绝对路径
        'resources' => sprintf('%s/storage', __DIR__), // 资源目录
    ]);

    /**
     * 所有数据格式
     * 总索引页和车型索引页 不需要传递"value", 只需要传递其他值以及版式, 由程序生成
     *
     * @example
     * [
     *      'key' => [
     *          'style' => 'remarkGuide', // 版式
     *          'title' => 'Clutch Release Bearing', // 标题
     *          'catalog' => 'Clutch Release Bearing', // 分组
     *          'remark' => '', // 用于索引页 以及 目录索引
     *          'value' => ['/path/name.jpg'] // 数据
     *      ]
     *      // ......
     * ]
     *
     */
    $sCatalog = sprintf('%s/storage/data/%s.php', WEB_PATH, $sMethod);
    $aCatalog = is_file($sCatalog) ? require $sCatalog : [];

    // dd($aCatalog);
    // dd(sprintf("'%s'", implode("', '", array_keys($aCatalog))));
    
    /**
     * 方法说明
     * @example
     * 整图插入页: remarkGuide 封面封底,介绍页,规则页,说明页
     *
     * 总索引: generalGuide 其他小样
     *
     * 车型索引: vehicleGuide 轮毂小样
     *          vehicle1Guide 水泵小样
     *
     * 轮毂参数页: hubAssembliesByWheel 轮毂小样(Hub Assemblies)
     *           brakeByWheel 轮毂小样(BraKe Disc/Brake Drum)
     *           driveShaftByWheel 轮毂小样(Drive Shaft)
     *           bearingKitsByWheel 轮毂小样(Bearing Kits)
     *
     * 轮毂附件配置页: tools1Guide 轮毂小样(Hub Assemblies & BraKe Disc/Brake Drum & Drive Shaft & Drive Shaft)
     *
     * 驱动轴参数页: propellerShaftByDrive 驱动轴小样
     *              propShaftByDrive 驱动轴小样
     *
     * 车型排版: modelGuideByWheel 轮毂小样
     *          modelGuideByWaterPump 水泵小样
     *          modelGuideByShockAbsorber 减振器小样
     *          modelGuideBySuspension 悬挂件小样
     *          modelGuideByPropShaft 传动轴小样
     *          modelGuideByDrive 驱动轴小样
     *
     * 图片排版: photoGuideByWheel 轮毂小样
     *          photoGuideByWaterPump 水泵小样
     *          photoGuideBySuspension 悬挂件小样, 驱动轴小样
     *
     * 交叉号排版: cross1Guide 轮毂小样(5列, 蓝线)
     *            cross2Guide 水泵小样(4列, 黑线)
     *            cross3Guide 减振器小样(4列, 无线, 高列头)
     *
     * 新号码排版: new1Guide 轮毂小样
     */

    //
    
    /**
     * 参数排版说明
     * @example
     * Hub Assemblies: hubAssembliesByWheel
     * BraKe Disc/Brake Drum: brakeByWheel // 方法看 hubAssembliesByWheel
     * Drive Shaft: driveShaftByWheel
     * Bearing Kits: bearingKitsByWheel
     */
    $oCatalog = new Catalog($aOption, $aCatalog);
    $sResult = $oCatalog->run();

    // 文件模式输出文件名称
    if (!$aOption['browser']) {
        dd($sResult);
    }
    exit;
}

?>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">

    <title>冠盛, PDFlib</title>
    <style>
        .form-check-label {
            font-size: 10px;
        }
        .card-body {
            padding: 0;
        }
        .green {
            color: green;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="py-5 text-center">
        <h3>冠盛, PDF</h3>
        <p class="lead">Use PDFlib.</p>
    </div>
    <ul>
        <?php foreach ($aMethod as $sKey => $sVal): ?>
        <li><a href="index.php?method=<?php echo $sKey; ?>" target="_blank"><?php echo $sVal; ?></a></li>
        <?php endforeach; ?>
    </ul>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
