<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List, java.text.SimpleDateFormat, com.hrs.model.entity.Order, com.hrs.model.entity.House, com.hrs.model.entity.User" %>
<%
    User loginUser = (User) session.getAttribute("loginUser");
    if (loginUser == null || !"0".equals(loginUser.getRoleType())) {
        response.sendRedirect(request.getContextPath() + "/user/login");
        return;
    }
    
    List<Order> orderList = (List<Order>) request.getAttribute("orderList");
    Integer totalCount = (Integer) request.getAttribute("totalCount");
    String msg = request.getParameter("msg");
    String error = request.getParameter("error");
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>我的订单 - 租房系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, "微软雅黑", sans-serif;
            background-color: #f5f5f5;
        }
        
        .header {
            background-color: #4CAF50;
            color: white;
            padding: 15px 0;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .container {
            width: 1200px;
            margin: 0 auto;
            overflow: hidden;
        }
        
        .logo {
            float: left;
            font-size: 24px;
            font-weight: bold;
        }
        
        .nav {
            float: right;
            margin-top: 5px;
        }
        
        .nav a {
            color: white;
            text-decoration: none;
            margin-left: 20px;
            padding: 5px 10px;
        }
        
        .nav a:hover, .nav a.active {
            background-color: #45a049;
            border-radius: 4px;
        }
        
        .user-info {
            float: right;
            margin-right: 20px;
            font-size: 14px;
        }
        
        .breadcrumb {
            background-color: white;
            padding: 12px 0;
            margin-bottom: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .breadcrumb a {
            color: #4CAF50;
            text-decoration: none;
        }
        
        .stats {
            background-color: white;
            padding: 10px 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            color: #666;
        }
        
        .message {
            background-color: #e8f5e9;
            color: #2e7d32;
            padding: 12px;
            border-radius: 4px;
            margin-bottom: 20px;
            text-align: center;
            animation: fadeOut 3s ease forwards;
        }
        
        .error-message {
            background-color: #ffebee;
            color: #c62828;
            padding: 12px;
            border-radius: 4px;
            margin-bottom: 20px;
            text-align: center;
            animation: fadeOut 3s ease forwards;
        }
        
        @keyframes fadeOut {
            0% { opacity: 1; }
            70% { opacity: 1; }
            100% { opacity: 0; display: none; }
        }
        
        .order-table {
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
        }
        
        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        th {
            background-color: #f5f5f5;
            font-weight: bold;
            color: #333;
        }
        
        tr:hover {
            background-color: #f9f9f9;
        }
        
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .status-pending-pay {
            background-color: #fff3e0;
            color: #f39c12;
        }
        
        .status-paid {
            background-color: #e8f5e9;
            color: #4CAF50;
        }
        
        .status-cancelled {
            background-color: #ffebee;
            color: #f44336;
        }
        
        .status-pending-sign {
            background-color: #e3f2fd;
            color: #2196F3;
        }
        
        .status-completed {
            background-color: #e8f5e9;
            color: #2e7d32;
        }
        
        .price {
            color: #f44336;
            font-weight: bold;
        }
        
        .btn {
            padding: 5px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            display: inline-block;
            margin: 0 3px;
        }
        
        .btn-pay {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-pay:hover {
            background-color: #45a049;
        }
        
        .btn-cancel {
            background-color: #f44336;
            color: white;
        }
        
        .btn-cancel:hover {
            background-color: #d32f2f;
        }
        
        .btn-disabled {
            background-color: #ccc;
            color: #666;
            cursor: not-allowed;
        }
        
        .house-link {
            color: #4CAF50;
            text-decoration: none;
        }
        
        .house-link:hover {
            text-decoration: underline;
        }
        
        .empty-state {
            text-align: center;
            padding: 60px;
            background-color: white;
            border-radius: 8px;
            color: #999;
        }
        
        .empty-state p {
            margin-top: 20px;
        }
        
        .empty-state a {
            color: #4CAF50;
            text-decoration: none;
        }
        
        .footer {
            background-color: #333;
            color: white;
            text-align: center;
            padding: 20px;
            margin-top: 40px;
        }
        
        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="container clearfix">
            <div class="logo">🏠 租房系统</div>
            <div class="user-info">
                欢迎，<%= loginUser.getRealName() %> |
                <a href="${pageContext.request.contextPath}/user/logout" style="color:white;">退出</a>
            </div>
            <div class="nav">
                <a href="${pageContext.request.contextPath}/user/house/list">首页</a>
                <a href="${pageContext.request.contextPath}/user/collection/list">我的收藏</a>
                <a href="${pageContext.request.contextPath}/user/reservation/list">我的预约</a>
                <a href="${pageContext.request.contextPath}/user/order/list" class="active">我的订单</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="breadcrumb">
            <a href="${pageContext.request.contextPath}/user/house/list">首页</a> &gt;
            <span>我的订单</span>
        </div>
        
        <% if (msg != null) { %>
            <div class="message"><%= msg %></div>
        <% } %>
        <% if (error != null) { %>
            <div class="error-message"><%= error %></div>
        <% } %>
        
        <div class="stats">
            📋 共有 <strong><%= totalCount %></strong> 个订单
        </div>
        
        <div class="order-table">
            <%
                if (orderList != null && !orderList.isEmpty()) {
            %>
            <table>
                <thead>
                    <tr>
                        <th>订单编号</th>
                        <th>房源信息</th>
                        <th>租赁时间</th>
                        <th>总租金</th>
                        <th>支付状态</th>
                        <th>订单状态</th>
                        <th>下单时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        for (Order order : orderList) {
                            House house = order.getHouse();
                    %>
                    <tr>
                        <td style="font-size:12px;"><%= order.getOrderNo() %></td>
                        <td>
                            <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>" class="house-link">
                                <strong><%= house.getTitle() %></strong>
                            </a><br>
                            <span style="font-size:12px;color:#999;"><%= house.getArea() %> | <%= house.getHouseType() %></span>
                        </td>
                        <td>
                            <%= sdf.format(order.getRentStartTime()) %> ~ <%= sdf.format(order.getRentEndTime()) %><br>
                            <span style="font-size:12px;color:#999;">共<%= order.getRentDays() %>天</span>
                        </td>
                        <td class="price">¥<%= order.getTotalRent() %></td>
                        <td>
                            <%
                                String payStatusClass = "";
                                String payStatusText = "";
                                if ("0".equals(order.getPayStatus())) {
                                    payStatusClass = "pending-pay";
                                    payStatusText = "待支付";
                                } else if ("1".equals(order.getPayStatus())) {
                                    payStatusClass = "paid";
                                    payStatusText = "已支付";
                                } else if ("2".equals(order.getPayStatus())) {
                                    payStatusClass = "cancelled";
                                    payStatusText = "已退款";
                                } else {
                                    payStatusClass = "pending-pay";
                                    payStatusText = "未知";
                                }
                            %>
                            <span class="status-badge status-<%= payStatusClass %>"><%= payStatusText %></span>
                        </td>
                        <td>
                            <%
                                String orderStatusClass = "";
                                String orderStatusText = "";
                                if ("0".equals(order.getStatus())) {
                                    orderStatusClass = "pending-sign";
                                    orderStatusText = "待签约";
                                } else if ("1".equals(order.getStatus())) {
                                    orderStatusClass = "paid";
                                    orderStatusText = "已签约";
                                } else if ("2".equals(order.getStatus())) {
                                    orderStatusClass = "completed";
                                    orderStatusText = "已结束";
                                } else if ("3".equals(order.getStatus())) {
                                    orderStatusClass = "cancelled";
                                    orderStatusText = "已取消";
                                } else {
                                    orderStatusClass = "pending-sign";
                                    orderStatusText = "未知";
                                }
                            %>
                            <span class="status-badge status-<%= orderStatusClass %>"><%= orderStatusText %></span>
                        </td>
                        <td><%= sdf.format(order.getCreateTime()) %></td>
                        <td>
                            <%
                                // 待支付状态：显示支付和取消按钮
                                if ("0".equals(order.getPayStatus()) && "0".equals(order.getStatus())) {
                            %>
                                <a href="${pageContext.request.contextPath}/user/order/pay?id=<%= order.getId() %>" 
                                   class="btn btn-pay" onclick="return confirm('确认支付 ¥<%= order.getTotalRent() %> 吗？')">立即支付</a>
                                <a href="${pageContext.request.contextPath}/user/order/cancel?id=<%= order.getId() %>" 
                                   class="btn btn-cancel" onclick="return confirm('确定要取消该订单吗？')">取消订单</a>
                            <%
                                } else if ("1".equals(order.getPayStatus()) && "0".equals(order.getStatus())) {
                                    // 已支付待签约状态
                            %>
                                <span class="btn btn-disabled">等待房东确认</span>
                            <%
                                } else if ("1".equals(order.getStatus())) {
                                    // 已签约
                            %>
                                <span class="btn btn-disabled">已签约</span>
                            <%
                                } else if ("2".equals(order.getStatus())) {
                                    // 已结束
                            %>
                                <span class="btn btn-disabled">已完成</span>
                            <%
                                } else if ("3".equals(order.getStatus())) {
                                    // 已取消
                            %>
                                <span class="btn btn-disabled">已取消</span>
                            <%
                                }
                            %>
                        </td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            20
            <%
                } else {
            %>
            <div class="empty-state">
                <div style="font-size: 64px;">📦</div>
                <p>还没有任何订单</p>
                <p><a href="${pageContext.request.contextPath}/user/house/list">去浏览房源 &gt;&gt;</a></p>
            </div>
            <%
                }
            %>
        </div>
    </div>
    
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
</body>
</html>