<?php

require_once '../../../includes/modal_header.php';

$client_id = intval($_GET['client_id'] ?? 0);

$license_types_array = array (
    'Device',
    'User',
    'Site',
    'Concurrent',
    'Trial',
    'Perpetual',
    'Usage-based'
);

ob_start();

?>
<div class="modal-header bg-dark">
    <h5 class="modal-title"><i class="fa fa-fw fa-cube me-2"></i>New License</h5>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

    <div class="modal-body">

        <ul class="nav nav-pills nav-justified mb-3">
            <li class="nav-item">
                <a class="nav-link active" data-bs-toggle="pill" href="#pills-details">Details</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-licensing">Licensing</a>
            </li>
            <?php if ($client_id) { // Dont show these when in global mode ?>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-device-licenses">Devices</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-user-licenses">Users</a>
            </li>
            <?php } ?>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-notes">Notes</a>
            </li>
        </ul>

        <hr>

        <div class="tab-content">

            <div class="tab-pane fade show active" id="pills-details">

                <?php if ($client_id) { ?>
                    <input type="hidden" name="client_id" value="<?= $client_id ?>">
                <?php } else { ?>

                    <div class="mb-3">
                        <label>Client <strong class="text-danger">*</strong></label>
                        <div class="input-group">
                                <span class="input-group-text"><i class="fa fa-fw fa-user"></i></span>
                            <select class="form-select select2" name="client_id" required>
                                <option value="">- Select Client -</option>
                                <?php

                                $sql = mysqli_query($mysqli, "SELECT client_id, client_name FROM clients WHERE client_archived_at IS NULL " . clientScopeSql('clients.client_id') . " ORDER BY client_name ASC");
                                while ($row = mysqli_fetch_assoc($sql)) {
                                    $client_id_select = intval($row['client_id']);
                                    $client_name = escapeHtml($row['client_name']); ?>
                                    <option <?php if ($client_id == $client_id_select) { echo "selected"; } ?> value="<?= $client_id_select ?>"><?= $client_name ?></option>

                                <?php } ?>
                            </select>
                        </div>
                    </div>

                <?php } ?>

                <div class="mb-3">
                    <label>Type <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-tag"></i></span>
                        <select class="form-select select2" name="type" required>
                            <option value="">- Select Type -</option>
                            <<?php
                            $sql_software_types_select = mysqli_query($mysqli, "
                                SELECT category_name FROM categories
                                WHERE category_type = 'software_type'
                                AND category_archived_at IS NULL
                                ORDER BY category_order ASC, category_name ASC
                            ");
                            while ($row = mysqli_fetch_assoc($sql_software_types_select)) {
                                $software_type_select = escapeHtml($row['category_name']);
                                ?>
                                <option><?= $software_type_select ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Software Name <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-cube"></i></span>
                        <input type="text" class="form-control" name="name" placeholder="Software name" maxlength="200" required>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Version</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-cube"></i></span>
                        <input type="text" class="form-control" name="version" placeholder="Software version" maxlength="200">
                    </div>
                </div>

                <?php if ($client_id) { ?>
                <div class="mb-3">
                    <label>Vendor</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-building"></i></span>
                        <select class="form-select select2" name="vendor">
                            <option value="">- Select Vendor -</option>
                            <?php

                            $sql = mysqli_query($mysqli, "SELECT vendor_name, vendor_id FROM vendors WHERE vendor_archived_at IS NULL AND vendor_client_id = $client_id ORDER BY vendor_name ASC");
                            while ($row = mysqli_fetch_assoc($sql)) {
                                $vendor_id = intval($row['vendor_id']);
                                $vendor_name = escapeHtml($row['vendor_name']);
                                ?>
                                <option value="<?= $vendor_id ?>"><?= $vendor_name ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <?php } ?>

                <div class="mb-3">
                    <label>Description</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-align-left"></i></span>
                        <input type="text" class="form-control" name="description" placeholder="Short description">
                    </div>
                </div>

            </div>

            <div class="tab-pane fade" id="pills-licensing">

                <div class="mb-3">
                    <label>License Type</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-cube"></i></span>
                        <select class="form-select select2" name="license_type">
                            <option value="">- Select a License Type -</option>
                            <?php foreach ($license_types_array as $license_type) { ?>
                                <option><?= $license_type ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Seats</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-chair"></i></span>
                        <input type="text" inputmode="numeric" pattern="[0-9]*" class="form-control" name="seats" placeholder="Number of seats">
                    </div>
                </div>

                <div class="mb-3">
                    <label>License Key</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-key"></i></span>
                        <input type="text" class="form-control" name="key" placeholder="License key" maxlength="200">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Purchase Reference</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-shopping-cart"></i></span>
                        <input type="text" class="form-control" name="purchase_reference" placeholder="eg. Invoice, PO Number" maxlength="200">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Purchase Date</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-calendar-check"></i></span>
                        <input type="date" class="form-control" name="purchase" max="2999-12-31">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Expire</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-calendar-times"></i></span>
                        <input type="date" class="form-control" name="expire" max="2999-12-31">
                    </div>
                </div>

            </div>

            <?php if ($client_id) { // Dont show these when in global mode ?>

            <div class="tab-pane fade" id="pills-device-licenses">

                <ul class="list-group">

                    <li class="list-group-item bg-dark">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" onclick="this.closest('.tab-pane').querySelectorAll('.asset-checkbox').forEach(checkbox => checkbox.checked = this.checked);">
                            <label class="form-check-label ms-3"><strong>Licensed Devices</strong></label>
                        </div>
                    </li>


                    <?php
                    $sql_assets_select = mysqli_query($mysqli, "SELECT asset_archived_at, asset_id, asset_name, asset_type, contact_name FROM assets LEFT JOIN contacts ON asset_contact_id = contact_id WHERE asset_archived_at IS NULL AND asset_client_id = $client_id ORDER BY asset_archived_at ASC, asset_name ASC");

                    while ($row = mysqli_fetch_assoc($sql_assets_select)) {
                        $asset_id_select = intval($row['asset_id']);
                        $asset_name_select = escapeHtml($row['asset_name']);
                        $asset_type_select = escapeHtml($row['asset_type']);
                        $asset_archived_at = escapeHtml($row['asset_archived_at']);
                        if (empty($asset_archived_at)) {
                            $asset_archived_display = "";
                        } else {
                            $asset_archived_display = "Archived - ";
                        }
                        $contact_name_select = escapeHtml($row['contact_name']);

                        ?>
                        <li class="list-group-item">
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input asset-checkbox" name="assets[]" value="<?= $asset_id_select ?>">
                                <label class="form-check-label ms-2"><?= "$asset_archived_display$asset_name_select - $contact_name_select" ?></label>
                            </div>
                        </li>

                    <?php } ?>

                </ul>

            </div>

            <div class="tab-pane fade" id="pills-user-licenses">

                <ul class="list-group">

                    <li class="list-group-item bg-dark">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" onclick="this.closest('.tab-pane').querySelectorAll('.user-checkbox').forEach(checkbox => checkbox.checked = this.checked);">
                            <label class="form-check-label ms-3"><strong>Licensed Users</strong></label>
                        </div>
                    </li>

                    <?php
                    $sql_contacts_select = mysqli_query($mysqli, "SELECT contact_email, contact_id, contact_name FROM contacts WHERE contact_archived_at IS NULL AND contact_client_id = $client_id ORDER BY contact_name ASC");

                    while ($row = mysqli_fetch_assoc($sql_contacts_select)) {
                        $contact_id_select = intval($row['contact_id']);
                        $contact_name_select = escapeHtml($row['contact_name']);
                        $contact_email_select = escapeHtml($row['contact_email']);

                        ?>
                        <li class="list-group-item">
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input user-checkbox" name="contacts[]" value="<?= $contact_id_select ?>">
                                <label class="form-check-label ms-2"><?= "$contact_name_select - $contact_email_select" ?></label>
                            </div>
                        </li>

                    <?php } ?>

                </ul>

            </div>

            <?php } ?>

            <div class="tab-pane fade" id="pills-notes">

                <textarea class="form-control" rows="12" placeholder="Enter some notes" name="notes"></textarea>

            </div>

        </div>

    </div>
    <div class="modal-footer">
        <button type="submit" name="add_software" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Create</button>
        <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
    </div>
</form>

<?php

require_once '../../../includes/modal_footer.php';
