<?php

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

enforceUserPermission('module_client', 2);

$vendor_id = intval($_GET['id']);

$sql = mysqli_query($mysqli, "SELECT vendor_account_number, vendor_client_id, vendor_code, vendor_contact_name,
    vendor_description, vendor_email, vendor_extension, vendor_hours, vendor_name,
    vendor_notes, vendor_phone, vendor_phone_country_code, vendor_sla, vendor_template_id,
    vendor_website FROM vendors WHERE vendor_id = $vendor_id LIMIT 1");

$row = mysqli_fetch_assoc($sql);
$vendor_name = escapeHtml($row['vendor_name']);
$vendor_description = escapeHtml($row['vendor_description']);
$vendor_account_number = escapeHtml($row['vendor_account_number']);
$vendor_contact_name = escapeHtml($row['vendor_contact_name']);
$vendor_phone_country_code = escapeHtml($row['vendor_phone_country_code']);
$vendor_phone = escapeHtml(formatPhoneNumber($row['vendor_phone'], $vendor_phone_country_code));
$vendor_extension = escapeHtml($row['vendor_extension']);
$vendor_email = escapeHtml($row['vendor_email']);
$vendor_website = escapeHtml($row['vendor_website']);
$vendor_hours = escapeHtml($row['vendor_hours']);
$vendor_sla = escapeHtml($row['vendor_sla']);
$vendor_code = escapeHtml($row['vendor_code']);
$vendor_notes = escapeHtml($row['vendor_notes']);
$vendor_template_id = intval($row['vendor_template_id']);
$client_id = intval($row['vendor_client_id']);

if ($client_id) {
    enforceClientAccess();
}

ob_start();

?>

<div class="modal-header bg-dark">
    <h5 class="modal-title"><i class="fas fa-fw fa-building me-2"></i>Editing vendor: <strong><?= $vendor_name ?></strong></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'] ?>">
    <input type="hidden" name="vendor_id" value="<?= $vendor_id ?>">
    <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<?= $vendor_id ?>">Details</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-support<?= $vendor_id ?>">Support</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-notes<?= $vendor_id ?>">Notes</a>
            </li>
        </ul>

        <hr>

        <div class="tab-content">

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

                <div class="mb-3">
                    <label>Vendor Name <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-building"></i></span>
                        <input type="text" class="form-control" name="name" placeholder="Vendor Name" maxlength="200" value="<?= "$vendor_name" ?>" required>
                    </div>
                </div>

                <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="Description" maxlength="200" value="<?= $vendor_description ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Account Number</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-fingerprint"></i></span>
                        <input type="text" class="form-control" name="account_number" placeholder="Account number" maxlength="200" value="<?= $vendor_account_number ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Account Manager</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-user"></i></span>
                        <input type="text" class="form-control" name="contact_name" maxlength="200" value="<?= $vendor_contact_name ?>" placeholder="Vendor contact name">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Template Base</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-puzzle-piece"></i></span>
                        <select class="form-select select2" name="vendor_template_id">
                            <option value="0">- None -</option>
                            <?php

                            $sql_vendor_templates = mysqli_query($mysqli, "SELECT vendor_template_id, vendor_template_name FROM vendor_templates WHERE vendor_template_archived_at IS NULL ORDER BY vendor_template_name ASC");
                            while ($row = mysqli_fetch_assoc($sql_vendor_templates)) {
                                $vendor_template_id_select = $row['vendor_template_id'];
                                $vendor_template_name_select = escapeHtml($row['vendor_template_name']); ?>
                                <option <?php if ($vendor_template_id == $vendor_template_id_select) { echo "selected"; } ?> value="<?= $vendor_template_id_select ?>"><?= $vendor_template_name_select ?></option>

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

            </div>

            <div class="tab-pane fade" id="pills-support<?= $vendor_id ?>">

                <label>Support Phone / <span class="text-secondary">Extension</span></label>
                <div class="row g-2">
                    <div class="col-9">
                        <div class="mb-3">
                            <div class="input-group">
                                    <span class="input-group-text"><i class="fa fa-fw fa-phone"></i></span>
                                <input type="tel" class="form-control col-2" name="phone_country_code" value="<?= $vendor_phone_country_code ?>" placeholder="+" maxlength="4">
                                <input type="tel" class="form-control" name="phone" value="<?= $vendor_phone ?>" placeholder="Phone Number" maxlength="200">
                            </div>
                        </div>
                    </div>
                    <div class="col-3">
                        <div class="mb-3">
                            <input type="text" class="form-control" name="extension" value="<?= $vendor_extension ?>" placeholder="ext." maxlength="200">
                        </div>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Support Hours</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
                        <input type="text" class="form-control" name="hours" placeholder="Support Hours" maxlength="200" value="<?= $vendor_hours ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Support Email</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-envelope"></i></span>
                        <input type="email" class="form-control" name="email" placeholder="Support Email" maxlength="200" value="<?= $vendor_email ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Support Website URL</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-globe"></i></span>
                        <input type="text" class="form-control" name="website" placeholder="Do not include http(s)://" maxlength="200" value="<?= $vendor_website ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>SLA</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-handshake"></i></span>
                        <input type="text" class="form-control" name="sla" placeholder="SLA Response Time" maxlength="200" value="<?= $vendor_sla ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Pin/Code</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="code" placeholder="Access Code or Pin" maxlength="200" value="<?= $vendor_code ?>">
                    </div>
                </div>

            </div>

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

                <div class="mb-3">
                    <textarea class="form-control" rows="12" placeholder="Enter some notes" name="notes"><?= $vendor_notes ?></textarea>
                </div>

                <p class="text-muted text-end">Vendor ID: <?= $vendor_id ?></p>

            </div>

        </div>

    </div>
    <div class="modal-footer">
        <button type="submit" name="edit_vendor" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Save</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';
