<?php
include("config.php");

$type = $_POST['type'] ?? '';
$action = $_POST['action'] ?? '';

if ($action == 'save') {
    
    // 1. BIO DATA SAVING
    if ($type == 'bio') {
        $full_name = mysqli_real_escape_string($conn, $_POST['full_name']);
        $email     = mysqli_real_escape_string($conn, $_POST['email']);
        $phone     = mysqli_real_escape_string($conn, $_POST['phone']);
        $address   = mysqli_real_escape_string($conn, $_POST['address']);
        $linkedin  = mysqli_real_escape_string($conn, $_POST['linkedin']);
        $github    = mysqli_real_escape_string($conn, $_POST['github']);
        $objective = mysqli_real_escape_string($conn, $_POST['objective']);

        $pic_query = "";
        if (!empty($_FILES['profile_pic']['name'])) {
            $target_dir = "uploads/";
            if (!is_dir($target_dir)) mkdir($target_dir, 0777, true);
            
            $file_ext = pathinfo($_FILES['profile_pic']['name'], PATHINFO_EXTENSION);
            $pic_name = "profile_" . time() . "." . $file_ext;
            $target_file = $target_dir . $pic_name;
            
            if (move_uploaded_file($_FILES['profile_pic']['tmp_name'], $target_file)) {
                $pic_query = ", profile_pic = '$pic_name'";
            }
        }

        // Check if Bio exists (Update) or Create New
        $check = mysqli_query($conn, "SELECT id FROM cv_bio LIMIT 1");
        if (mysqli_num_rows($check) > 0) {
            $sql = "UPDATE cv_bio SET full_name='$full_name', email='$email', phone='$phone', address='$address', linkedin='$linkedin', github='$github', objective='$objective' $pic_query WHERE id=1";
        } else {
            $sql = "INSERT INTO cv_bio (full_name, profile_pic, email, phone, address, linkedin, github, objective) 
                    VALUES ('$full_name', '$pic_name', '$email', '$phone', '$address', '$linkedin', '$github', '$objective')";
        }
        mysqli_query($conn, $sql);

    // 2. EDUCATION SAVING
    } elseif ($type == 'edu') {
        $level = mysqli_real_escape_string($conn, $_POST['degree_level']);
        $board = mysqli_real_escape_string($conn, $_POST['board_university']);
        $inst  = mysqli_real_escape_string($conn, $_POST['institute']);
        $marks = mysqli_real_escape_string($conn, $_POST['marks_gpa']);
        $div   = mysqli_real_escape_string($conn, $_POST['division_grade']);

        $sql = "INSERT INTO cv_education (degree_level, board_university, institute, marks_gpa, division_grade) 
                VALUES ('$level', '$board', '$inst', '$marks', '$div')";
        mysqli_query($conn, $sql);

    // 3. EXPERIENCE SAVING
    } elseif ($type == 'exp') {
        $title   = mysqli_real_escape_string($conn, $_POST['job_title']);
        $comp    = mysqli_real_escape_string($conn, $_POST['company']);
        $loc     = mysqli_real_escape_string($conn, $_POST['location']);
        $start   = $_POST['start_date'];
        $end     = $_POST['end_date'];
        $current = isset($_POST['is_current']) ? 1 : 0;
        $desc    = mysqli_real_escape_string($conn, $_POST['description']);

        $sql = "INSERT INTO cv_experience (job_title, company, location, start_date, end_date, is_current, description) 
                VALUES ('$title', '$comp', '$loc', '$start', '$end', '$current', '$desc')";
        mysqli_query($conn, $sql);

    // 4. PROJECTS SAVING
    } elseif ($type == 'proj') {
        $title = mysqli_real_escape_string($conn, $_POST['project_title']);
        $tech  = mysqli_real_escape_string($conn, $_POST['tech_stack']);
        $link  = mysqli_real_escape_string($conn, $_POST['project_link']);
        $desc  = mysqli_real_escape_string($conn, $_POST['description']);

        $sql = "INSERT INTO cv_projects (project_title, tech_stack, project_link, description) 
                VALUES ('$title', '$tech', '$link', '$desc')";
        mysqli_query($conn, $sql);

    // 5. SKILLS & HOBBIES
    } elseif ($type == 'skill') {
        $skill = mysqli_real_escape_string($conn, $_POST['skill_name']);
        $prof  = $_POST['proficiency'];
        mysqli_query($conn, "INSERT INTO cv_skills (skill_name, proficiency) VALUES ('$skill', '$prof')");
    } elseif ($type == 'hobby') {
        $hobby = mysqli_real_escape_string($conn, $_POST['hobby_name']);
        mysqli_query($conn, "INSERT INTO cv_hobbies (hobby_name) VALUES ('$hobby')");
    }
}

// --- FETCHING DATA TO DISPLAY BACK IN DASHBOARD ---

if ($type == 'bio') {
    $res = mysqli_query($conn, "SELECT * FROM cv_bio LIMIT 1");
    if($row = mysqli_fetch_assoc($res)) {
        if($row['profile_pic']) echo "<img src='uploads/".$row['profile_pic']."' style='width:80px; height:80px; border-radius:50%; object-fit:cover; border:2px solid #38bdf8;'><br>";
        echo "<h4>" . $row['full_name'] . "</h4>";
        echo "<p>" . $row['objective'] . "</p>";
    }
} elseif ($type == 'edu') {
    $res = mysqli_query($conn, "SELECT * FROM cv_education ORDER BY id DESC");
    while($row = mysqli_fetch_assoc($res)) {
        echo "<div class='data-card'><strong>".$row['degree_level']."</strong> from ".$row['institute']." (".$row['division_grade'].")</div>";
    }
} elseif ($type == 'exp') {
    $res = mysqli_query($conn, "SELECT * FROM cv_experience ORDER BY id DESC");
    while($row = mysqli_fetch_assoc($res)) {
        $status = ($row['is_current']) ? "Present" : $row['end_date'];
        echo "<div class='data-card'><strong>".$row['job_title']."</strong> at ".$row['company']." (".$row['start_date']." to ".$status.")</div>";
    }
} elseif ($type == 'proj') {
    $res = mysqli_query($conn, "SELECT * FROM cv_projects ORDER BY id DESC");
    while($row = mysqli_fetch_assoc($res)) {
        echo "<div class='data-card'><strong>".$row['project_title']."</strong> - Stack: ".$row['tech_stack']."</div>";
    }
}
?>