{% load i18n %}

<style>
    .ui-autocomplete {
        z-index: 9999;
    }
</style>

<script type="text/javascript">
    $(document).ready(function () {
        var page_length = 10;
        var table = $('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: true,
            pageLength: page_length,
            buttons: [{ extend: 'excelHtml5', text: '导出到Excel' }],
            language: lng,
            data: {{ data| safe}}
        });
    table.columns().every(function () {
        var that = this;
        $('input', this.footer()).on('keyup change clear', function () {
            if (that.search() !== this.value) {
                that.search(this.value).draw();
            }
        });
    });
    $('#tb').on("click", "td a", function (event) {
        event.preventDefault();
        $.RefreshContent($(this).attr('href'));
    });
 
    $('.datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
    $('.datepicker').datepicker('setDate', new Date());
    setDatePickerZh();

    $('#dt_fr').val('{{date_from}}');
    $('#dt_to').val('{{date_to}}');

    $('.btn-qry').click(function () {
        var date_from, date_to;
        date_fr = $('#dt_fr').val();
        date_to = $('#dt_to').val();
        var params = [];
        if (date_fr != '') { params.push('date_from=' + date_fr) };
        if (date_to != '') { params.push('date_to=' + date_to) };

        if (params.length == 0) {
            bootbox.alert("{% trans '请先输入查询条件！'%}");
        } else {
            $.RefreshContent('{{request.path}}?' + params.join('&'));
        }
    }); 
    });



    function deleteItem(id) {
        var r = confirm('您确定要删除吗？');
        if (r) {
            var durl = '{{request.path}}delete/' + id + '/'
            $.RefreshContent(durl);
        }
    }
    function permitIt(id,e) { 
        var event = e || window.event;
        var btn = event.target;


        var r = confirm('是否将状态改为允许？');
        if (r) {
            var durl = '{{request.path}}permit/' + id + '/'
            //$.RefreshContent(durl);
            $.ajax({
                url:durl,
                success:function(res){
                    if(res.e === ''){

                        $(btn).hide();
                        $(btn).siblings().text('是').css({'color':'green'})
                    }
                    else{
                        alert(res.e);
                    }
                }
            })
        }
    }



    $('#user_label').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/obj/ajax/autocomplete/user/',
                dataType: "json",
                data: {
                    term: $("#user_label").val(),//搜索栏里的内容
                    company_id: '{{request.user.company.id}}',//额外参数
                },
                success: function (data) {
                    response(data);
                }
            });
        },
        select: function (event, ui) {
            $("#id_user").val(ui.item.value);
            $("#user_label").val(ui.item.label)
            return false;
        }
    });

    $('#modal_order').on('shown.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var id = button.data('id') // Extract info from data-* attributes
        $('#id_record').val(id)
    })


    $("#btn-save").click(function () {
        if ($("#id_user").val() !== '') {
            $.SubmitForm('#form_1', '{{request.path}}');
            $('#modal_order').modal('hide')
        }
        else {
            alert('请选择业务员')
        }

    })

</script>



<ol class="breadcrumb">
    <li>{{subtt.0}}</li>
    <li>{{subtt.1}}</li>
</ol>


 
<form role="form" class="form-inline" style="margin-top:0px;padding-left:10px;">
    <div class="col-sm-2 input-group">
        <span class="input-group-addon">{% trans "开始日期"%}</span>
        <input class="datepicker form-control" id="dt_fr" />
    </div>
    <div class="col-sm-2 input-group">
        <span class="input-group-addon">{% trans "结束日期"%}</span>
        <input class="datepicker form-control" id="dt_to" />
    </div>
    <div class="col-sm-2 input-group">
        <span class='input-group-btn'>
            <a type="button" class='btn btn-primary btn-qry'>
                <span class='glyphicon glyphicon-search' style='padding-right:5px;'></span>{% trans '查找' %}
            </a>
        </span>
    </div>
</form>
<div class="clearfix"></div> 


<div class="col-md-12 col-sm-12 col-xs-12">
    <div class="x_panel">
        <div class="table-responsive">
            <table id='tb' class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                <thead>
                    {% for c in clmns %}
                    <th>{{c}}</th>
                    {% endfor %}
                </thead>
                <tbody></tbody>
                <tfoot>
                    {% for c in clmns %}
                    <th><input type="text" placeholder="{{c}}"></th>
                    {% endfor %}
                </tfoot>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="modal_order" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">分配业务员</h4>
            </div>
            <div class="modal-body">

                <form role="form" method='POST' enctype="multipart/form-data" class="form-horizontal" id="form_1">
                    {% csrf_token %}


                    <div class="form-group">
                        <label for="user_label">用户账号</label>
                        <div>
                            <input id='user_label'>
                        </div>
                    </div>
                    <input type="hidden" id="id_user" name="user_id" />
                    <input type="hidden" id="id_record" name="id" />

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="btn-save">确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->