<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__.'/../../includes/auth.php';
requireLogin();

require_once __DIR__.'/../../config/database.php';

$meterId = isset($_GET['meter_id']) ? (int)$_GET['meter_id'] : 0;

if(!$meterId){
    die("Invalid meter");
}

try {

    $pdo->beginTransaction();

    /* CLOSE ACTIVE TENANT */
    $stmt = $pdo->prepare("
    UPDATE meter_tenants
    SET active = 0, end_date = CURDATE()
    WHERE meter_id = ? AND active = 1
    ");
    $stmt->execute([$meterId]);

    $pdo->commit();

    header("Location: meters.php?success=tenant_moved_out");
    exit;

} catch(Exception $e){

    $pdo->rollBack();
    die($e->getMessage());
}