<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.hrs.model.entity.User" %>
<%
    User loginUser = (User) session.getAttribute("loginUser");
    if (loginUser == null || !"1".equals(loginUser.getRoleType())) {
        response.sendRedirect(request.getContextPath() + "/user/login");
        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: #ff9800;
            color: white;
            padding: 15px 0;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .container {
            width: 900px;
            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: #fb8c00;
            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: #ff9800;
            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;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
        }
        
        input[type="text"],
        input[type="number"],
        select,
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        
        input[type="file"] {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            width: 100%;
        }
        
        textarea {
            resize: vertical;
            min-height: 100px;
        }
        
        .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: #ff9800;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #fb8c00;
        }
        
        .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}/landlord/house/list">房源管理</a>
                <a href="${pageContext.request.contextPath}/landlord/reservation/list">预约管理</a>
                <a href="${pageContext.request.contextPath}/landlord/order/list">订单管理</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="breadcrumb">
            <a href="${pageContext.request.contextPath}/landlord/house/list">房源管理</a> &gt;
            <span>发布房源</span>
        </div>
        
        <div class="main-card">
            <h2>📝 发布新房源</h2>
            
            <% if (error != null) { %>
                <div class="error"><%= error %></div>
            <% } %>
            
            <form action="${pageContext.request.contextPath}/landlord/house/add" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label>房源标题 *</label>
                    <input type="text" name="title" placeholder="例如：朝阳区温馨一居室" required>
                </div>
                
                <div class="row">
                    <div class="form-group">
                        <label>所在区域</label>
                        <select name="area">
                            <option value="朝阳区">朝阳区</option>
                            <option value="海淀区">海淀区</option>
                            <option value="东城区">东城区</option>
                            <option value="西城区">西城区</option>
                            <option value="丰台区">丰台区</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>户型</label>
                        <select name="houseType">
                            <option value="一室一厅">一室一厅</option>
                            <option value="两室一厅">两室一厅</option>
                            <option value="两室两厅">两室两厅</option>
                            <option value="三室一厅">三室一厅</option>
                            <option value="三室两厅">三室两厅</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>租赁类型</label>
                        <select name="rentType">
                            <option value="0">整租</option>
                            <option value="1">合租</option>
                        </select>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>详细地址</label>
                    <input type="text" name="address" placeholder="请输入详细地址">
                </div>
                
                <div class="row">
                    <div class="form-group">
                        <label>月租金（元）*</label>
                        <input type="number" name="rentPrice" placeholder="例如：2500" required>
                    </div>
                    <div class="form-group">
                        <label>配套设施</label>
                        <input type="text" name="facility" placeholder="例如：空调,洗衣机,热水器,WiFi">
                        <div class="hint">多个设施用英文逗号分隔</div>
                    </div>
                </div>
                
                <!-- 房源图片上传字段 -->
                <div class="form-group">
                    <label>房源图片</label>
                    <input type="file" name="images" multiple accept="image/jpeg,image/jpg,image/png,image/gif" id="imageInput">
                    <div class="hint">支持jpg、png、gif格式，可多选，单张不超过5MB</div>
                </div>
                
                <!-- 图片预览区域 -->
                <div class="form-group" id="previewArea" style="display:none;">
                    <label>图片预览</label>
                    <div id="imagePreview" style="display:flex; gap:10px; flex-wrap:wrap;"></div>
                </div>
                
                <div class="form-group">
                    <label>房源描述</label>
                    <textarea name="description" placeholder="请输入房源描述信息..."></textarea>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">提交审核</button>
                    <a href="${pageContext.request.contextPath}/landlord/house/list" class="btn btn-secondary">返回</a>
                </div>
                
                <div class="hint" style="margin-top: 15px; text-align: center;">
                    房源提交后将进入审核状态，审核通过后即可在租客端展示
                </div>
            </form>
        </div>
    </div>
    
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
    
    <script>
        // 图片预览
        document.getElementById('imageInput').addEventListener('change', function(e) {
            var previewArea = document.getElementById('previewArea');
            var previewContainer = document.getElementById('imagePreview');
            previewContainer.innerHTML = '';
            
            if (this.files.length > 0) {
                previewArea.style.display = 'block';
                
                for (var i = 0; i < this.files.length; i++) {
                    var file = this.files[i];
                    var reader = new FileReader();
                    
                    reader.onload = function(e) {
                        var img = document.createElement('img');
                        img.src = e.target.result;
                        img.style.width = '100px';
                        img.style.height = '80px';
                        img.style.objectFit = 'cover';
                        img.style.borderRadius = '4px';
                        img.style.border = '1px solid #ddd';
                        previewContainer.appendChild(img);
                    };
                    
                    reader.readAsDataURL(file);
                }
            } else {
                previewArea.style.display = 'none';
            }
        });
    </script>
</body>
</html>