<?php
/**
 * Enterprise HRM ERP System - View Employee Transfer
 */

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

$pageTitle = 'View Transfer';
$currentModule = 'transfer';

$siteId = getCurrentSiteId();
$transferId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($transferId <= 0) {
    redirect(BASE_URL . '/modules/transfer/list.php', 'Invalid transfer ID.', 'error');
}

try {
    $db = getDB();
    $stmt = $db->prepare("
        SELECT t.*, e.employee_code, e.first_name, e.last_name, e.middle_name,
               fs.site_name as from_site, ts.site_name as to_site,
               fd.department_name as from_dept, td.department_name as to_dept,
               fdes.designation_name as from_desig, tdes.designation_name as to_desig,
               u.full_name as created_by_name
        FROM employee_transfers t
        INNER JOIN employees e ON t.employee_id = e.employee_id
        INNER JOIN sites fs ON t.from_site_id = fs.site_id
        INNER JOIN sites ts ON t.to_site_id = ts.site_id
        LEFT JOIN departments fd ON t.old_department_id = fd.department_id
        LEFT JOIN departments td ON t.new_department_id = td.department_id
        LEFT JOIN designations fdes ON t.old_designation_id = fdes.designation_id
        LEFT JOIN designations tdes ON t.new_designation_id = tdes.designation_id
        LEFT JOIN users u ON t.created_by = u.user_id
        WHERE t.transfer_id = ?
    ");
    $stmt->execute([$transferId]);
    $transfer = $stmt->fetch();
    
    if (!$transfer) {
        redirect(BASE_URL . '/modules/transfer/list.php', 'Transfer not found.', 'error');
    }
    
} catch (Exception $e) {
    error_log("Error loading transfer: " . $e->getMessage());
    redirect(BASE_URL . '/modules/transfer/list.php', 'Error loading transfer data.', 'error');
}

include __DIR__ . '/../../includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Employee Transfer Details</h2>
        <a href="<?php echo BASE_URL; ?>/modules/transfer/list.php" class="btn btn-secondary">Back to Transfers</a>
    </div>
    
    <div style="margin-top: 20px;">
        <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px;">
            <div>
                <h3 style="margin-bottom: 15px;">Employee Information</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Employee Code</strong></td>
                        <td><?php echo htmlspecialchars($transfer['employee_code']); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Employee Name</strong></td>
                        <td><?php echo htmlspecialchars(trim($transfer['first_name'] . ' ' . ($transfer['middle_name'] ?? '') . ' ' . $transfer['last_name'])); ?></td>
                    </tr>
                </table>
            </div>
            
            <div>
                <h3 style="margin-bottom: 15px;">Transfer Details</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Transfer Date</strong></td>
                        <td><?php echo formatDate($transfer['transfer_date']); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Status</strong></td>
                        <td>
                            <span class="status-badge status-<?php echo $transfer['transfer_status']; ?>">
                                <?php echo ucfirst($transfer['transfer_status']); ?>
                            </span>
                        </td>
                    </tr>
                    <?php if ($transfer['approved_at']): ?>
                    <tr>
                        <td><strong>Approved At</strong></td>
                        <td><?php echo formatDateTime($transfer['approved_at']); ?></td>
                    </tr>
                    <?php endif; ?>
                </table>
            </div>
            
            <div>
                <h3 style="margin-bottom: 15px;">From</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Site</strong></td>
                        <td><?php echo htmlspecialchars($transfer['from_site']); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Department</strong></td>
                        <td><?php echo htmlspecialchars($transfer['from_dept'] ?? 'N/A'); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Designation</strong></td>
                        <td><?php echo htmlspecialchars($transfer['from_desig'] ?? 'N/A'); ?></td>
                    </tr>
                </table>
            </div>
            
            <div>
                <h3 style="margin-bottom: 15px;">To</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Site</strong></td>
                        <td><?php echo htmlspecialchars($transfer['to_site']); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Department</strong></td>
                        <td><?php echo htmlspecialchars($transfer['to_dept'] ?? 'N/A'); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Designation</strong></td>
                        <td><?php echo htmlspecialchars($transfer['to_desig'] ?? 'N/A'); ?></td>
                    </tr>
                </table>
            </div>
        </div>
        
        <?php if ($transfer['transfer_reason']): ?>
        <div style="margin-top: 30px;">
            <h3 style="margin-bottom: 15px;">Transfer Reason</h3>
            <p style="padding: 15px; background: #f9fafb; border-radius: 6px;">
                <?php echo nl2br(htmlspecialchars($transfer['transfer_reason'])); ?>
            </p>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php include __DIR__ . '/../../includes/footer.php'; ?>

