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

require('../../includes/master/form_functions.php');
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `itemdetails` WHERE `itemnumber`=?") or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i', $_REQUEST['itemnumber']);
$stmt->execute();
$rc = $stmt->get_result();
$stmt->close();
$cd = mysqli_fetch_array($rc);
$dateOfManufacture = ($cd['dateofmanf'] == "") ? "Not Known" : $cd['dateofmanf'];
?>

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

.form-details-edit-header {
    background: #f8fafc;
    border-bottom: 1px solid #e3e9ef;
    padding: 1rem 1.25rem;
}

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

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

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

.form-details-field {
    min-width: 0;
}

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

.form-details-field .form-control,
.form-details-field select {
    min-height: 38px;
    width: 100%;
}

.form-details-edit-actions {
    align-items: center;
    background: #f8fafc;
    border-top: 1px solid #e3e9ef;
    display: flex;
    gap: .75rem;
    justify-content: flex-end;
    padding: .9rem 1.25rem;
}

@media (max-width: 767.98px) {
    .form-details-edit-grid {
        grid-template-columns: 1fr;
        padding: .9rem;
    }

    .form-details-edit-header,
    .form-details-edit-actions {
        padding: .9rem;
    }
}

@media (max-width: 575.98px) {
    .form-details-edit-actions {
        align-items: stretch;
        flex-direction: column-reverse;
    }

    .form-details-edit-actions .btn {
        width: 100%;
    }
}
</style>

<form id="itemEditForm">
    <section class="form-details-edit-panel">
        <div class="form-details-edit-header">
            <span>Edit asset details</span>
            <strong><?= htmlspecialchars($cd['desc'] !== '' ? $cd['desc'] : 'Item details', ENT_QUOTES) ?></strong>
        </div>

        <div class="form-details-edit-grid">
            <div class="form-details-field">
                <label for="detail_desc">Description</label>
                <input type="text" class="form-control" id="detail_desc" name="desc" value="<?= htmlspecialchars($cd['desc'], ENT_QUOTES) ?>" readonly>
            </div>

            <div class="form-details-field">
                <label for="detail_dateofmanf">Date of Manufacture</label>
                <input type="text" class="form-control" id="detail_dateofmanf" name="dateofmanf" value="<?= htmlspecialchars($dateOfManufacture, ENT_QUOTES) ?>">
            </div>

            <div class="form-details-field">
                <label for="detail_plantnumber">Plant Number</label>
                <input type="text" class="form-control" id="detail_plantnumber" name="plantnumber" value="<?= htmlspecialchars($cd['plantnumber'], ENT_QUOTES) ?>">
            </div>

            <div class="form-details-field">
                <label for="detail_model">Model</label>
                <input type="text" class="form-control" id="detail_model" name="model" value="<?= htmlspecialchars($cd['model'], ENT_QUOTES) ?>">
            </div>

            <div class="form-details-field">
                <label for="detail_serialnumber">Serial</label>
                <input type="text" class="form-control" id="detail_serialnumber" name="serialnumber" value="<?= htmlspecialchars($cd['serialnumber'], ENT_QUOTES) ?>">
            </div>

            <div class="form-details-field">
                <label>Location</label>
                <?= getSites2($cd['clientnumber'], $cd['locnumber']) ?>
            </div>

            <div class="form-details-field">
                <label for="detail_manf">Manufacturer</label>
                <input type="text" class="form-control" id="detail_manf" name="manf" value="<?= htmlspecialchars($cd['manf'], ENT_QUOTES) ?>">
            </div>

            <div class="form-details-field">
                <label for="detail_subloc">Location on Site</label>
                <input type="text" class="form-control" id="detail_subloc" name="subloc" value="<?= htmlspecialchars($cd['subloc'], ENT_QUOTES) ?>">
            </div>

            <div class="form-details-field">
                <label for="detail_swl">SWL</label>
                <input type="text" class="form-control" id="detail_swl" name="swl" value="<?= htmlspecialchars($cd['swl'], ENT_QUOTES) ?>">
            </div>
        </div>

        <div class="form-details-edit-actions">
            <button type="button" onclick="cancelEdit('<?= htmlspecialchars($_REQUEST['itemnumber'], ENT_QUOTES) ?>')" class="btn btn-outline-danger btn-sm">
                <i class="fad fa-times"></i> Cancel
            </button>
            <button type="submit" class="btn btn-info btn-sm">
                <i class="fad fa-save"></i> Save changes
            </button>
        </div>
    </section>

    <input type="hidden" name="FUNCTION" id="FUNCTION" value="update_item_details" />
    <input type="hidden" name="itemnumber" id="itemnumber" value="<?= htmlspecialchars($_REQUEST['itemnumber'], ENT_QUOTES) ?>" />
    <input type="hidden" name="tenant" id="tenant" value="<?= TENANT_URL ?>" />
</form>
