{{!
  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.

  WSO2 Inc. licenses this file to you under the Apache License,
  Version 2.0 (the "License"); you may not use this file except
  in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
}}
{{unit "cdmf.unit.ui.title" pageTitle="Device Management"}}

{{unit "cdmf.unit.data-tables-extended"}}
{{unit "cdmf.unit.lib.ui-permissions-utility"}}

{{#zone "breadcrumbs"}}
    <li>
        <a href="{{@app.context}}/">
            <i class="icon fw fw-home"></i>
        </a>
    </li>
    {{#if group}}
        <li>
            <a href="{{@app.context}}/groups">
                Groups
            </a>
        </li>
        <li>
            <a href="#">
                {{group.name}}
            </a>
        </li>
    {{else}}
        <li>
            <a href="{{@app.context}}/devices">
                Devices
            </a>
        </li>
    {{/if}}
{{/zone}}

{{#zone "navbarActions"}}
    {{#if group}}
        <li>
            <a href="{{@app.context}}/devices" class="cu-btn">
            <span class="icon fw-stack">
                <i class="fw fw-add fw-stack-1x"></i>
                <i class="fw fw-circle-outline fw-stack-2x"></i>
            </span>
                Assign from My Devices
            </a>
        </li>
        <!--<li>
            <a href="{{@app.context}}/group/{{group.id}}/analytics" class="cu-btn">
            <span class="icon fw-stack">
                <i class="fw fw-statistics fw-stack-1x"></i>
                <i class="fw fw-circle-outline fw-stack-2x"></i>
            </span>
                View Analytics
            </a>
        </li>-->
    {{else}}
        {{#if permissions.enroll}}
            <li>
                <a href="{{@app.context}}/device/enroll" class="cu-btn">
            <span class="icon fw-stack">
                <i class="fw fw-add fw-stack-1x"></i>
                <i class="fw fw-circle-outline fw-stack-2x"></i>
            </span>
                    Enroll Device
                </a>
            </li>
        {{/if}}
    {{/if}}
{{/zone}}

{{#zone "content"}}
    {{#if group}}
        <h1 class="page-sub-title">
            {{group.name}} group
        </h1>
        <div class="row no-gutter add-padding-5x add-margin-top-5x" style="border: 1px solid #e4e4e4;">
            <div class="media">
                <div id="device_overview">
                    <div class="media-left col-lg-2">
                        <div class="icon">
                            <img src="/devicemgt/public/cdmf.page.groups/images/group-icon.png"
                                 style="background-color: #11375b; height: 152px;">
                        </div>
                    </div>
                    <div class="media-body asset-desc add-padding-left-5x">
                        <div style="background: #11375B; color: #fff; padding: 10px; margin-bottom: 5px">
                            Overview
                        </div>
                        <table class="table table-responsive table-striped" id="members" style="margin-bottom: 0px;">
                            <tbody>
                            <tr role="row" class="even">
                                <td class="sorting_1" style="padding:10px 15px; width: 15%;">Owner</td>
                                <td id="group_owner" style="padding:10px 15px;">{{group.owner}}</td>
                            </tr>
                            <tr role="row" class="odd">
                                <td class="sorting_1" style="padding:10px 15px; width: 15%;">Shared with roles</td>
                                <td style="padding:10px 15px;">
                                    {{#each roles}}
                                        {{this}}<br/>
                                    {{/each}}
                                </td>
                            </tr>
                            <tr role="row" class="even">
                                <td class="sorting_1" style="padding:10px 15px;width: 15%;">Device Count</td>
                                <td style="padding:10px 15px;">{{deviceCount}}</td>
                            </tr>
                            <tr role="row" class="odd">
                                <td class="sorting_1" style="padding:10px 15px;width: 15%;">Description</td>
                                <td style="padding:10px 15px;">{{group.description}}</td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <br/>
        <h1 class="page-sub-title add-margin-top-5x">
            Devices in {{group.name}} group
        </h1>
    {{/if}}
    <div class="wr-device-list row">
        <div class="wr-hidden-operations wr-advance-operations"></div>
        <div class="col-md-12 wr-page-content">
            <div>
                <span id="permission" data-permission="{{permissions.list}}"></span>
                {{unit "cdmf.unit.device.operation-mod"}}
                {{#if deviceCount}}
                    <div id="loading-content" class="col-centered">
                        <i class="fw fw-wso2-logo fw-pulse fw-2x"></i>
                        Loading devices . . .
                        <br>
                    </div>
                    <div id="device-listing-status" class="raw hidden">
                        <ul style="list-style-type: none;">
                            <li class="message message-info">
                                <h4>
                                    <i class="icon fw fw-info"></i>
                                    <a id="device-listing-status-msg"></a>
                                </h4>
                            </li>
                        </ul>
                    </div>
                    <div id="device-table">
                        <table class="table table-striped table-hover list-table display responsive nowrap data-table grid-view hidden"
                               id="device-grid">
                            <thead>
                            <tr class="sort-row">
                                <th class="no-sort"></th>
                                <th>By Device Name</th>
                                <th>By Owner</th>
                                <th>By Status</th>
                                <th>By Platform</th>
                                <th>By Ownership</th>
                                <th class="no-sort"></th>
                            </tr>
                            <tr class="filter-row filter-box">
                                <th class="no-sort"></th>
                                <th data-for="By Device name" class="text-filter"></th>
                                <th data-for="By Owner" class="text-filter"></th>
                                <th data-for="By Status" class="select-filter"></th>
                                <th data-for="By Platform" class="select-filter data-platform"></th>
                                <th data-for="By Ownership" class="select-filter"></th>
                                <th class="no-sort"></th>
                            </tr>
                            <tr class="bulk-action-row">
                                <th colspan="7">
                                    <ul class="tiles">
                                        {{#unless group}}
                                            <li>
                                                <a href="#" style="width: 100px;height: 80px;;"
                                                   data-click-event="remove-form"
                                                   class="btn square-element add-devices-to-group-link"
                                                   data-toggle="modal" data-target="#modalDemo">
                                                    <span class="icon fw-stack">
                                                        <i class="fw fw-circle-outline fw-stack-2x"></i>
                                                            <i class="fw fw-group fw-stack-1x"></i>
                                                    </span>
                                                    Add to Group
                                                </a>
                                            </li>
                                        {{/unless}}
                                        <li>
                                            <a href="#" style="width: 100px;height: 80px;"
                                               data-click-event="remove-form"
                                               class="btn square-element remove-device-link"
                                               data-toggle="modal" data-target="#modalDemo">
                                                <span class="icon fw-stack">
                                                    <i class="fw fw-circle-outline fw-stack-2x"></i>
                                                    <i class="fw fw-delete fw-stack-1x"></i>
                                                </span>
                                                {{#if group}}
                                                    Remove from group
                                                {{else}}
                                                    Remove Device
                                                {{/if}}
                                            </a>
                                        </li>
                                    </ul>
                                </th>
                            </tr>
                            </thead>
                            <tbody id="ast-container">

                            <br class="c-both"/>
                            </tbody>
                        </table>
                    </div>
                    <br class="c-both"/>
                    <div id="content-filter-types" style="display: none">
                        <div class="sort-title">Sort By</div>
                        <div class="sort-options">
                            <a href="#">By Device Name<span class="ico-sort-asc"></span></a>
                            <a href="#">By Owner</a>
                            <a href="#">By Status</a>
                            <a href="#">By Platform</a>
                            <a href="#">By Ownership</a>
                        </div>
                    </div>
                {{else}}
                    <div id="ast-container" class="ast-container list-view">
                        <div class="ctrl-info-panel col-centered text-center wr-login">
                            <h3 class="text-muted">
                                <i class="fw fw-devices fw-3x"></i>
                            </h3>
                            <h3 class="text-muted">
                                You don't have any
                                {{#if group}}
                                    devices assigned to this group. Let's add a device!
                                {{else}}
                                    enrolled devices. Let's enroll a device!
                                {{/if}}

                            </h3>
                            <h3>
                                {{#if group}}
                                    <!--suppress HtmlUnknownTarget -->
                                    <a href="{{@app.context}}/devices" class="btn-operations btn-default">
                                            <span class="fw-stack">
                                                <i class="fw fw-circle-outline fw-stack-2x"></i>
                                                <i class="fw fw-add fw-stack-1x"></i>
                                            </span>
                                        Assign from My Devices
                                    </a>
                                {{else}}
                                    {{#if permissions.enroll}}
                                        <!--suppress HtmlUnknownTarget -->
                                        <a href="{{@app.context}}/device/enroll" class="btn-operations btn-default">
                                            <span class="fw-stack">
                                                <i class="fw fw-circle-outline fw-stack-2x"></i>
                                                <i class="fw fw-add fw-stack-1x"></i>
                                            </span>
                                            Enroll New Device
                                        </a>
                                    {{/if}}
                                {{/if}}
                            </h3>
                        </div>
                    </div>
                {{/if}}

                <div id="group-device-modal-content" class="hide">
                    <div class="modal-header">
                        <h3 class="pull-left modal-title">
                            <span>
                                <span class="fw-stack">
                                    <i class="fw fw-circle-outline fw-stack-2x"></i>
                                    <i class="fw fw-group fw-stack-1x"></i>
                                </span> Device Grouping
                            </span>
                        </h3>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i
                                class="fw fw-cancel"></i></button>
                    </div>
                    <div class="modal-body add-margin-top-2x add-margin-bottom-2x">
                        <div id="notification-error-msg" class="alert alert-danger hidden" role="alert">
                            <i class="icon fw fw-error"></i><span></span>
                        </div>
                        <div id="user-groups">Loading...</div>
                    </div>
                    <div class="modal-footer">
                        <div class="buttons">
                            <a href="{{@app.context}}/group/add" id="group-add-link" class="btn-operations">
                                Add device group
                            </a>
                            <a href="#" id="group-device-add-link" class="btn-operations">
                                Add devices to group
                            </a>
                            <a href="#" id="group-device-update-link" class="btn-operations">
                                Update assignment
                            </a>
                            <a href="#" id="group-device-cancel-link" class="btn-operations btn-default">
                                Cancel
                            </a>
                        </div>
                    </div>
                </div>

                <div id="group-associate-device-200-content" class="hide">
                    <div class="modal-content">
                        <div class="row">
                            <div class="col-md-3 col-centered">
                                <h3>Device associations updated.</h3>
                            </div>
                        </div>
                    </div>
                </div>

                <div id="remove-device-modal-content" class="hide">
                    <div class="modal-header">
                        <h3 class="pull-left modal-title">
                            {{#if group}}
                                Do you really want to remove this device(s) from '{{group.name}}' group?
                            {{else}}
                                Do you really want to remove this device(s) from your Devices?
                            {{/if}}
                        </h3>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i
                                class="fw fw-cancel"></i>
                        </button>
                    </div>
                    <div class="modal-body add-margin-top-2x add-margin-bottom-2x">

                    </div>
                    <div class="modal-footer">
                        <div class="buttons">
                            <a href="#" id="remove-device-yes-link" class="btn-operations">
                                Yes
                            </a>

                            <a href="#" id="remove-device-cancel-link" class="btn-operations">
                                Cancel
                            </a>
                        </div>
                    </div>
                </div>

                <div id="remove-device-200-content" class="hide">
                    <div class="modal-header">
                        <h3 class="pull-left modal-title">
                            {{#if group}}
                                Successfully removed from '{{group.name}}' group.
                            {{else}}
                                Successfully removed.
                            {{/if}}
                        </h3>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i
                                class="fw fw-cancel"></i>
                        </button>
                    </div>
                    <div class="modal-body add-margin-top-2x add-margin-bottom-2x">

                    </div>
                    <div class="modal-footer">
                        <div class="buttons">
                            <a href="javascript:hidePopup()" class="btn-operations">Ok</a>
                        </div>
                    </div>
                </div>

                <div id="edit-device-modal-content" class="hide">
                    <div class="modal-header">
                        <h3 class="pull-left modal-title">
                            Please enter new name for the device?
                        </h3>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i
                                class="fw fw-cancel"></i>
                        </button>
                    </div>
                    <div class="modal-body add-margin-top-2x add-margin-bottom-2x">
                        <input id="edit-device-name" style="color:#3f3f3f;padding:5px" type="text"
                               value=""
                               placeholder="Type here" size="60">
                    </div>
                    <div class="modal-footer">
                        <div class="buttons">
                            <a href="#" id="edit-device-yes-link" class="btn-operations">
                                Rename
                            </a>

                            <a href="#" id="edit-device-cancel-link" class="btn-operations">
                                Cancel
                            </a>
                        </div>
                    </div>
                </div>

                <div id="edit-device-200-content" class="hide">
                    <div class="modal-header">
                        <h3 class="pull-left modal-title">
                            Device was successfully updated.
                        </h3>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i
                                class="fw fw-cancel"></i>
                        </button>
                    </div>
                    <div class="modal-body add-margin-top-2x add-margin-bottom-2x">

                    </div>
                    <div class="modal-footer">
                        <div class="buttons">

                            <div id="edit-device-200-content" class="hide">
                                <div class="content">
                                    <div class="row">
                                        <div class="col-lg-5 col-md-6 col-centered">
                                            <h3>Device was successfully updated.</h3>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div id="device-400-content" class="hide">
                                <div class="modal-content">
                                    <div class="row">
                                        <div class="col-lg-5 col-md-6 col-centered">
                                            <h3>Exception at backend. Try Later.</h3>
                                            <br/>
                                            <div class="buttons">
                                                <a href="#" id="device-400-link" class="btn-operations">
                                                    Ok
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div id="device-403-content" class="hide">
                                <div class="modal-content">
                                    <div class="row">
                                        <div class="col-lg-5 col-md-6 col-centered">
                                            <h3>Operation not permitted.</h3>
                                            <br/>
                                            <div class="buttons">
                                                <a href="#" id="device-403-link" class="btn-operations">
                                                    Ok
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div id="group-404-content" class="hide">
                                <div class="modal-content">
                                    <div class="row">
                                        <div class="col-lg-5 col-md-6 col-centered">
                                            <h3>
                                                You don't have any device group to add this device. Let's add a new
                                                device group!</h3>
                                            <br/>
                                            <div class="buttons">
                                                <a href="{{@app.context}}/group/add" class="btn-operations">
                                                    Add New Group
                                                </a>

                                                <a href="#" id="cancel-link" class="btn-operations">
                                                    Cancel
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div id="device-409-content" class="hide">
                                <div class="modal-content">
                                    <div class="row">
                                        <div class="col-lg-5 col-md-6 col-centered">
                                            <h3>Device does not exist.</h3>
                                            <br/>
                                            <div class="buttons">
                                                <a href="#" id="remove-device-409-link" class="btn-operations">
                                                    Ok
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div id="no-device-selected" class="hide">
                                <div class="modal-content">
                                    <div class="row">
                                        <div class="col-lg-5 col-md-6 col-centered">
                                            <h3>Please select one ore more devices in order to perform this
                                                operation.</h3>
                                            <br/>
                                            <div class="buttons">
                                                <a href="#" id="no-device-selected-link" class="btn-operations">
                                                    Ok
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{{/zone}}

{{#zone "bottomJs"}}
    <script id="device-listing" data-analitics-url="{{analiticsURL}}" data-current-user="{{@user.username}}" data-device-types="{{deviceTypes}}"
            type="text/x-handlebars-template"></script>
    {{js "js/listing.js"}}
{{/zone}}

{{#zone "topCss"}}
    <style>
        .select2-dropdown {
            z-index: 999999999;
        }

        .wr-input-control {
            color: black;
        }

        .select2-selection__choice {
            font-size: medium;
        }
    </style>
{{/zone}}
