<?php
$CURRENT_SECTION = 'tenant';
include '../inc/header.php';

// 실제 환경에서는 ?id=...로 받아서 DB에서 해당 유저 정보 조회
// 샘플 데이터
$allowed_options = ['사번', '계좌번호']; // 이 테넌트(회사)에서 허용된 옵션

$users = [
    1 => [
        'id' => 1,
        'user_id' => 'kevin',
        'name' => '권혁성',
        'email' => 'kevin@sample.com',
        'phone' => '010-1111-2222',
        'options' => json_encode(['사번' => 'A001', '계좌번호' => '111-2222-3333']),
        'profile_photo_path' => '',
    ],
    2 => [
        'id' => 2,
        'user_id' => 'sally',
        'name' => '김슬기',
        'email' => 'sally@sample.com',
        'phone' => '010-3333-4444',
        'options' => json_encode(['사번' => 'A002', '계좌번호' => '222-3333-4444']),
        'profile_photo_path' => '',
    ],
];

$user_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$user = isset($users[$user_id]) ? $users[$user_id] : null;

if (! $user) {
    echo '<div class="alert alert-danger mt-4 text-center">해당 회원을 찾을 수 없습니다.</div>';
    include '../inc/footer.php';
    exit;
}
$user_options = json_decode($user['options'], true);
?>

    <div class="container" style="max-width:800px; margin-top:40px;">
        <div class="card shadow p-4">
            <h4 class="mb-3 text-center">회원 정보 수정</h4>
            <form id="userEditForm" method="post" action="/tenant/tenant/user_edit_process.php" enctype="multipart/form-data" autocomplete="off">
                <input type="hidden" name="user_id" value="<?= htmlspecialchars($user['id']) ?>">
                <div class="mb-3">
                    <label class="form-label">회원 아이디</label>
                    <input type="text" class="form-control" name="user_id_display" value="<?= htmlspecialchars($user['user_id']) ?>" readonly>
                </div>
                <div class="mb-3">
                    <label class="form-label">이름 <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" name="name" maxlength="100" value="<?= htmlspecialchars($user['name']) ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">이메일 <span class="text-danger">*</span></label>
                    <input type="email" class="form-control" name="email" maxlength="100" value="<?= htmlspecialchars($user['email']) ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">전화번호</label>
                    <input type="text" class="form-control" name="phone" maxlength="30" value="<?= htmlspecialchars($user['phone']) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">프로필 사진</label>
                    <?php if ($user['profile_photo_path']) { ?>
                        <div class="mb-2"><img src="<?= htmlspecialchars($user['profile_photo_path']) ?>" alt="프로필" style="height:40px;"></div>
                    <?php } ?>
                    <input type="file" class="form-control" name="profile_photo" accept="image/*">
                </div>
                <div class="mb-3">
                    <label class="form-label">비밀번호 변경</label>
                    <input type="password" class="form-control" name="password" maxlength="30" placeholder="변경 시 입력">
                </div>
                <div class="mb-3">
                    <label class="form-label">비밀번호 확인</label>
                    <input type="password" class="form-control" name="password2" maxlength="30" placeholder="변경 시 입력">
                </div>
                <?php foreach ($allowed_options as $opt) { ?>
                    <div class="mb-3">
                        <label class="form-label"><?= htmlspecialchars($opt) ?></label>
                        <input type="text" class="form-control" name="option_<?= urlencode($opt) ?>" value="<?= isset($user_options[$opt]) ? htmlspecialchars($user_options[$opt]) : '' ?>">
                    </div>
                <?php } ?>
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-primary w-50">수정</button>
                    <a href="/tenant/tenant/user_list.php" class="btn btn-secondary w-50">취소</a>
                </div>
            </form>
        </div>
    </div>

    <script>
        $(function(){
            $('#userEditForm').on('submit', function(e){
                var name = $('[name="name"]').val().trim();
                var email = $('[name="email"]').val().trim();
                var pw1 = $('[name="password"]').val();
                var pw2 = $('[name="password2"]').val();
                if (name.length < 2) {
                    alert('이름은 2글자 이상 입력하세요.');
                    $('[name="name"]').focus();
                    e.preventDefault(); return false;
                }
                if (!email || !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) {
                    alert('이메일을 올바르게 입력하세요.');
                    $('[name="email"]').focus();
                    e.preventDefault(); return false;
                }
                if (pw1 || pw2) {
                    if (pw1.length < 4) {
                        alert('비밀번호는 4글자 이상이어야 합니다.');
                        $('[name="password"]').focus();
                        e.preventDefault(); return false;
                    }
                    if (pw1 !== pw2) {
                        alert('비밀번호가 일치하지 않습니다.');
                        $('[name="password2"]').focus();
                        e.preventDefault(); return false;
                    }
                }
            });
        });
    </script>

<?php include '../inc/footer.php'; ?>
