<?php
/**
 * my/profile_edit.php — 회원정보 수정
 */
require_once __DIR__ . '/../config/bootstrap.php';
Auth::requireLogin();

$page_title = '회원정보 수정';
$active_tab = 'mypage';

$user = Auth::user();


$genders = [
    ['id' => 'gender_male',   'value' => 'male',   'label' => '남성'],
    ['id' => 'gender_female', 'value' => 'female', 'label' => '여성'],
    ['id' => 'gender_none',   'value' => 'none',   'label' => '선택 안 함'],
];
$user['gender'] = $user['gender'] ?? 'none';

include __DIR__ . '/../head.php';
?>

<style>
    @media screen and (max-width: 768px) {
        .site-footer {display: none;}
    }
</style>

<section class="mypage-page profile-edit-page">
    <div class="inner">
        <form class="profile-edit-form" id="profileEditForm" action="/my/profile_edit_update.php" method="post" enctype="multipart/form-data">
            <?= csrf_field() ?>
            <div class="profile-edit-wrap">

                <div class="profile-edit-photo-box">
                    <label for="profile_photo" class="profile-edit-photo-label">
                        <span class="profile-edit-photo-wrap">
                            <img class="profile-edit-photo-thumb" src="<?= !empty($user['profile_img']) ? htmlspecialchars($user['profile_img']) : '/img/default-profile.webp' ?>" alt="프로필 이미지" onerror="this.src='/img/default-profile.webp'">
                            <svg class="camera-ico" width="28" height="28" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <rect x="1" y="1" width="26" height="26" rx="13" fill="#FF4D4F"/>
                                <rect x="1" y="1" width="26" height="26" rx="13" stroke="white" stroke-width="2"/>
                                <g clip-path="url(#clip0_2141_256)">
                                <path d="M19.9557 17.7917C19.9557 18.079 19.8416 18.3545 19.6384 18.5577C19.4353 18.7609 19.1597 18.875 18.8724 18.875H9.1224C8.83508 18.875 8.55953 18.7609 8.35636 18.5577C8.1532 18.3545 8.03906 18.079 8.03906 17.7917V11.8333C8.03906 11.546 8.1532 11.2705 8.35636 11.0673C8.55953 10.8641 8.83508 10.75 9.1224 10.75H11.2891L12.3724 9.125H15.6224L16.7057 10.75H18.8724C19.1597 10.75 19.4353 10.8641 19.6384 11.0673C19.8416 11.2705 19.9557 11.546 19.9557 11.8333V17.7917Z" stroke="white" stroke-width="1.19167" stroke-linecap="round" stroke-linejoin="round"/>
                                <path d="M14.0026 16.7083C15.1992 16.7083 16.1693 15.7383 16.1693 14.5417C16.1693 13.345 15.1992 12.375 14.0026 12.375C12.806 12.375 11.8359 13.345 11.8359 14.5417C11.8359 15.7383 12.806 16.7083 14.0026 16.7083Z" stroke="white" stroke-width="1.19167" stroke-linecap="round" stroke-linejoin="round"/>
                                </g>
                                <defs>
                                <clipPath id="clip0_2141_256">
                                <rect width="13" height="13" fill="white" transform="translate(7.5 7.5)"/>
                                </clipPath>
                                </defs>
                            </svg>
                        </span>
                        <strong>프로필 사진 변경</strong>
                    </label>
                    <input type="file" id="profile_photo" name="profile_photo" accept="image/jpeg,image/png">
                </div>

                <div class="profile-edit-line"></div>

                <div class="profile-edit-section">
                    <label for="profile_name" class="profile-edit-label required">이름</label>
                    <div class="profile-edit-input-box">
                        <input type="text" id="profile_name" name="profile_name" value="<?= htmlspecialchars($user['name']) ?>" required>
                    </div>
                </div>

                <div class="profile-edit-section">
                    <label for="profile_nickname" class="profile-edit-label required">닉네임</label>
                    <div class="profile-edit-input-box">
                        <input type="text" id="profile_nickname" name="profile_nickname" value="<?= htmlspecialchars($user['nickname']) ?>" required>
                    </div>
                </div>

                <div class="profile-edit-section">
                    <label for="profile_email" class="profile-edit-label required">이메일</label>
                    <div class="profile-edit-input-box">
                        <input type="email" id="profile_email" name="profile_email" value="<?= htmlspecialchars($user['email']) ?>" required>
                    </div>
                    <p class="profile-edit-desc">이메일 변경 시 로그인에 사용되는 이메일도 함께 변경됩니다</p>
                </div>

                <div class="profile-edit-section">
                    <label for="profile_phone" class="profile-edit-label">휴대폰 번호</label>
                    <div class="profile-edit-input-box">
                        <input type="tel" id="profile_phone" name="profile_phone" value="<?= htmlspecialchars($user['phone']) ?>">
                    </div>
                </div>

                <div class="profile-edit-section">
                    <strong class="profile-edit-label">성별</strong>
                    <div class="profile-edit-gender-list">
                        <?php foreach ($genders as $gender): ?>
                            <div class="custom-radio profile-edit-gender-item">
                                <input
                                    type="radio"
                                    name="gender"
                                    id="<?= htmlspecialchars($gender['id']) ?>"
                                    value="<?= htmlspecialchars($gender['value']) ?>"
                                    <?= $user['gender'] === $gender['value'] ? 'checked' : '' ?>
                                >
                                <label for="<?= htmlspecialchars($gender['id']) ?>">
                                    <?= htmlspecialchars($gender['label']) ?>
                                </label>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>

                <?php
                $birth_year = (int)str_replace('년', '', $user['birth_year']);
                $birth_month = (int)str_replace('월', '', $user['birth_month']);
                $birth_day = (int)str_replace('일', '', $user['birth_day']);

                $current_year = (int)date('Y');
                ?>

                <div class="profile-edit-section">
                    <strong class="profile-edit-label">생년월일</strong>

                    <div class="profile-edit-birth-list">
                        <span class="custom-select profile-edit-birth-select">
                            <select name="birth_year" id="birth_year">
                                <?php for ($year = $current_year; $year >= 1950; $year--): ?>
                                    <option value="<?= $year ?>" <?= $birth_year === $year ? 'selected' : '' ?>>
                                        <?= $year ?>년
                                    </option>
                                <?php endfor; ?>
                            </select>
                        </span>

                        <span class="custom-select profile-edit-birth-select">
                            <select name="birth_month" id="birth_month">
                                <?php for ($month = 1; $month <= 12; $month++): ?>
                                    <option value="<?= $month ?>" <?= $birth_month === $month ? 'selected' : '' ?>>
                                        <?= $month ?>월
                                    </option>
                                <?php endfor; ?>
                            </select>
                        </span>

                        <span class="custom-select profile-edit-birth-select">
                            <select name="birth_day" id="birth_day">
                                <?php for ($day = 1; $day <= 31; $day++): ?>
                                    <option value="<?= $day ?>" <?= $birth_day === $day ? 'selected' : '' ?>>
                                        <?= $day ?>일
                                    </option>
                                <?php endfor; ?>
                            </select>
                        </span>
                    </div>
                </div>

                <div class="profile-edit-bottom">
                    <button type="submit" class="square-btn big primary-btn profile-edit-submit">
                        저장하기
                    </button>
                </div>

            </div>
        </form>
    </div>
</section>

<script>
(function () {
    // 프로필 사진 선택 시 즉시 미리보기
    var photoInput = document.getElementById('profile_photo');
    var photoThumb = document.querySelector('.profile-edit-photo-thumb');
    if (photoInput && photoThumb) {
        photoInput.addEventListener('change', function () {
            var f = this.files && this.files[0];
            if (!f) return;
            if (f.size > 5 * 1024 * 1024) { alert('이미지는 5MB 이하만 업로드 가능합니다.'); this.value = ''; return; }
            var reader = new FileReader();
            reader.onload = function (e) { photoThumb.src = e.target.result; };
            reader.readAsDataURL(f);
        });
    }
    // 제출 전 간단 검증
    var form = document.getElementById('profileEditForm');
    if (form) {
        form.addEventListener('submit', function (e) {
            var nick = document.getElementById('profile_nickname');
            if (nick && (nick.value.trim().length < 2 || nick.value.trim().length > 10)) {
                e.preventDefault();
                alert('닉네임은 2~10자로 입력해주세요.');
                nick.focus();
            }
        });
    }
})();
</script>

<?php include __DIR__ . '/../tail.php'; ?>