<script src="<?= asset_url('assets/ext/jquery-ui/jquery-ui-timepicker-addon.js') ?>"></script>
<script src="<?= asset_url('assets/js/backend_cases_helper.js') ?>"></script>
<script src="<?= asset_url('assets/js/backend_cases.js') ?>"></script>
<script>
    var GlobalVariables = {
        csrfToken          : <?= json_encode($this->security->get_csrf_hash()) ?>,
        availableProviders : <?= json_encode($available_providers) ?>,
        availableServices  : <?= json_encode($available_services) ?>,
        secretaryProviders : <?= json_encode($secretary_providers) ?>,
        dateFormat         : <?= json_encode($date_format) ?>,
        timeFormat         : <?= json_encode($time_format) ?>,
        baseUrl            : <?= json_encode($base_url) ?>,
        casess          : <?= json_encode($casess) ?>,
        user               : {
            id         : <?= $user_id ?>,
            email      : <?= json_encode($user_email) ?>,
            role_slug  : <?= json_encode($role_slug) ?>,
            privileges : <?= json_encode($privileges) ?>
        }
    };

    $(document).ready(function() {
        BackendCases.initialize(true);
    });
</script>

<div id="casess-page" class="container-fluid backend-page">
    <div class="row">
        <div id="filter-casess" class="filter-records column col-xs-12 col-sm-5">
            <form>
                <div class="input-group">
                    <input type="text" class="key form-control">

                    <div class="input-group-addon">
                        <div>
                            <button class="filter btn btn-default" type="submit" title="<?= lang('filter') ?>">
                                <span class="glyphicon glyphicon-search"></span>
                            </button>
                            <button class="clear btn btn-default" type="button" title="<?= lang('clear') ?>">
                                <span class="glyphicon glyphicon-repeat"></span>
                            </button>
                        </div>
                    </div>
                </div>
            </form>

            <h3>Cases</h3>
            <div class="results"></div>
        </div>

        <div class="record-details col-xs-12 col-sm-7">
            <div class="btn-toolbar">
                <div id="add-edit-delete-group" class="btn-group">

                    <?php
                    $privileges[PRIV_CASES]['edit'] = FALSE;
                    $privileges[PRIV_CASES]['delete'] = FALSE;
                    $privileges[PRIV_CASES]['add'] = FALSE;
                    ?>

                    <?php if ($privileges[PRIV_CASES]['add'] === TRUE): ?>
                        <button id="add-cases" class="btn btn-primary">
                            <span class="glyphicon glyphicon-plus"></span>
                            <?= lang('add') ?>
                        </button>
                    <?php endif ?>

                    <?php if ($privileges[PRIV_CASES]['edit'] === TRUE): ?>
                        <button id="edit-cases" class="btn btn-default" disabled="disabled">
                            <span class="glyphicon glyphicon-pencil"></span>
                            <?= lang('edit') ?>
                        </button>
                    <?php endif ?>

                    <?php if ($privileges[PRIV_CASES]['delete'] === TRUE): ?>
                        <button id="delete-cases" class="btn btn-default" disabled="disabled">
                            <span class="glyphicon glyphicon-remove"></span>
                            <?= lang('delete') ?>
                        </button>
                    <?php endif ?>

                    <button id="gen-appt" class="btn btn-default" disabled="disabled">
                        <span class="glyphicon glyphicon-pencil"></span>
                        Generate Appointment Link
                    </button>
                </div>

                <div id="save-cancel-group" class="btn-group" style="display:none;">
                    <button id="save-cases" class="btn btn-primary">
                        <span class="glyphicon glyphicon-ok"></span>
                        <?= lang('save') ?>
                    </button>
                    <button id="cancel-cases" class="btn btn-default">
                        <i class="glyphicon glyphicon-ban-circle"></i>
                        <?= lang('cancel') ?>
                    </button>
                </div>
            </div>

            <input id="cases-id" type="hidden">

            <div class="row">
                <div class="col-xs-12 col-sm-6" style="margin-left: 0;">
                    <h3><?= lang('details') ?></h3>

                    <div id="form-message" class="alert" style="display:none;"></div>

                    <div class="form-group">
                        <label class="control-label" for="serviceforlink">Service for link:</label>
                    <select id="serviceforlink" class="form-control required">


                        <?php
                        // Group services by category, only if there is at least one service with a parent category.
                        $has_category = FALSE;
                        foreach($available_services as $service) {
                            if ($service['category_id'] != NULL) {
                                $has_category = TRUE;
                                break;
                            }
                        }

                        if ($has_category) {
                            $grouped_services = array();

                            foreach($available_services as $service) {
                                if ($service['category_id'] != NULL) {
                                    if (!isset($grouped_services[$service['category_name']])) {
                                        $grouped_services[$service['category_name']] = array();
                                    }

                                    $grouped_services[$service['category_name']][] = $service;
                                }
                            }

                            // We need the uncategorized services at the end of the list so
                            // we will use another iteration only for the uncategorized services.
                            $grouped_services['uncategorized'] = array();
                            foreach($available_services as $service) {
                                if ($service['category_id'] == NULL) {
                                    $grouped_services['uncategorized'][] = $service;
                                }
                            }

                            foreach($grouped_services as $key => $group) {
                                $group_label = ($key != 'uncategorized')
                                    ? $group[0]['category_name'] : 'Uncategorized';

                                if (count($group) > 0) {
                                    echo '<optgroup label="' . $group_label . '">';
                                    foreach($group as $service) {
                                        echo '<option value="' . $service['id'] . '">'
                                            . $service['name'] . '</option>';
                                    }
                                    echo '</optgroup>';
                                }
                            }
                        }  else {
                            foreach($available_services as $service) {
                                echo '<option value="' . $service['id'] . '">' . $service['name'] . '</option>';
                            }
                        }
                        ?>
                    </select>
                </div>

                    <div class="form-group">
                        <label class="control-label" for="contact-name">Contact Name:</label>
                        <input id="contact-name" class="form-control required">
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="entity">Entity:</label>
                        <input id="entity" class="form-control required">
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="created-date">Created Date:</label>
                        <input id="created-date" class="form-control required">
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="created-by">Created By:</label>
                        <input id="created-by" class="form-control required">
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="closed-date">Closed Date: </label>
                        <input id="closed-date" class="form-control">
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="subject">Subject: </label>
                        <input id="subject" class="form-control">
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="description">Description</label>
                        <textarea id="description" rows="4" class="form-control"></textarea>
                    </div>

                    <p class="text-center">
                        <em id="form-message" class="text-danger"><?= lang('fields_are_required') ?></em>
                    </p>
                </div>
                <div class="col-xs-12 col-sm-6">
                    <h3><?= lang('appointments') ?></h3>
                    <div id="cases-appointments" class="well"></div>
                    <div id="appointment-details" class="well hidden"></div>

                    <h3>Unbooked Links</h3>
                    <div id="cases-unbookedlinks" class="well"></div>
                </div>
            </div>
        </div>
    </div>
</div>
