<?php
require_once 'conn.php';
checkLogin();

$id = $_GET['id'] ?? '';

// 验证并获取订单数据
if (!empty($id) && is_numeric($id)) {
    // 获取订单基本信息
    $employee_id = $_SESSION['employee_id'];
    $isAdmin = checkIfAdmin();
    
    $sql = "SELECT o.*, c.cs_company, c.cs_code, cc.contact_name, e.em_user as employee_name 
            FROM orders o 
            LEFT JOIN customer c ON o.customer_id = c.id 
            LEFT JOIN customer_contact cc ON o.contact_id = cc.id 
            LEFT JOIN employee e ON o.employee_id = e.id
            WHERE o.id = $id";
    
    // 非管理员只能查看自己的订单
    if (!$isAdmin) {
        $sql .= " AND o.employee_id = $employee_id";
    }

    $result = mysqli_query($conn, $sql);

    if ($row = mysqli_fetch_assoc($result)) {
        $order = $row;
    } else {
        echo "<script>alert('订单不存在或您没有权限查看');history.back();</script>";
        exit;
    }

    // 获取订单项信息
    $sql = "SELECT oi.*, p.ProductName,ps.spec_name
            FROM order_items oi 
            LEFT JOIN products p ON oi.product_id = p.id 
            LEFT JOIN product_specifications ps ON oi.specification_id = ps.id
            WHERE oi.order_id = $id";

    $itemsResult = mysqli_query($conn, $sql);

    $orderItems = [];
    while ($itemRow = mysqli_fetch_assoc($itemsResult)) {
        $orderItems[] = $itemRow;
    }
} else {
    echo "<script>alert('订单不存在!');history.back();</script>";
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>订单详情</title>
    <link rel="stylesheet" href="css/common.css" type="text/css" />
    <link rel="stylesheet" href="css/alert.css" type="text/css" />
    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/js.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            background: #fff;
        }
        #man_zone {
            margin-left: 0;
        }
        .order-info {
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
            background-color: #f9f9f9;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .order-info h2 {
            margin-top: 0;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
        }
        .order-info .info-row {
            margin-bottom: 10px;
        }
        .order-info .info-label {
            font-weight: bold;
            display: inline-block;
            width: 120px;
        }
        
        /* 产品行样式 - 从order_add.php采用 */
        .product-row {
            position: relative;
            padding: 12px 15px;
            margin-bottom: 8px;
            border-radius: 4px;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 8px;
            background-color: #f9f9f9;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .product-info {
            flex: 2;
            min-width: 200px;
        }
        .product-spec {
            flex: 2;
            min-width: 200px;
        }
        .product-quantity {
            flex: 1;
            min-width: 80px;
        }
        .product-unit {
            flex: 0.5;
            min-width: 60px;
        }
        .product-price {
            flex: 1;
            min-width: 100px;
        }
        .product-total {
            flex: 1;
            min-width: 100px;
            font-weight: bold;
        }
        .product-discount {
            flex: 1;
            min-width: 100px;
        }
        .product-notes {
            flex: 2;
            min-width: 200px;
        }
        .row-section {
            display: flex;
            flex-direction: column;
        }
        .row-section-label {
            font-size: 12px;
            color: #666;
            margin-bottom: 3px;
        }
        .product-list-header {
            display: flex;
            background-color: #eee;
            padding: 8px 15px;
            margin-bottom: 10px;
            border-radius: 4px;
            font-weight: bold;
            color: #555;
            font-size: 13px;
            gap: 8px;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .product-row {
                flex-direction: column;
                align-items: flex-start;
            }
            .product-info, .product-spec, .product-quantity, 
            .product-unit, .product-price, .product-total,
            .product-discount, .product-notes {
                width: 100%;
                min-width: 100%;
                margin-bottom: 8px;
            }
            .product-list-header {
                display: none !important;
            }
            .row-section-label span {
                display: inline;
            }
        }
        
        .notes-section {
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .total-section {
            text-align: right;
            font-size: 1.1em;
            margin-top: 10px;
            border-top: 1px solid #ddd;
            padding-top: 10px;
            background-color: #f5f5f5;
            border-radius: 4px;
            padding: 15px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
<div id="man_zone">
    <div align="right" style="margin-bottom: 10px;">
        <input type="button" value="返回订单列表" class="btn1" onClick="location.href='order.php'" />
        <input type="button" value="编辑此订单" class="btn1" onClick="location.href='order_edit.php?id=<?= $id ?>'" />
    </div>

    <div class="order-info">
        <h2>订单信息</h2>
        <div class="info-row">
            <span class="info-label">销售订单号:</span> <?= htmlspecialcharsFix($order['order_code']) ?>
        </div>
        <div class="info-row">
            <span class="info-label">客户:</span> 
            <?php if (!empty($order['cs_code'])): ?>
                <?= htmlspecialcharsFix($order['cs_code']) ?> - 
            <?php endif; ?>
            <?= htmlspecialcharsFix($order['cs_company']) ?>
        </div>
<!--        <div class="info-row">-->
<!--            <span class="info-label">联系人:</span> --><?php //= htmlspecialcharsFix($order['contact_name']) ?>
<!--        </div>-->
        <div class="info-row">
            <span class="info-label">出货日期:</span> <?= date('Y-m-d', strtotime($order['order_date'])) ?>
        </div>
        <div class="info-row">
            <span class="info-label">创建时间:</span> <?= $order['created_at'] ?>
        </div>
        <div class="info-row">
            <span class="info-label">最后更新:</span> <?= $order['updated_at'] ?>
        </div>
        <div class="info-row">
            <span class="info-label">销售员:</span> <?= htmlspecialcharsFix($order['employee_name']) ?>
        </div>
    </div>

    <h2>订单产品</h2>
    
    <!-- 产品表头 -->
    <div class="product-list-header">
        <div style="flex: 2; min-width: 200px;">产品</div>
        <div style="flex: 2; min-width: 200px;">规格</div>
        <div style="flex: 1; min-width: 80px;">数量</div>
        <div style="flex: 0.5; min-width: 60px;">单位</div>
        <div style="flex: 1; min-width: 100px;">单价</div>

        <div style="flex: 1; min-width: 100px;">总价</div>

    </div>
    
    <!-- 产品列表 -->
    <div id="product-container">
        <?php foreach ($orderItems as $index => $item): ?>
            <div class="product-row">
                <!-- 产品名称 -->
                <div class="row-section product-info">

                    <div class="selected-product-info"><?= htmlspecialcharsFix($item['ProductName']) ?></div>
                </div>
                
                <!-- 规格 -->
                <div class="row-section product-spec">

                    <div><?= $item['spec_name'] ?></div>
                </div>
                
                <!-- 数量 -->
                <div class="row-section product-quantity">

                    <div><?= $item['quantity'] ?></div>
                </div>
                
                <!-- 单位 -->
                <div class="row-section product-unit">

                    <div><?= htmlspecialcharsFix($item['unit']) ?></div>
                </div>
                
                <!-- 单价 -->
                <div class="row-section product-price">

                    <div><?= number_format($item['unit_price'], 2) ?></div>
                </div>
                

                
                <!-- 总价 -->
                <div class="row-section product-total">

                    <div><?= number_format($item['total_price'], 2) ?></div>
                </div>
                

            </div>
        <?php endforeach; ?>
    </div>

    <div class="total-section">
        <div style="font-size: 1.2em; margin-top: 5px;">
            <strong>订单总额:</strong> <?= number_format($order['total_amount'], 2) ?>
        </div>
    </div>

    <div class="notes-section">
        <h2>订单备注</h2>
        <div>
            <?php if (!empty($order['notes'])): ?>
                <p><?= nl2br(htmlspecialcharsFix($order['notes'])) ?></p>
            <?php else: ?>
                <p><em>无备注</em></p>
            <?php endif; ?>
        </div>
    </div>

</div>
</body>
</html>