<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="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;
    }
    
    House house = (House) request.getAttribute("house");
    if (house == null) {
        response.sendRedirect(request.getContextPath() + "/user/house/list");
        return;
    }
    
    String error = (String) request.getAttribute("error");
%>
<!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: 800px;
            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 {
            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;
        }
        
        .main-card {
            background-color: white;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .house-summary {
            background-color: #f9f9f9;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 25px;
            display: flex;
            gap: 15px;
        }
        
        .house-img {
            width: 100px;
            height: 100px;
            background-color: #e0e0e0;
            background-size: cover;
            background-position: center;
            border-radius: 8px;
        }
        
        .house-info {
            flex: 1;
        }
        
        .house-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 8px;
        }
        
        .house-detail {
            color: #666;
            font-size: 14px;
            margin-bottom: 5px;
        }
        
        .house-price {
            color: #f44336;
            font-size: 18px;
            font-weight: bold;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
        }
        
        input[type="date"],
        input[type="time"],
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        
        textarea {
            resize: vertical;
            min-height: 80px;
        }
        
        .error {
            background-color: #ffebee;
            color: #c62828;
            padding: 12px;
            border-radius: 4px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-primary {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #45a049;
        }
        
        .btn-secondary {
            background-color: #999;
            color: white;
            margin-left: 10px;
        }
        
        .btn-secondary:hover {
            background-color: #777;
        }
        
        .hint {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }
        
        .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">我的订单</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="breadcrumb">
            <a href="${pageContext.request.contextPath}/user/house/list">首页</a> &gt;
            <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>">房源详情</a> &gt;
            <span>预约看房</span>
        </div>
        
        <div class="main-card">
            <h2 style="margin-bottom: 20px;">📅 预约看房</h2>
            
            <% if (error != null) { %>
                <div class="error"><%= error %></div>
            <% } %>
            
            <!-- 房源信息摘要 -->
            <div class="house-summary">
                <div class="house-img" style="background-image: url('/HRS/<%= house.getFirstImage() != null ? house.getFirstImage() : "" %>');">
                    <% if (house.getFirstImage() == null) { %>
                        <div style="display:flex;align-items:center;justify-content:center;height:100%;color:#999;">🏠</div>
                    <% } %>
                </div>
                <div class="house-info">
                    <div class="house-title"><%= house.getTitle() %></div>
                    <div class="house-detail">📍 <%= house.getArea() %> | <%= house.getHouseType() %> | <%= house.getRentTypeText() %></div>
                    <div class="house-price">¥<%= house.getRentPrice() %>/月</div>
                </div>
            </div>
            
            <form action="${pageContext.request.contextPath}/user/reservation/add" method="post">
                <input type="hidden" name="houseId" value="<%= house.getId() %>">
                
                <div class="form-group">
                    <label>选择看房日期 *</label>
                    <input type="date" name="reserveDate" required min="<%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()) %>">
                    <div class="hint">请选择您方便的看房日期</div>
                </div>
                
                <div class="form-group">
                    <label>选择看房时间 *</label>
                    <input type="time" name="reserveTime" required>
                    <div class="hint">建议选择 9:00-18:00 之间的时间</div>
                </div>
                
                <div class="form-group">
                    <label>备注信息（可选）</label>
                    <textarea name="remark" placeholder="请输入您的特殊需求或问题..."></textarea>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">提交预约</button>
                    <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>" class="btn btn-secondary">返回</a>
                </div>
            </form>
        </div>
    </div>
    
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
    
    <script>
        // 设置日期最小值
        var dateInput = document.querySelector('input[type="date"]');
        var today = new Date();
        var yyyy = today.getFullYear();
        var mm = String(today.getMonth() + 1).padStart(2, '0');
        var dd = String(today.getDate()).padStart(2, '0');
        dateInput.min = yyyy + '-' + mm + '-' + dd;
        
        // 设置时间默认值
        var timeInput = document.querySelector('input[type="time"]');
        timeInput.value = "10:00";
    </script>
</body>
</html>