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

    <div class="d-flex justify-content-center align-items-center" style="min-height: 70vh;">
        <div class="card shadow p-4" style="width: 380px;">
            <form id="withdrawForm" method="post" action="/tenant/member/logout.php" autocomplete="off">
                <h3 class="text-center mb-4 text-danger">회원 탈퇴</h3>
                <p class="text-center mb-3">
                    정말로 회원 탈퇴를 진행하시겠습니까?<br>
                    <span class="text-danger fw-bold">탈퇴 시 모든 정보가 삭제됩니다.</span>
                </p>
                <div class="mb-3">
                    <label for="reason" class="form-label">탈퇴 사유</label>
                    <select class="form-select" id="reason" name="reason" required>
                        <option value="">사유를 선택하세요</option>
                        <option value="서비스 불만족">서비스 불만족</option>
                        <option value="사용 빈도 낮음">사용 빈도 낮음</option>
                        <option value="개인정보 우려">개인정보 우려</option>
                        <option value="기타">기타 (직접 입력)</option>
                    </select>
                </div>
                <div class="mb-3" id="reasonEtcDiv" style="display:none;">
                    <input type="text" class="form-control" id="reason_etc" name="reason_etc" maxlength="100" placeholder="탈퇴 사유를 입력하세요">
                </div>
                <div class="mb-4">
                    <label for="password" class="form-label">비밀번호 확인</label>
                    <input type="password" class="form-control" id="password" name="password" maxlength="30" required placeholder="비밀번호를 입력하세요">
                </div>
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-danger w-50">탈퇴</button>
                    <a href="/tenant/member/dashboard.php" class="btn btn-secondary w-50">취소</a>
                </div>
            </form>
        </div>
    </div>

    <script>
        $(function(){
            // 탈퇴 사유 기타 선택시 입력 필드 노출
            $('#reason').on('change', function(){
                if ($(this).val() === '기타') {
                    $('#reasonEtcDiv').show();
                    $('#reason_etc').prop('required', true);
                } else {
                    $('#reasonEtcDiv').hide();
                    $('#reason_etc').prop('required', false);
                }
            });

            // 밸리데이션
            $('#withdrawForm').on('submit', function(e){
                var pw = $('#password').val();
                var reason = $('#reason').val();
                var reasonEtc = $('#reason_etc').val();

                if (!reason) {
                    alert('탈퇴 사유를 선택하세요.');
                    $('#reason').focus();
                    e.preventDefault(); return false;
                }
                if (reason === '기타' && (!reasonEtc || reasonEtc.trim().length < 2)) {
                    alert('탈퇴 사유를 입력하세요.');
                    $('#reason_etc').focus();
                    e.preventDefault(); return false;
                }
                if (!pw || pw.length < 4) {
                    alert('비밀번호를 올바르게 입력하세요.');
                    $('#password').focus();
                    e.preventDefault(); return false;
                }
                // 서버 전송
            });
        });
    </script>

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