<div class="cover margin-bottom-large">
    <h1 class="zone xl margin-bottom-large">
        <a data-ls-attrs="href=/console/home?project={{router.params.project}}" class="back text-size-small link-return-animation--start"><i class="icon-left-open"></i> Home</a>
        <br />

        <span>Tasks</span>
    </h1>
</div>
<div class="zone xl"
    data-service="projects.listTasks"
    data-scope="console"
    data-event="load,projects.createTask,projects.updateTask,projects.deleteTask"
    data-name="console-tasks"
    data-param-project-id="{{router.params.project}}"
    data-success="trigger"
    data-success-param-trigger-events="projects.listTasks">

    <div data-ls-if="0 === {{console-tasks.sum}} || undefined === {{console-tasks.sum}}" class="box margin-top margin-bottom">
        <h3 class="margin-bottom-small text-bold">No Tasks Found</h3>

        <p class="margin-bottom-no">You haven't created any tasks for your project yet.</p>
    </div>

    <div class="box y-scroll margin-bottom" data-ls-if="0 != {{console-tasks.sum}}">
        <table class="full vertical">
            <thead>
                <tr>
                    <th>Name</th>
                    <th width="140">Status</th>
                    <th width="180">Schedule</th>
                    <th width="140"></th>
                </tr>
            </thead>
            <tbody data-ls-loop="console-tasks.tasks" data-ls-as="task" class="list">
                <tr>
                    <td>
                        <div class="margin-bottom-tiny text-one-liner">
                            <span data-ls-attrs="title={{task.name}} ({{task.failures}} errors)" data-ls-bind="{{task.name}}"></span>

                            <span data-ls-if="false === {{task.security}}">
                                &nbsp; <span class="text-danger">SSL/TLS Disabled</span>
                            </span>
                            <span data-ls-if="0 !== {{task.failures}}">
                                &nbsp; <span class="text-danger" data-ls-bind="({{task.failures}} errors)"></span>
                            </span>
                        </div>

                        <a data-ls-attrs="href={{task.httpUrl}}" data-ls-bind="{{task.httpUrl}}" target="_blank" class="text-one-liner text-size-small"></a>
                    </td>
                    <td data-title="" style="vertical-align: middle">
                        <span data-ls-if="'play' === {{task.status}}">
                            <span class="tag green"><i class="icon-play"></i> Running &nbsp;</span>
                        </span>
                        <span data-ls-if="'play' !== {{task.status}}">
                            <span class="tag red"><i class="icon-pause"></i> Paused &nbsp;</span>
                        </span>
                    </td>
                    <td data-title="" style="vertical-align: middle" class="text-size-small text-fade">
                        <span data-ls-bind="Next: {{task.next|dateTime}}"></span>

                        <br />
                    
                        <span data-ls-if="undefined !== {{task.previous}} && {{task.previous}}">
                            <span data-ls-bind="Prev: {{task.previous|dateTime}}"></span>

                            <!-- <span data-ls-if="undefined !== {{task.delay}} && 59 < {{task.delay}}" class="text-danger margin-top-tiny">
                                <small data-ls-bind="({{task.delay|ms2hum}} Delay)"></small>
                            </span> -->
                        </span>
                        <small data-ls-if="undefined === {{task.previous}}" class="text-size-small">
                            None.
                        </small>
                    </td>
                    <td data-title="">
                        <div data-ui-modal class="modal box sticky-footer close" data-button-alias="none" data-open-event="task-update-{{task.$id}}">
                            <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                            <h1>Update Task</h1>

                            <form
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update Project Task"
                                data-service="projects.updateTask"
                                data-scope="console"
                                data-event="submit"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Updated task successfully"
                                data-success-param-trigger-events="projects.updateTask"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to update task"
                                data-failure-param-alert-classname="error">

                                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
                                <input type="hidden" name="taskId" data-ls-bind="{{task.$id}}" />

                                <label data-ls-attrs="for=name-{{task.$id}}">Name</label>
                                <input type="text" class="full-width" data-ls-attrs="id=name-{{task.$id}}" name="name" required autocomplete="off" data-ls-bind="{{task.name}}" maxlength="128" />

                                <label data-ls-attrs="for=status-{{task.$id}}" class="margin-bottom">Status
                                    <div class="margin-top-small">
                                        <input name="status" type="radio" checked="checked" required data-ls-bind="{{task.status}}" value="play" /> &nbsp; <span>Play</span> &nbsp;
                                        <input name="status" type="radio" required data-ls-bind="{{task.status}}" value="pause" /> &nbsp; <span>Pause</span> &nbsp;
                                    </div>
                                </label>

                                <label data-ls-attrs="for=schedule-{{task.$id}}">Schedule (CRON Syntax)</label>
                                <input type="text" class="full-width" data-ls-attrs="id=schedule-{{task.$id}}" name="schedule" required autocomplete="off" data-ls-bind="{{task.schedule}}" placeholder="* * * * *" />

                                <div class="row responsive thin">
                                    <div class="col span-4">
                                        <label data-ls-attrs="for=httpMethod-{{task.$id}}">HTTP Method</label>
                                        <select data-ls-attrs="id=httpMethod-{{task.$id}}" name="httpMethod" required data-ls-bind="{{task.httpMethod}}">
                                            <option value="POST">POST</option>
                                            <option value="GET">GET</option>
                                            <option value="PUT">PUT</option>
                                            <option value="PATCH">PATCH</option>
                                            <option value="DELETE">DELETE</option>
                                            <option value="CONNECT">CONNECT</option>
                                            <option value="HEAD">HEAD</option>
                                            <option value="TRACE">TRACE</option>
                                            <option value="OPTIONS">OPTIONS</option>
                                        </select>
                                    </div>
                                    <div class="col span-8">
                                        <label data-ls-attrs="for=httpUrl-{{task.$id}}">HTTP URL</label>
                                        <input type="url" class="full-width" data-ls-attrs="id=httpUrl-{{task.$id}}" name="httpUrl" required autocomplete="off" placeholder="https://example.com/callback" data-ls-bind="{{task.httpUrl}}" />
                                    </div>
                                </div>

                                <div class="margin-bottom toggle" data-ls-ui-open data-button-aria="Advanced Options">
                                    <i class="icon-plus pull-end margin-top-tiny"></i>
                                    <i class="icon-minus pull-end margin-top-tiny"></i>

                                    <h2 class="margin-bottom">
                                        Advanced Options
                                        <small class="text-size-small">(optional)</small>
                                    </h2>

                                    <label for="httpHeaders" class="text-bold">HTTP Headers</label>

                                    <input type="hidden" id="httpHeadersInit" name="httpHeaders" data-cast-to="array-empty">
                                    
                                    <div class="margin-bottom-small">
                                        <div data-ls-loop="task.httpHeaders" data-ls-as="header" style="overflow: hidden">
                                            <div class="margin-bottom-small">
                                                <div data-forms-remove class="row thin">
                                                    <div class="col span-10">
                                                        <input type="hidden" name="httpHeaders" data-forms-headers data-ls-bind="{{header}}" data-cast-to="array" />
                                                    </div>
                                                    <div class="col span-2">
                                                        <button type="button" data-remove class="reverse danger round pull-end"><i class="icon-cancel"></i></button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div data-forms-clone="" data-label="Add Header" data-first="0">
                                            <div class="">
                                                <div data-forms-remove class="row thin margin-bottom-small">
                                                    <div class="col span-10">
                                                        <input type="hidden" name="httpHeaders" data-forms-headers data-cast-to="array" />
                                                    </div>
                                                    <div class="col span-2">
                                                        <button type="button" data-remove class="reverse danger round pull-end"><i class="icon-cancel"></i></button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <label data-ls-attrs="for=security-{{task.$id}}" class="margin-bottom">
                                        <div class="margin-bottom-small text-bold">SSL / TLS (Certificate verification)</div>
                                        
                                        <input name="security" type="radio" required data-ls-bind="{{task.security}}" value="true" data-cast-to="boolean" /> &nbsp; <span>Enabled</span>
                                        <input name="security" type="radio" required data-ls-bind="{{task.security}}" value="false" data-cast-to="boolean" /> &nbsp; <span>Disabled</span>
                                    </label>

                                    <p class="margin-bottom text-size-small text-fade"><span class="text-red">Warning</span>: Untrusted or self-signed certificates may not be secure.
                                        <a href="https://en.wikipedia.org/wiki/Self-signed_certificate" target="_blank" rel="noopener">Learn more<i class="icon-link-ext"></i></a>
                                    </p>

                                    <label class="text-bold">HTTP Authentication <span class="tooltip" data-tooltip="Use to secure your endpoint from untrusted sources"><i class="icon-question"></i></span> &nbsp;<small>(optional)</small></label>

                                    <div class="row responsive thin">
                                        <div class="col span-6 margin-bottom">
                                            <label data-ls-attrs="for=httpUser-{{task.$id}}">User</label>
                                            <input type="text" class="full-width margin-bottom-no" data-ls-attrs="id=httpUser-{{task.$id}}" name="httpUser" autocomplete="off" data-ls-bind="{{task.httpUser}}" />
                                        </div>
                                        <div class="col span-6 margin-bottom">
                                            <label data-ls-attrs="for=httpPass-{{task.$id}}">Password</label>
                                            <input data-forms-show-secret type="password" class="full-width margin-bottom-no" data-ls-attrs="id=httpPass-{{task.$id}}" name="httpPass" autocomplete="off" data-ls-bind="{{task.httpPass}}" />
                                        </div>
                                    </div>
                                </div>

                                <footer>
                                    <button type="submit">Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                                </footer>
                            </form>

                        </div>

                        <form
                            data-analytics
                            data-analytics-activity
                            data-analytics-event="submit"
                            data-analytics-category="console"
                            data-analytics-label="Delete Project Task"
                            data-service="projects.deleteTask"
                            data-scope="console"
                            data-event="task-delete-{{task.$id}}"
                            data-confirm="Are you sure you want to delete this task?"
                            data-success="alert,trigger"
                            data-success-param-alert-text="Deleted task successfully"
                            data-success-param-trigger-events="projects.deleteTask"
                            data-failure="alert"
                            data-failure-param-alert-text="Failed to delete task"
                            data-failure-param-alert-classname="error">

                            <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
                            <input type="hidden" name="taskId" data-ls-bind="{{task.$id}}" />
                        </form>

                        <button class="pull-end margin-start round desktops-only" data-ls-ui-trigger="task-update-{{task.$id}}"><i class="icon-cog"></i></button>
                        <button class="pull-end reverse danger round desktops-only" data-ls-ui-trigger="task-delete-{{task.$id}}"><i class="icon-trash"></i></button>

                        <button class="link pull-start margin-end-small tablets-only phones-only" data-ls-ui-trigger="task-update-{{task.$id}}">Update</button>
                        <button class="link pull-start margin-end text-danger tablets-only phones-only" data-ls-ui-trigger="task-delete-{{task.$id}}">Delete</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="clear">
        <div data-ui-modal class="modal box close sticky-footer" data-button-text="Add Task">
            <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

            <h1>Add Task</h1>

            <form
                data-analytics
                data-analytics-activity
                data-analytics-event="submit"
                data-analytics-category="console"
                data-analytics-label="Create Project Task"
                data-service="projects.createTask"
                data-scope="console"
                data-event="submit"
                data-success="alert,trigger,reset"
                data-success-param-alert-text="Created task successfully"
                data-success-param-trigger-events="projects.createTask"
                data-failure="alert"
                data-failure-param-alert-text="Failed to create task"
                data-failure-param-alert-classname="error">

                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />

                <label for="name">Name</label>
                <input type="text" class="full-width" id="name" name="name" required autocomplete="off" maxlength="128" />

                <label data-ls-attrs="for=status" class="margin-bottom">Status
                    <div class="margin-top-small">
                        <input name="status" type="radio" value="play" checked="checked" required /> &nbsp; <span>Play</span> &nbsp;
                        <input name="status" type="radio" value="pause" required /> &nbsp; <span>Pause</span> &nbsp;
                    </div>
                </label>

                <label for="schedule">Schedule (CRON Syntax)</label>
                <input type="text" class="full-width" id="schedule" name="schedule" required autocomplete="off" placeholder="* * * * *" />

                <div class="row responsive thin">
                    <div class="col span-4">
                        <label for="httpMethod">HTTP Method</label>
                        <select id="httpMethod" name="httpMethod" required>
                            <option value="POST">POST</option>
                            <option value="GET">GET</option>
                            <option value="PUT">PUT</option>
                            <option value="PATCH">PATCH</option>
                            <option value="DELETE">DELETE</option>
                            <option value="CONNECT">CONNECT</option>
                            <option value="HEAD">HEAD</option>
                            <option value="TRACE">TRACE</option>
                            <option value="OPTIONS">OPTIONS</option>
                        </select>
                    </div>
                    <div class="col span-8">
                        <label for="httpUrl">HTTP URL</label>
                        <input type="url" class="full-width" id="httpUrl" name="httpUrl" required autocomplete="off" placeholder="https://example.com/callback" />
                    </div>
                </div>
                
                <div class="margin-bottom toggle" data-ls-ui-open data-button-aria="Advanced Options">
                    <i class="icon-plus pull-end margin-top-tiny"></i>
                    <i class="icon-minus pull-end margin-top-tiny"></i>
                    
                    <h2 class="margin-bottom">
                        Advanced Options
                        <small class="text-size-small">(optional)</small>
                    </h2>

                    <label for="httpHeaders" class="text-bold">HTTP Headers</label>
                    
                    <input type="hidden" id="httpHeadersInitAdd" name="httpHeaders" data-cast-to="array-empty">

                    <div class="margin-bottom-small">
                        <div data-forms-clone="" data-label="Add Header" data-first="0">
                            <div class="">
                                <div data-forms-remove class="row thin margin-bottom-small">
                                    <div class="col span-10">
                                        <input type="hidden" name="httpHeaders" data-forms-headers data-cast-to="array" />
                                    </div>
                                    <div class="col span-2">
                                        <button type="button" data-remove class="reverse danger round pull-end"><i class="icon-cancel"></i></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <label for="secure" class="margin-bottom-small">
                        <div class="margin-bottom-small text-bold">SSL / TLS (Certificate verification)</div>
                        
                        <input name="security" data-ls-attrs="id=secure-yes" type="radio" value="1" checked="checked" required /> &nbsp; <span>Enabled</span>
                        <input name="security" data-ls-attrs="id=secure-no" type="radio" value="0" required /> &nbsp; <span>Disabled</span>
                    </label>

                    <p class="margin-bottom text-size-small text-fade"><span class="text-red">Warning</span>: Untrusted or self-signed certificates may not be secure.
                        <a href="https://en.wikipedia.org/wiki/Self-signed_certificate" target="_blank" rel="noopener">Learn more<i class="icon-link-ext"></i></a>
                    </p>

                    <label class="text-bold">HTTP Authentication <span class="tooltip" data-tooltip="Use to secure your endpoint from untrusted sources"><i class="icon-question"></i></span> &nbsp;<small>(optional)</small></label>

                    <div class="row responsive thin">
                        <div class="col span-6 margin-bottom">
                            <label for="httpUser">User</label>
                            <input type="text" class="full-width margin-bottom-no" id="httpUser" name="httpUser" autocomplete="off" />
                        </div>
                        <div class="col span-6 margin-bottom">
                            <label for="httpPass">Password</label>
                            <input type="password" data-forms-show-secret class="full-width margin-bottom-no" id="httpPass" name="httpPass" autocomplete="off" />
                        </div>
                    </div>
                </div>

                <footer>
                    <button type="submit">Create</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                </footer>
            </form>
        </div>
    </div>
</div>
