@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 class="btn btn-primary pull-right fa fa-phone" data-toggle="modal"
                                      data-target="#myModal2" 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>

                {{-- 过滤条件 --}}
                <div class="col-xl-12" style="padding: 5px">
                    <form method="GET" action="" accept-charset="UTF-8" id="filter_records">
                        {{ csrf_field() }}
                        <div class="row">
                            <div class="col-sm-1" style="font-weight: 500;text-align: right;color: #999;">
                                <h4>过滤条件：</h4>
                            </div>
                            <div class="col-sm-2">
                                <select name="ip" class="form-control" onchange="filter_records()">
                                    <option value="">拨打IP</option>
                                    <option value="172.31.20.181">172.31.20.181</option>
                                    <option value="172.31.20.182">172.31.20.182</option>
                                    <option value="172.31.20.183">172.31.20.183</option>
                                    <option value="172.31.20.184">172.31.20.184</option>
                                    <option value="172.31.20.185">172.31.20.185</option>
                                    <option value="172.31.20.186">172.31.20.186</option>
                                    <option value="172.31.20.187">172.31.20.187</option>
                                    <option value="172.31.20.188">172.31.20.188</option>

                                </select>
                            </div>

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


                            <div class="col-sm-2" id="data_1">
                                <div class="input-group date">
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar"></i></span>
                                    <input type="text" id="start" class="form-control" placeholder="开始日期" name="start"
                                           value="{{Request::get('start') ? : ''}}">
                                </div>
                            </div>


                            <div class="col-sm-2" id="data_2">
                                <div class="input-group date">
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar"></i></span>
                                    <input type="text" id="end" class="form-control" placeholder="结束日期" name="end"
                                           value="{{Request::get('end') ? : ''}}">
                                </div>
                            </div>


                            <div class="col-sm-2">
                                <div class="input-group">
                                    <input type="text" class="form-control" value="{{Request::get('phone')}}"
                                           placeholder="请输入电话号码或标签"
                                           name="phone">
                                    <span class="input-group-append">
                                                <button type="button" class="btn btn-sm btn-default"
                                                      onclick="filter_records()">搜索</button>
                                            </span>
                                </div>
                            </div>


                        </div>
                    </form>

                </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>--}}

                {{--</th>--}}

                {{--<th>--}}

                {{--</th>--}}

                {{--<th>--}}


                {{--</th>--}}

                {{--<th>--}}

                {{--</th>--}}
                {{--</form>--}}
                {{--</tr>--}}
                {{--</table>--}}

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

            </div>
        </div>

        <div class="modal inmodal" id="myModal2" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content animated flipInY">
                    <div class="modal-header">
                        <h4 class="modal-title">选择拨打的IP</h4></div>
                    <small class="font-bold">
                        <div class="modal-body">
                            <div class="ibox float-e-margins">
                                <div class="ibox-content">
                                    <select name="ip" class="form-control" id="ip">
                                        <option value="172.31.20.181">172.31.20.181</option>
                                        <option value="172.31.20.182">172.31.20.182</option>
                                        <option value="172.31.20.183">172.31.20.183</option>
                                        <option value="172.31.20.184">172.31.20.184</option>
                                        <option value="172.31.20.185">172.31.20.185</option>
                                        <option value="172.31.20.186">172.31.20.186</option>
                                        <option value="172.31.20.187">172.31.20.187</option>
                                        <option value="172.31.20.188">172.31.20.188</option>

                                    </select>

                                    <div class="form-group">
                                        <label class="control-label col-sm-3">&nbsp;</label>
                                        <div class="col-sm-9">
                                            <input type="submit" class="btn btn-success" style="margin-right:20px;"
                                                   id="addCallList">
                                            <input type="reset" class="btn btn-default">
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                        </div>
                    </small>
                </div>
                <small class="font-bold">
                </small>
            </div>
            <small class="font-bold">
            </small>
        </div>
    </div>
@endsection

@section('js')
    <script type="text/javascript">
        var checkedIds = []

        /*电话全选功能*/
        $('body').on('click', '#checkAll', function () {
            items = $('.contacts');

            isChecked = $(this).prop('checked')
            items.prop('checked', isChecked)
            items.each(function () {
                saveChecked($(this))
            });
            if (checkedIds.length) {
                $('.fa-phone').show()
            } else {
                $('.fa-phone').hide()
            }

        })

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

            isChecked = $(this).prop('checked')
            items.prop('checked', isChecked)

            if (isChecked == true) {
                $('.fa-phone').show()
                checkedIds = [{{ $allIds }}][0]
                $('#checkAll').prop('checked', true)
            } else {
                $('.fa-phone').hide()
                $('#checkAll').prop('checked', false)
                checkedIds = []
            }
        })

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

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

            saveChecked($(this))
        });


        /*添加选择的电话到拨打列表*/
        $('#addCallList').click(function () {
            contact_phones = checkedIds;
            csrf_token = "{{ csrf_token() }}";
            ip = $('#ip').val()

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

                    }
                }

            })
        });

        /*保存选中的项*/
        function saveChecked(e) {
            if (e.is(":checked") && checkedIds.indexOf(e.data("id"), 0) == -1) {
                checkedIds.push(e.data("id"));
            } else {
                for (var i = 0; i < checkedIds.length; i++) {
                    if (e.data("id") == checkedIds[i]) {
                        checkedIds.splice(i, 1);
                        break;
                    }
                }
            }

        }

        /*翻页后设置选中项*/
        function setChecked() {
            var $boxes = $('.contacts');
            $boxes.each(function () {
                id = $(this).data('id')
                if (checkedIds.indexOf(id, 0) != -1) {
                    $(this).prop('checked', true)
                } else {
                    $(this).prop('checked', false)
                }
            })

            checkedLength = $('.contacts:checked').length
            if (checkedLength == $boxes.length) {
                $('#checkAll').prop('checked', true)
            }

        }


        /*通话纪录筛选*/
        function filter_records() {
            console.log(1)
            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)
                    setChecked()
                })
            }

        });


    </script>
@endsection