<?php
require_once __DIR__ . '/auth.php';
require_login();
check_csrf();

$eid = (int)($_POST['equipment_id'] ?? 0);
$loc = $_POST['location_id'] ?: null;

$cur = $pdo->prepare('SELECT location_id FROM equipment WHERE id=?');
$cur->execute([$eid]);
$from = $cur->fetchColumn();

$pdo->prepare('UPDATE equipment SET location_id=? WHERE id=?')->execute([$loc, $eid]);

$pdo->prepare("INSERT INTO equipment_log (equipment_id,user_id,action,notes) VALUES (?,?,?,?)")
    ->execute([$eid, current_user()['id'], 'STATUS_CHANGE', 'Movido de local '.$from.' para '.$loc]);

header('Location: '.base_url('equipment.php'));
