{% extends 'base.html' %}
{% block content %}
{% include 'nav_cat_bar.html' %}
<script type="text/javascript">
    function search_ip(text, noselect, total){
        $("#" + noselect).children().each(
            function(){
                $(this).remove();
            });

        $("#" + total).children().each(function(){
            if($(this).text().search(text) != -1){
                $("#" + noselect).append($(this).clone())
            }
            })
    }
</script>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-10">
            <div id="add_asset" class="ibox float-e-margins">
                <div id="ibox-content" class="ibox-title">
                    <h5> 填写要申请主机的基本信息 </h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                        </ul>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>

                <select id="assets_total" name="assets" class="form-control m-b" size="12" multiple style="display: none">
                    {% for asset in posts %}
                        <option value="{{ asset.ip }}">{{ asset.ip }}</option>
                    {% endfor %}
                </select>

                <select id="asset_select_total" name="j_hosts" class="form-control m-b" size="12"  multiple style="display: none">
                    {% for asset in eposts %}
                    <option value="{{ asset.ip }}">{{ asset.ip }}</option>
                    {% endfor %}
                </select>

                <div class="ibox-content">
                    {% if emg %}
                        <div class="alert alert-warning text-center">{{ emg }}</div>
                    {% endif %}
                    {% if smg %}
                        <div class="alert alert-success text-center">{{ smg }}</div>
                    {% endif %}
                    <form id="assetForm" method="post" class="form-horizontal">
                        {% csrf_token %}
                        <div class="form-group"><label class="col-sm-2 control-label"> 申请人 <span class="red-fonts">*</span></label>
                            <div class="col-sm-8"><input type="text" name="applyer" value="{{  user.name }}" class="form-control" readonly="readonly"></div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label"> 所在用户组 <span class="red-fonts">*</span></label>
                            <div class="col-sm-8"><input type="text" name="dept" value="{{  user.group.all | join:',' }}" class="form-control" readonly="readonly"></div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group" id="j_da"><label class="col-sm-2 control-label"> 管理员 <span class="red-fonts">*</span></label>
                            <div class="radio">
                                    <label><input type="radio" value="{{ admin.id }}" name="da"> {{ admin.name }}</label>
                                {% for da in dept_da %}
                                    <label><input type="radio" value="{{ da.id }}" name="da"> {{ da.name }}</label>
                                {% endfor %}
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label for="group" class="col-lg-2 control-label">主机组</label>
                            <div class="col-sm-8">
                                <select id="group" name="group" class="form-control m-b" multiple size="10">
                                    {% for g in egroup %}
                                        <option type="checkbox" value="{{ g.name }}">{{ g.name }} {% if g.comment %} --- {{ g.comment }} {% endif %}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label for="group_name" class="col-sm-2 control-label">过滤</label>
                            <div class="col-sm-4">
                                <input id="noselect" class="form-control" oninput="search_ip(this.value, 'assets', 'assets_total')">
                            </div>
                            <div class="col-sm-1">
                            </div>
                            <div id="select" class="col-sm-3">
                                <input  class="form-control" oninput="search_ip(this.value, 'asset_select', 'asset_select_total')">
                            </div>
                        </div>


                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">主机<span class="red-fonts">*</span></label>
                            <div class="col-sm-4">
                                <div>
                                    <select id="assets" name="assets" class="form-control m-b" size="12" multiple>
                                        {% for post in posts %}
                                        <option value="{{ post.ip }}">{{ post.ip }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>

                            <div class="col-sm-1">
                                <div class="btn-group" style="margin-top: 60px;">
                                    <button type="button" class="btn btn-white" onclick="move('assets', 'asset_select', 'assets_total', 'asset_select_total'  )"><i class="fa fa-chevron-right"></i></button>
                                    <button type="button" class="btn btn-white" onclick="move_left('asset_select', 'assets', 'asset_select_total', 'assets_total')"><i class="fa fa-chevron-left"></i> </button>
                                </div>
                            </div>

                            <div class="col-sm-3">
                                <div>
                                    <select id="asset_select" name="hosts" class="form-control m-b" size="12"  multiple></select>
                                </div>
                            </div>
                        </div>


                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label"> 申请说明 </label>
                            <div class="col-sm-8"><textarea type="text" placeholder="" name="comment" class="form-control" rows="5" cols="20"></textarea></div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-5">
                                <button class="btn btn-primary" id="submit_button" type="submit"> 提交 </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
        $("#submit_button").click(function(){
            $('#assetForm option').each(function(){
                $(this).prop('selected', true)
            })
        });

    })

    $('#assetForm').validator({
    timely: 2,
    theme: "yellow_right_effect",
    fields: {
        "j_da": {
            rule: "required",
            tip: "选择管理员",
            ok: "",
            msg: {required: "管理员必须选择!"},
            data: {'data-ok':"ok"}
        }
    },
    valid: function(form) {
        form.submit();
        }
    });

    var $rows = $('#groups option');
    $('#search').keyup(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

        $rows.show().filter(function() {
            var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
            return !~text.indexOf(val);
        }).hide();
    });
</script>
{% endblock content %}