<?php if(!defined('ACCESS_CONTROL')) {header($_SERVER['SERVER_PROTOCOL']. ' 404'); exit('404 not found');}?>
<?php include VIEWS_PATH.'/layouts/errors.html.php'?>

<div class="tabpanel">
    <!-- tabs -->
    <ul class="nav nav-tabs" id="tabs-newtask">
        <li class="<?php echo(!isset($task['type']) || $task['type'] == 'local' ? 'active' : '')?>"><a href="#newtask-local" aria-controls="newtask-local" role="tab">From local file</a></li>
        <li class="<?php echo(isset($task['type']) && $task['type'] == 'remote' ? 'active' : '')?>"><a href="#newtask-remote" aria-controls="newtask-remote" role="tab">From remote file</a></li>
    </ul>
    <div class="tab-content" style="background-color: #fff; padding: 15px;">
        <!-- forms -->
        <!-- form local  -->
        <div role="tabpanel" class="tab-pane fade in <?php echo(!isset($task['type']) || $task['type'] == 'local') ? 'active' : ''?>" id="newtask-local">
            <form class="form-group-sm form-horizontal" action="<?php echo($form_action)?>" method="post" enctype="multipart/form-data">
                <input type="hidden" name="task[type]" value="local">
                <div class="form-group">
                    <label for="task_name" class="control-label col-sm-2">Name</label>
                    <div class="col-sm-10">
                        <input type="text" name="task[name]" id="task_name" class="form-control" value="<?php echo isset($task['type']) && $task['type']=='local' ? htmlentities($task['name']) : ''?>">
                    </div>
                </div>
                <div class="form-group">
                    <label for="task_rights" class="control-label col-sm-2">Level privileges</label>
                    <div class="col-sm-2">
                        <select id="task_rights" name="task[pl]" class="form-control">
                            <option value="all">All</option>
                            <option value="user" <?php echo isset($task['type']) && $task['type']=='local' && ($task['pl'] == 'user' || $task['pl'] == '0') ? 'selected' : ''?> >User</option>
                            <option value="admin" <?php echo isset($task['type']) && $task['type']=='local' && ($task['pl'] == 'admin' || $task['pl'] == '1') ? 'selected' : ''?> >Admin</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="upload_file" class="control-label col-sm-2">Local file</label>
                    <div class="col-sm-10">
                        <input type="file" name="upload[file]" id="upload_file" class="form-control">
                    </div>
                </div>

                <div class="form-group">
                    <label for="task_countries" class="control-label col-sm-2">Countries</label>
                    <div class="col-sm-10">
                        <p class="help-block small">Alpha2 country codes separated by spaces, like <strong>US CA AU</strong>. Leave blank to select all countries</p>
                        <input type="text" class="form-control" name="task[countries]" id="task_countries" value="<?php echo isset($task['type']) && $task['type']=='local' ? htmlentities($task['countries']) : ''?>">
                        <div class="checkbox">
                            <label><input type="checkbox" name="task[countries_inverse]" <?php echo isset($task['type']) && $task['type']=='local' && $task['countries_inverse'] ? 'checked' : ''?> > All except selected countries </label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="task_limit" class="control-label col-sm-2">Limit</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="task[limit]" id="task_limit" value="<?php echo isset($task['type']) && $task['type']=='local' ? (int)trim($task['limit']) : '0'?>">
                    </div>
                    <div class="col-sm-8">
                        <p class="form-control-static">0 - without limit</p>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> Save task</button>
                    </div>
                </div>

            </form>
        </div>


        <!-- form remote  -->
        <div role="tabpanel" class="tab-pane fade in <?php echo(isset($task['type']) && $task['type'] == 'remote') ? 'active' : ''?>" id="newtask-remote">
            <form class="form-group-sm form-horizontal" action="<?php echo($form_action)?>" method="post">
                <input type="hidden" name="task[type]" value="remote">

                <div class="form-group">
                    <label for="task_name_remote" class="control-label col-sm-2">Name</label>
                    <div class="col-sm-10">
                        <input type="text" name="task[name]" id="task_name_remote" class="form-control" value="<?php echo isset($task['type']) && $task['type']=='remote' ? htmlentities($task['name']) : ''?>">
                    </div>
                </div>
                <div class="form-group">
                    <label for="task_rights_remote" class="control-label col-sm-2">Level privileges</label>
                    <div class="col-sm-2">
                        <select id="task_rights_remote" name="task[pl]" class="form-control">
                            <option value="all">All</option>
                            <option value="user" <?php echo isset($task['type']) && $task['type']=='remote' && ($task['pl'] == 'user' || $task['pl'] == '0') ? 'selected' : ''?> >User</option>
                            <option value="admin" <?php echo isset($task['type']) && $task['type']=='remote' && ($task['pl'] == 'admin' || $task['pl'] == '1') ? 'selected' : ''?> >Admin</option>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label for="task_file_remote" class="control-label col-sm-2">Remote file</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="task[remote_file]" id="task_file_remote" placeholder="http://" value="<?php echo isset($task['type']) && $task['type']=='remote' ? htmlentities($task['remote_file']) : ''?>">
                    </div>
                </div>
                <div class="form-group">
                    <label for="task_file_update_remote_fraq" class="control-label col-sm-2">Update</label>
                    <div class="col-sm-10">
                        <select id="task_file_update_remote_fraq" disabled class="form-control" name="task[update_remote_fraq]" >
                            <option value="0">Never</option>
                            <option value="60" <?php echo isset($task['type']) && $task['type']=='remote'  && $task['update_remote_fraq'] == 60 ? 'selected' : '' ?>>Every 1 minute</option>
                            <option value="300" <?php echo isset($task['type']) && $task['type']=='remote'  && $task['update_remote_fraq'] == 300 ? 'selected' : '' ?>>Every 5 minutes</option>
                            <option value="600" <?php echo isset($task['type']) && $task['type']=='remote'  && $task['update_remote_fraq'] == 600 ? 'selected' : '' ?>>Every 10 minutes</option>
                            <option value="1800" <?php echo isset($task['type']) && $task['type']=='remote'  && $task['update_remote_fraq'] == 1800 ? 'selected' : '' ?>>Every 30 minutes</option>
                            <option value="3600" <?php echo isset($task['type']) && $task['type']=='remote'  && $task['update_remote_fraq'] == 3600 ? 'selected' : '' ?>>Every 1 hour</option>
                        </select>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="download-remote-file" <?php echo isset($task['type']) && $task['type']=='remote' && isset($task['update_remote_fraq']) ? 'checked' : ''?>>  Download and distribute file with the current host
                            </label>
                        </div>
                    </div>
                </div>

                <div class="form-group" >
                    <label for="task_countries_remote" class="control-label col-sm-2">Countries</label>
                    <div class="col-sm-10" >
                        <p class="help-block small">Alpha2 country codes separated by spaces, like <strong>US CA AU</strong>. Leave blank to select all countries</p>
                        <input type="text" class="form-control" name="task[countries]" id="task_countries_remote" value="<?php echo isset($task['type']) && $task['type']=='remote' ? htmlentities($task['countries']) : ''?>">
                        <div class="checkbox">
                            <label><input type="checkbox" name="task[countries_inverse]" <?php echo isset($task['type']) && $task['type']=='remote' && $task['countries_inverse'] ? 'checked' : ''?>> All except selected countries </label>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="task_limit_remote" class="control-label col-sm-2">Limit</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="task[limit]" id="task_limit_remote" value="<?php echo isset($task['type']) && $task['type']=='remote' ? (int)trim($task['limit']) : '0'?>">
                    </div>
                    <div class="col-sm-8">
                        <p class="form-control-static">0 - without limit</p>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> Save task</button>
                    </div>
                </div>

            </form>
        </div>

    </div>
</div>
<script>
    $('#task_file_update_remote_fraq').prop('disabled', !$('#download-remote-file').prop('checked'));
    $('#download-remote-file').on('change', function(){
        $('#task_file_update_remote_fraq').prop('disabled', !$(this).prop('checked'));
    });
    $('#tabs-newtask a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    })
</script>
