<?php
/**
 * modules/contractors/view_contractors.php
 */

$p = $GLOBALS['my_privs'] ?? ['can_view' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) { $p['can_view'] = 1; }

if ($p['can_view'] == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=contractors&page=list_contractors') . "&status=access_denied");
    exit;
}

$cid = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($cid == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=contractors&page=list_contractors'));
    exit;
}

$sql = "SELECT * FROM TBL_CONTRACTORS WHERE CONTRACTOR_ID = $cid";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=contractors&page=list_contractors'));
    exit;
}
?>

<div class="p-3">
    <section class="content-header px-0">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="font-weight-bold text-primary"><i class="fas fa-id-badge mr-2"></i>Contractor Profile</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=contractors&page=list_contractors'); ?>" class="btn btn-default shadow-sm border">
                        <i class="fas fa-arrow-left mr-1"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </section>

    <section class="content px-0">
        <div class="container-fluid">
            <div class="card card-outline card-primary shadow-sm" style="border-top: 4px solid #007bff;">
                <div class="card-header bg-white">
                    <h3 class="card-title font-weight-bold text-dark">Legal & Contact Information</h3>
                    <div class="card-tools">
                        <span class="badge <?php echo ($data['STATUS'] == 'ACTIVE') ? 'badge-success' : 'badge-danger'; ?> px-3 py-1" style="font-size: 14px;">
                            <?php echo $data['STATUS']; ?>
                        </span>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">System Code</label>
                            <h5 class="text-primary font-weight-bold"><?php echo htmlspecialchars($data['CONTRACTOR_CODE']); ?></h5>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Official Name</label>
                            <h5 class="text-dark font-weight-bold"><?php echo htmlspecialchars($data['CONTRACTOR_NAME']); ?></h5>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Contact Person</label>
                            <h5 class="text-dark"><i class="fas fa-user-tie mr-2 text-secondary"></i><?php echo htmlspecialchars($data['CONTACT_PERSON']); ?></h5>
                        </div>
                        
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Mobile Number</label>
                            <h5 class="text-dark"><i class="fas fa-phone mr-2 text-success"></i><?php echo htmlspecialchars($data['MOBILE_NUMBER']); ?></h5>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">CNIC Number</label>
                            <h5 class="text-dark"><?php echo $data['CNIC_NUMBER'] ? htmlspecialchars($data['CNIC_NUMBER']) : '<span class="text-muted italic">Not Provided</span>'; ?></h5>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">NTN (Tax Status)</label>
                            <h5 class="text-dark"><?php echo $data['NTN_NUMBER'] ? htmlspecialchars($data['NTN_NUMBER']) : '<span class="text-muted italic">Non-Filer / Not Provided</span>'; ?></h5>
                        </div>

                        <div class="col-md-12 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Registered Address</label>
                            <div class="p-3 bg-light border rounded">
                                <?php echo $data['ADDRESS'] ? nl2br(htmlspecialchars($data['ADDRESS'])) : '<span class="text-muted italic">No address recorded.</span>'; ?>
                            </div>
                        </div>

                        <div class="col-md-6 mt-2 border-top pt-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Record Created</label>
                            <p class="font-weight-bold mb-0"><?php echo date('d-M-Y h:i A', strtotime($data['CREATED_DATE'])); ?></p>
                        </div>
                        <div class="col-md-6 mt-2 border-top pt-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Last Updated</label>
                            <p class="font-weight-bold mb-0"><?php echo $data['UPDATED_DATE'] ? date('d-M-Y h:i A', strtotime($data['UPDATED_DATE'])) : 'Never Updated'; ?></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>