@extends('admin.layouts.app')

@section('content')
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>通话纪录</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link"> <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="form-group">

                        @if(role('Call/Records/addCallList'))
                            <div class="col-sm-8 pull-right">
                                    <span id="addCallList" class="btn btn-primary pull-right fa fa-phone"
                                          style="display: none">添加到电话列表</span>
                            </div>
                        @endif
                        {{--@if(role('Call/Records/create'))--}}
                        {{--<div class="col-sm-8 pull-right">--}}
                        {{--<a href="{{ U('Call/Records/create')}}" class="btn btn-primary pull-right">添加</a>--}}
                        {{--</div>--}}
                        {{--@endif--}}
                    </div>
                </div>

                {{-- 过滤条件 --}}
                <table class="table table-striped table-bordered table-hover dataTables-example dataTable">
                    <tr>
                        <th> 过滤条件</th>
                        <form method="GET" action="" accept-charset="UTF-8" id="filter_records">
                            <th>
                                <select name="ip" class="form-control" onchange="filter_records()">
                                    <option value="">拨打IP</option>
                                    <option value="172.31.20.133">172.31.20.133</option>
                                    <option value="172.31.20.134">172.31.20.134</option>
                                    <option value="172.31.20.135">172.31.20.135</option>
                                    <option value="172.31.20.136">172.31.20.136</option>
                                    <option value="172.31.20.137">172.31.20.137</option>
                                </select>
                            </th>

                            <th>
                                <select name="term_status" class="form-control" onchange="filter_records()">
                                    <option value="">接听状态</option>
                                    <option value="200">已接通</option>
                                    <option value="408">未接通</option>
                                </select>
                            </th>

                            <th>
                                <div class="input-group">
                                    <input type="text" class="form-control" value="{{Request::get('phone')}}"
                                           placeholder="请输入电话号码"
                                           name="phone">
                                    <span class="input-group-append">
                                                <span class="btn btn-sm btn-default"
                                                      style="height: 100%" onclick="filter_records()">搜索</span>
                                            </span>
                                </div>
                            </th>
                        </form>
                    </tr>
                </table>

                <div id="records-list">
                    @include('admin.call.records.data')
                </div>

            </div>
        </div>
    </div>
    </div>
@endsection

@section('js')
    <script type="text/javascript">
        /*电话全选功能*/

        $('body').on('click', '#checkAll', function () {
            items = $('.contacts');

            isChecked = $(this).prop('checked')
            items.prop('checked', isChecked)
            if (isChecked == true) {
                $('#addCallList').show()
            } else {
                $('#addCallList').hide()
            }
        })

        $('body').on('click','.contacts',function () {
            items = $('.contacts');

            checkedLength = $('.contacts:checked').length
            if (checkedLength) {
                $('#addCallList').show()
            } else {
                $('#addCallList').hide()
            }
            if (items.length == checkedLength) {
                $('#addCallList').show()
            } else {
                $('#checkAll').prop('checked', false)
            }
        })


        /*添加选择的电话到拨打列表*/
        $('#addCallList').click(function () {
            contact_phones = [];
            csrf_token = "{{ csrf_token() }}";
            $('.contacts:checked').each(function () {
                contact_phones.push($(this).val())
            });

            $.ajax({
                type: 'post',
                url: '{{ U('Call/Records/addCallList') }}',
                data: {contact_phones: contact_phones, _token: csrf_token},
                success: function (data) {
                    if (data == 200) {
                        layer.msg('导入成功', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            window.location.href = window.location.href
                        });

                    }
                }

            })
        });

        /*导出通话纪录到excel*/
        {{--$('#export_call').click(function () {--}}
        {{--call_ids = [];--}}
        {{--csrf_token = "{{ csrf_token() }}";--}}
        {{--$('.contacts:checked').each(function () {--}}
        {{--call_ids.push($(this).val())--}}
        {{--});--}}

        {{--$.ajax({--}}
        {{--type: 'post',--}}
        {{--url: '{{ U('Call/Records/export_call') }}',--}}
        {{--data: {call_ids: call_ids, _token: csrf_token},--}}
        {{--success: function (data) {--}}
        {{--if (data == 200) {--}}
        {{--layer.msg('导入成功', {--}}
        {{--icon: 1,--}}
        {{--time: 2000 //2秒关闭（如果不配置，默认是3秒）--}}
        {{--}, function () {--}}
        {{--window.location.href = window.location.href--}}
        {{--});--}}

        {{--}--}}
        {{--}--}}

        {{--})--}}
        {{--});--}}

        /*通话纪录筛选*/
        function filter_records() {
            data = $('#filter_records').serialize();
            $.ajax({
                type: 'get',
                data: data,
            }).done(function (data) {
                $('#records-list').html(data.html)
            })
        }

        /*Ajax分页*/
        $('body').on('click', '.pagination a', function (e) {
            e.preventDefault();
            if ($(this).attr('href') != '#') {
                data = $('#filter_records').serialize()
                $.ajax({
                    url: $(this).attr('href'),
                    type: 'get',
                    data: data,
                }).done(function (data) {
                    $('#records-list').html(data.html)
                })
            }

        });
    </script>
@endsection