<?php
if ($_REQUEST['dataLoad'] != "") {
    $d[2] = $_REQUEST['dataLoad'];
    $_REQUEST['itemnumber'] = $_REQUEST['dataLoad'];

    if (file_exists('../../..' . $_REQUEST['tenant'] . '/config.php')) {
        require_once('../../..' . $_REQUEST['tenant'] . '/config.php');
    }
    if (file_exists('/config.php')) {
        require_once('/config.php');
    }

    require('../../includes/master/form_functions.php');

    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `itemdetails` WHERE `itemnumber`=?");
    $stmt->bind_param('i', $_REQUEST['itemnumber']);
    $stmt->execute();
    $rc = $stmt->get_result();
    $stmt->close();
    $cd = mysqli_fetch_array($rc);
}

$plantFile = null;
$plantCode = $cd['code'];
$itemDescription = trim((string)$cd['desc']);
$siteLocation = getItemLocation($cd['itemnumber'], "name");
$siteDetail = getItemLocation($cd['itemnumber'], 'itemDetail');
$lastInspector = getItemInsp($cd['itemnumber'], 'name');
$dateOfManufacture = ($cd['dateofmanf'] == "" || $cd['dateofmanf'] == "Not Marked") ? "Not Known" : $cd['dateofmanf'];

if ($plantCode !== '') {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `doc_file`
        FROM `dd_plant`
        WHERE TRIM(`code`) = ?
          AND `doc_file` IS NOT NULL
          AND `doc_file` <> ''
        ORDER BY `id` DESC
        LIMIT 1
    ");
    $stmt->bind_param('s', $plantCode);
    $stmt->execute();
    $pr = $stmt->get_result();
    $stmt->close();

    if ($pr && ($row = $pr->fetch_assoc())) {
        $plantFile = (string)$row['doc_file'];
    }
}

$detailRows = [
    ['Description', $itemDescription],
    ['Plant Number', $cd['plantnumber']],
    ['Serial', $cd['serialnumber']],
    ['Manufacturer', $cd['manf']],
    ['SWL', $cd['swl']],
    ['Date of Manufacture', $dateOfManufacture],
    ['Model', $cd['model']],
    ['Location', $siteLocation],
    ['Location on Site', $cd['subloc']],
];
?>

<style>
.form-details-panel {
    background: #fff;
    border: 1px solid #dbe3ea;
    border-radius: 8px;
    box-shadow: 0 10px 26px rgba(31, 49, 68, .08);
    overflow: hidden;
}

.form-details-header {
    align-items: center;
    background: #f8fafc;
    border-bottom: 1px solid #e3e9ef;
    display: flex;
    gap: 1rem;
    justify-content: space-between;
    padding: 1rem 1.25rem;
}

.form-details-title {
    margin: 0;
    min-width: 0;
}

.form-details-title span {
    color: #5b6b7b;
    display: block;
    font-size: .75rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.form-details-title strong {
    color: #1f3448;
    display: block;
    font-size: 1.05rem;
    line-height: 1.25;
    overflow-wrap: anywhere;
}

.form-details-actions {
    display: flex;
    flex-wrap: wrap;
    gap: .5rem;
    justify-content: flex-end;
}

.form-details-grid {
    display: grid;
    gap: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.form-detail-item {
    border-bottom: 1px solid #edf1f5;
    display: grid;
    gap: .35rem;
    grid-template-columns: minmax(130px, .42fr) minmax(0, 1fr);
    padding: .85rem 1.25rem;
}

.form-detail-item:nth-child(odd) {
    border-right: 1px solid #edf1f5;
}

.form-detail-label {
    color: #536272;
    font-size: .78rem;
    font-weight: 700;
}

.form-detail-value {
    color: #1d2935;
    font-weight: 600;
    overflow-wrap: anywhere;
}

.form-detail-value.is-muted {
    color: #8a97a5;
    font-weight: 500;
}

.form-details-summary {
    align-items: stretch;
    background: #f8fafc;
    display: grid;
    gap: 1px;
    grid-template-columns: minmax(0, 1.2fr) minmax(220px, .8fr);
}

.form-details-summary > div {
    background: #f8fafc;
    padding: 1rem 1.25rem;
}

.form-details-summary-label {
    color: #536272;
    display: block;
    font-size: .78rem;
    font-weight: 700;
    margin-bottom: .25rem;
}

.form-details-summary-value {
    color: #1d2935;
    font-weight: 600;
    line-height: 1.35;
}

@media (max-width: 991.98px) {
    .form-details-grid,
    .form-details-summary {
        grid-template-columns: 1fr;
    }

    .form-detail-item:nth-child(odd) {
        border-right: 0;
    }
}

@media (max-width: 575.98px) {
    .form-details-header {
        align-items: stretch;
        flex-direction: column;
        padding: .9rem;
    }

    .form-details-actions {
        justify-content: stretch;
    }

    .form-details-actions .btn {
        flex: 1 1 auto;
    }

    .form-detail-item {
        grid-template-columns: 1fr;
        padding: .8rem .9rem;
    }

    .form-details-summary > div {
        padding: .85rem .9rem;
    }
}
</style>

<div class="form-details-shell" data-item-number="<?= htmlspecialchars($d[2], ENT_QUOTES) ?>">
    <section class="form-details-panel">
        <div class="form-details-header">
            <h3 class="form-details-title">
                <span>Asset details</span>
                <strong><?= htmlspecialchars($itemDescription !== '' ? $itemDescription : 'Item details', ENT_QUOTES) ?></strong>
            </h3>
            <div class="form-details-actions">
                <?php if ($plantFile): ?>
                <button type="button"
                        class="btn btn-outline-secondary btn-sm"
                        onclick="viewPlantResource('<?= htmlspecialchars($plantFile, ENT_QUOTES) ?>')"
                        data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover"
                        title="View plant resource">
                    <i class="fad fa-eye"></i> Resource
                </button>
                <?php endif; ?>
                <a id="editBtn" href="javascript:void(0)" class="btn btn-info btn-sm"
                   onclick="loadItemDetailsEdit('<?= htmlspecialchars($d[2], ENT_QUOTES) ?>')">
                    <i class="fad fa-pencil fa-sm text-light"></i> Edit details
                </a>
            </div>
        </div>

        <div class="form-details-grid">
            <?php foreach ($detailRows as $row):
                $value = trim((string)$row[1]);
                $displayValue = $value !== '' ? $value : 'Not Known';
            ?>
            <div class="form-detail-item">
                <div class="form-detail-label"><?= htmlspecialchars($row[0], ENT_QUOTES) ?></div>
                <div class="form-detail-value<?= $value === '' ? ' is-muted' : '' ?>">
                    <?= htmlspecialchars($displayValue, ENT_QUOTES) ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>

        <div class="form-details-summary">
            <div>
                <span class="form-details-summary-label">Item located at</span>
                <div class="form-details-summary-value">
                    <?= $siteDetail ?>
                    <?php if ($cd['subloc'] != "") { ?><br>Sublocation: <?= htmlspecialchars($cd['subloc'], ENT_QUOTES); } ?>
                </div>
            </div>
            <div>
                <span class="form-details-summary-label">Inspection history</span>
                <div class="form-details-summary-value">
                    <?php if ($lastInspector == "0") { ?>
                    <span class="text-danger">No inspection stored for this asset and report type</span>
                    <?php } else { ?>
                    Last inspected by <strong><?= htmlspecialchars($lastInspector, ENT_QUOTES) ?></strong>
                    <?php } ?>
                </div>
            </div>
        </div>
    </section>
</div>
