<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/admin/system/settings/db.php'; 
require __DIR__ . '/vendor/autoload.php';
session_start();
if(!$_SESSION['memberId']){
header('Location: /admin/login');
exit;
}else{
/////////////////////////////////////////////
/////////////////////////////////////////////
$eventId = 'v07vut5lo4dju2841rh3a17fnc'; 
/////////////////////////////////////////////
/////////////////////////////////////////////

$calisanID = $_SESSION['memberId'];
$kontrol = $DB->prepare("SELECT * FROM provider WHERE id = ?");
$kontrol->execute(array($calisanID));
$kontrol = $kontrol ->fetch(PDO::FETCH_ASSOC);

$settings = $DB->prepare("SELECT * FROM settings WHERE id = ?");
$settings->execute(array('1'));
$settings = $settings ->fetch(PDO::FETCH_ASSOC);


$client = new Google_Client();
$client->setClientId($settings['GoogleCalendarClientId']);
$client->setClientSecret($settings['GoogleCalendarClientSecret']);
$client->setRedirectUri('https://'.$_SERVER['SERVER_NAME'].'/admin/GoogleConnect');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->addScope(Google_Service_Calendar::CALENDAR);


$token = $kontrol['GoogleCalendarToken'];
$getRefreshToken = $kontrol['GoogleCalendarRefresh'];
$client->setAccessToken($token);


if ($client->isAccessTokenExpired() && $getRefreshToken) {
    $client->fetchAccessTokenWithRefreshToken($getRefreshToken);
    $newToken = $client->getAccessToken();
    $guncelle = $DB->prepare("UPDATE provider set GoogleCalendarToken=? WHERE id=?");
    $guncelle -> execute(array(json_encode($newToken),$calisanID));
    echo "token yenilendi<br>";
}

$service = new Google_Service_Calendar($client);
$event = $service->events->get('primary', $eventId);


$event->setSummary('Güncellenmiş Etkinlik Adı1');
$event->setDescription('Güncellenmiş Etkinlik Detayları1');

$newStartDateTime = new Google\Service\Calendar\EventDateTime();
$newStartDateTime->setDateTime('2024-01-06T15:00:00');
$newStartDateTime->setTimeZone('Europe/Istanbul');

$newEndDateTime = new Google\Service\Calendar\EventDateTime();
$newEndDateTime->setDateTime('2024-01-06T16:00:00');
$newEndDateTime->setTimeZone('Europe/Istanbul');

$event->setStart($newStartDateTime);
$event->setEnd($newEndDateTime);

$updatedEvent = $service->events->update('primary', $eventId, $event);


echo 'Etkinlik başarıyla güncellendi.';
}
?>