<?php

require_once 'includes/provider_auth.php';
require_once '../config/constants.php';

if(session_status() === PHP_SESSION_NONE){
    session_start();
}

$token = $_SESSION['provider_token'] ?? '';

$name = '';
$email = '';
$phone = '';
$photo = '';
$desc = '';

if($token){

    $api = API_URL."providers/full-profile.php";

    $ch = curl_init();

    curl_setopt_array($ch,[
        CURLOPT_URL => $api,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'X-API-KEY: '.API_KEY,
            'Authorization: Bearer '.$token
        ]
    ]);

    $res = curl_exec($ch);

    if(curl_errno($ch)){
        die("API Error: ".curl_error($ch));
    }

    curl_close($ch);

    $response = json_decode($res,true);

    if(isset($response['status']) && $response['status'] === 'success'){

        $provider = $response['data']['provider'] ?? [];

        $name = $provider['name'] ?? '';
        $email = $provider['email'] ?? '';
        $phone = $provider['phone'] ?? '';
        $photo = $provider['profile_photo'] ?? '';
        $desc = $provider['description'] ?? '';
    }
}

?>

<?php include './partials/layouts/layoutTop.php' ?>

<div class="dashboard-main-body">

<div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">
<h6 class="fw-semibold mb-0">View Profile</h6>
</div>

<div class="row gy-4">

<!-- LEFT PROFILE CARD -->

<div class="col-lg-4">

<div class="user-grid-card position-relative border radius-16 overflow-hidden bg-base h-100">

<img src="assets/images/user-grid/user-grid-bg1.png" class="w-100 object-fit-cover">

<div class="pb-24 ms-16 mb-24 me-16 mt--100">

<div class="text-center border border-top-0 border-start-0 border-end-0">

<img src="<?php echo $photo ? UPLOAD_URL.$photo : 'assets/images/user-grid/user-grid-img14.png' ?>"
class="border br-white border-width-2-px w-200-px h-200-px rounded-circle object-fit-cover">

<h6 class="mb-0 mt-16"><?php echo htmlspecialchars($name) ?></h6>

<span class="text-secondary-light mb-16"><?php echo htmlspecialchars($email) ?></span>

</div>

<div class="mt-24">

<h6 class="text-xl mb-16">Personal Info</h6>

<ul>

<li class="d-flex align-items-center gap-1 mb-12">
<span class="w-30 text-md fw-semibold text-primary-light">Full Name</span>
<span class="w-70 text-secondary-light fw-medium">: <?php echo htmlspecialchars($name) ?></span>
</li>

<li class="d-flex align-items-center gap-1 mb-12">
<span class="w-30 text-md fw-semibold text-primary-light">Email</span>
<span class="w-70 text-secondary-light fw-medium">: <?php echo htmlspecialchars($email) ?></span>
</li>

<li class="d-flex align-items-center gap-1 mb-12">
<span class="w-30 text-md fw-semibold text-primary-light">Phone</span>
<span class="w-70 text-secondary-light fw-medium">: <?php echo htmlspecialchars($phone) ?></span>
</li>

<li class="d-flex align-items-center gap-1">
<span class="w-30 text-md fw-semibold text-primary-light">Description</span>
<span class="w-70 text-secondary-light fw-medium">: <?php echo htmlspecialchars($desc) ?></span>
</li>

</ul>

</div>

</div>
</div>

</div>


<!-- RIGHT PROFILE EDIT -->

<div class="col-lg-8">

<div class="card h-100">

<div class="card-body p-24">

<ul class="nav border-gradient-tab nav-pills mb-20 d-inline-flex">
<li class="nav-item">
<button class="nav-link active">Edit Profile</button>
</li>
</ul>

<form id="profileForm">

<div class="row">

<div class="col-sm-6">
<div class="mb-20">
<label class="form-label fw-semibold text-primary-light text-sm mb-8">Full Name</label>
<input type="text" class="form-control radius-8" id="name"
value="<?php echo htmlspecialchars($name) ?>">
</div>
</div>

<div class="col-sm-6">
<div class="mb-20">
<label class="form-label fw-semibold text-primary-light text-sm mb-8">Email</label>
<input type="email" class="form-control radius-8" id="email"
value="<?php echo htmlspecialchars($email) ?>">
</div>
</div>

<div class="col-sm-6">
<div class="mb-20">
<label class="form-label fw-semibold text-primary-light text-sm mb-8">Phone</label>
<input type="text" class="form-control radius-8" id="phone"
value="<?php echo htmlspecialchars($phone) ?>">
</div>
</div>

<div class="col-sm-12">
<div class="mb-20">
<label class="form-label fw-semibold text-primary-light text-sm mb-8">Description</label>
<textarea class="form-control radius-8" id="description"><?php echo htmlspecialchars($desc) ?></textarea>
</div>
</div>

</div>

<div class="d-flex align-items-center justify-content-center gap-3">

<button type="button" class="btn btn-primary px-56 py-12 radius-8" id="saveProfile">
Save Profile
</button>

</div>

</form>

</div>
</div>
</div>

</div>
</div>


<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script>

$("#saveProfile").click(function(){

$.ajax({

url:"<?php echo BASE_URL ?>api/providers/update-profile.php",

method:"POST",

dataType:"json",

headers:{
'X-API-KEY':'<?php echo API_KEY ?>',
'Authorization':'Bearer <?php echo $_SESSION["provider_token"] ?>'
},

data:{
name:$("#name").val(),
email:$("#email").val(),
phone:$("#phone").val(),
description:$("#description").val()
},

success:function(res){

if(res.status === "success"){
alert("Profile updated successfully");
location.reload();
}else{
alert(res.message);
}

},

error:function(){
alert("Server error");
}

});

});

</script>

<?php include './partials/layouts/layoutBottom.php' ?>