{% extends '../../module/main.nunj' %}
{% block js %}
    <script src="/static/common/json-format/c.js"></script>
{% endblock %}
{% block content %}

    <div class="row">
        <!--table-->
        <div class="col-md-12">
            <form action="">
                <div class="item">
                    <div class="item-head clearfix">
                        <div class="pull-right form-inline">
                            <div class="input-group">
                            </div>
                            <div class="input-group">
                                <div class="has-feedback">
                                    <input type="hidden" name="project_id" value="{{ project_id }}">
                                    <input type="text" class="form-control" placeholder="{{ LN.interface.index.searchPlaceHolder }}" name="keyword" value="{{ keyword }}">
                                    <i class="fa fa-times-circle form-control-feedback" data-role="button"></i>
                                </div>
                                <span class="input-group-btn">
                        <button class="btn btn-info" type="submit">{{ LN.interface.index.search }}</button>
                        </span>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <div class="item">
                <div class="item-head clearfix">
                    <h4 class="pull-left">{{ project.project_name }}</h4>
                    {#<div class="pull-right form-inline">#}
                    {#<div class="input-group">#}
                    {#<div class="has-feedback">#}
                    {#<input type="text" class="form-control">#}
                    {#<i class="fa fa-times-circle form-control-feedback" data-role="button"></i>#}
                    {#</div>#}
                    {#<span class="input-group-btn">#}
                    {#<button class="btn btn-info" type="button">搜搜</button>#}
                    {#</span>#}
                    {#</div>#}
                    {#</div>#}
                    <button style="padding-right: 20px;float: right" data-is-proxy="1" id="oer_all_proxy" class="btn btn-info" type="button">{{ LN.interface.index.closeAllProxies }}</button>
                    <span style="padding-right: 20px;float: right">
                            <a class="btn btn-info" href="/interface/add/{{ project_id }}">{{ LN.interface.index.addInterface }}</a>
                                                        </span>
                </div>
                <div class="item-main">
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>{{ LN.interface.index.projectName }}</th>
                            <th>{{ LN.interface.index.interfaceName }}</th>
                            <th>{{ LN.interface.index.interfaceUrl }}</th>
                            <th>{{ LN.interface.index.againProxy }}</th>
                            <th>{{ LN.interface.index.actions }}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for item in list %}
                            <tr>
                                <td>{{ item.mockid }}</td>
                                <td>{{ item.project_name }}</td>
                                <td>{{ item.api_name }}</td>
                                {% if item.proxy_prefix %}
                                    {% set cur_prefix=item.proxy_prefix %}
                                {% elseif item.proxy_url %}
                                    {% set cur_prefix=item.proxy_url %}
                                {% else %}
                                    {% set cur_prefix=systemData.proxy_url %}

                                {% endif %}
                                <td><a target="_blank" style="color: #ff7e1d;" href="{{ item.project_prefix +item.api_url }}">{{ item.api_url }}</a>
                                    <p style="display: {{ 'block' if item.is_proxy ==1  else 'none' }}"
                                       class="J_proxy_all_tips J_proxy_tips_{{ item.mockid }}">{{ LN.interface.index.openProxyTipsPrefix }}[
                                        <span style="color: red;">
                                      {{ cur_prefix+item.project_prefix+item.api_url }}</span>]{{ LN.interface.index.openProxyTipsPostfix|safe }}</p>
                                </td>
                                <td class="proxy_state">{{ LN.interface.index.closed if item.is_proxy==0 else LN.interface.index.opened }}</td>
                                <td><a class="deleteLnk" href="/interface/delete?mockid={{ item.mockid }}">{{ LN.interface.index.delete }}</a>
                                    <a href="/interface/edit?mockid={{ item.mockid }}">{{ LN.interface.index.edit }}</a>
                                    <a href="/interface/add?iscopy=1&mockid={{ item.mockid }}">{{ LN.interface.index.clone }}</a>
                                    <a href="{{ item.project_prefix if item.project_prefix else '/api/' }}{{ item.api_url }}" target="_blank">{{ LN.interface.index.detail }}</a>
                                    <a href="javascript:;" mockid="{{ item.mockid }}" class="oper_proxy" data-is-proxy="{{ item.is_proxy }}">
                                        {% if item.is_proxy==0 %}
                                            {{ LN.interface.index.openProxy }}
                                        {% else %}
                                            {{ LN.interface.index.closeProxy }}
                                        {% endif %}
                                    </a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <!--table end-->
    </div>
    <script>
        $('.deleteLnk').click(function() {
            if (confirm('{{ LN.interface.index.deleteTips }}')) {
                return true;
            } else {
                return false;
            }
        })
        $('.fa-times-circle').click(function() {
            $(this).prev('.form-control').val('')
        })
        $('.oper_proxy').click(function() {
            var _this = $(this), is_proxy = $(this).attr('data-is-proxy'),
                mockid = $(this).attr('mockid'), txt = is_proxy === "0" ? '{{ LN.interface.index.closeProxy }}' : '{{ LN.interface.index.openProxy }}',
                oper_proxy = is_proxy === "0" ? 1 : 0,
                state = is_proxy === "0" ? '{{ LN.interface.index.opened }}' : '{{ LN.interface.index.closed }}';
            $.getJSON('/interface/setproxy', {mockid: mockid, is_proxy: oper_proxy}).done(function(data) {
                if (data && data.errno === 0) {
                    _this.html(txt).attr('data-is-proxy', oper_proxy);
                    _this.parent().siblings('.proxy_state').html(state);
                    is_proxy === "0" ? $('.J_proxy_tips_' + mockid).show() : $('.J_proxy_tips_' + mockid).hide()
                } else {
                    $.commonTips(data.errmsg, 'danger', 1500);
                }
            }).fail(function(err) {
                alert(err.message)
            });
        })
        $('#oer_all_proxy').click(function() {
            var _this = $(this), is_proxy = $(this).attr('data-is-proxy'),
                txt = is_proxy === "0" ? '{{ LN.interface.index.closeAllProxies }}' : '{{ LN.interface.index.openAllProxies }}',
                txtitem = is_proxy === "0" ? '{{ LN.interface.index.closeProxy }}' : '{{ LN.interface.index.openProxy }}',
                oper_proxy = is_proxy === "0" ? 1 : 0,
                state = is_proxy === "0" ? '{{ LN.interface.index.opened }}' : '{{ LN.interface.index.closed }}';
            var mockids = $('.oper_proxy').map(function() {
                return $(this).attr('mockid');
            }).get().join(",");
            $.getJSON('/interface/setproxies', {is_proxy: oper_proxy, mockids: mockids}).done(function(data) {
                if (data && data.errno === 0) {
                    $('.oper_proxy').html(txtitem);
                    _this.html(txt).attr('data-is-proxy', oper_proxy);
                    is_proxy === "0" ? $('.J_proxy_all_tips').show() : $('.J_proxy_all_tips').hide()
                    $('.proxy_state').html(state)
                } else {
                    $.commonTips(data.errmsg, 'danger', 1500);
                }
            }).fail(function(err) {
                alert(err.message)
            });
        })
    </script>
{% endblock %}