<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.hrs.model.entity.House, com.hrs.model.entity.User, java.text.SimpleDateFormat, java.util.Date" %>
<%
    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");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(new Date());
%>
<!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: 1000px;
            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);
        }
        
        h2 {
            margin-bottom: 20px;
            color: #333;
        }
        
        .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: 20px;
            font-weight: bold;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
        }
        
        input[type="date"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        
        .price-preview {
            background-color: #fff3e0;
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
            text-align: center;
        }
        
        .total-price {
            font-size: 28px;
            color: #f44336;
            font-weight: bold;
        }
        
        .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;
        }
        
        .row {
            display: flex;
            gap: 20px;
        }
        
        .row .form-group {
            flex: 1;
        }
        
        .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>📝 创建租赁订单</h2>
            
            <% if (error != null) { %>
                <div class="error"><%= error %></div>
            <% } %>
            
            <!-- 房源信息摘要 -->
            <div class="house-summary">
                <div class="house-img" style="background-image: url('<%= 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/order/create" method="post" id="orderForm">
                <input type="hidden" name="houseId" value="<%= house.getId() %>">
                
                <div class="row">
                    <div class="form-group">
                        <label>租赁开始日期 *</label>
                        <input type="date" name="startDate" id="startDate" required min="<%= today %>" onchange="calculateTotal()">
                    </div>
                    <div class="form-group">
                        <label>租赁结束日期 *</label>
                        <input type="date" name="endDate" id="endDate" required min="<%= today %>" onchange="calculateTotal()">
                    </div>
                </div>
                
                <div class="price-preview">
                    <div style="margin-bottom: 10px;">预计总租金</div>
                    <div class="total-price" id="totalPrice">¥0.00</div>
                    <div class="hint">* 租金按月租比例计算，实际金额以合同为准</div>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn btn-primary" id="submitBtn" disabled>确认下单</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 monthlyRent = <%= house.getRentPrice() %>;
        
        function calculateTotal() {
            var startDate = document.getElementById('startDate').value;
            var endDate = document.getElementById('endDate').value;
            var submitBtn = document.getElementById('submitBtn');
            var totalPriceSpan = document.getElementById('totalPrice');
            
            if (startDate && endDate) {
                var start = new Date(startDate);
                var end = new Date(endDate);
                
                if (end < start) {
                    totalPriceSpan.innerHTML = '¥0.00';
                    submitBtn.disabled = true;
                    return;
                }
                
                // 计算天数
                var diffTime = Math.abs(end - start);
                var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
                
                // 计算租金（按比例）
                var total = (monthlyRent / 30) * diffDays;
                totalPriceSpan.innerHTML = '¥' + total.toFixed(2);
                submitBtn.disabled = false;
            } else {
                totalPriceSpan.innerHTML = '¥0.00';
                submitBtn.disabled = true;
            }
        }
        
        // 设置结束日期最小值跟随开始日期
        document.getElementById('startDate').addEventListener('change', function() {
            var endDateInput = document.getElementById('endDate');
            endDateInput.min = this.value;
            if (endDateInput.value && endDateInput.value < this.value) {
                endDateInput.value = '';
            }
            calculateTotal();
        });
    </script>
</body>
</html>