@extends('admin.layouts.app')
@section('header')
    <script src="/base/js/plugins/fileDownload/jquery.fileDownload.js"></script>
@endsection
@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('User/Threads/addCallList'))
                            <div class="col-sm-8 pull-right">
                                    <span id="addCallList" class="btn btn-primary pull-right fa fa-phone"
                                          style="display: none">添加到电话列表</span>
                                <span id="export_threads" class="btn btn-success pull-right fa fa-table"
                                      style="display: none">导出到Excel</span>
                            </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_threads">
                            <th>
                                <select name="process" class="form-control" onchange="filter_threads()">
                                    <option value="">有无跟进</option>
                                    <option value="1">有</option>
                                    <option value="2">无</option>
                                </select>
                            </th>

                            {{--<th style="width: 40%">--}}
                            {{--<div class="row">--}}

                            {{--<div class="col-sm-5" 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-5" 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>--}}

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


                            <th>

                                <div class="input-group">
                                    <input type="text" class="form-control" value="{{Request::get('keyword')}}"
                                           placeholder="请输入企业名称"
                                           name="keyword">
                                    <span class="input-group-append">
                                                <span class="btn btn-sm btn-default"
                                                      style="height: 100%" onclick="filter_threads()">搜索</span>
                                            </span>
                                </div>

                            </th>
                        </form>
                    </tr>
                </table>

                <div id="threads-list">
                    @include('admin.user.threads.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();
                $('#export_threads').show()
            } else {
                $('#addCallList').hide();
                $('#export_threads').hide()
            }
        });

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

        /*添加选择的电话到拨打列表*/
        $('#addCallList').click(function () {
            contact_ids = []
            csrf_token = "{{ csrf_token() }}"
            $('.contacts:checked').each(function () {
                contact_ids.push($(this).val())
            })
            $.ajax({
                type: 'post',
                url: '{{ U('User/Threads/addCallList') }}',
                data: {contact_ids: contact_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
                        });
                    }
                }

            })
        });

        /*导出线索到excel*/
        $('#export_threads').click(function () {
            threads_ids = [];
            csrf_token = "{{ csrf_token() }}";
            $('.contacts:checked').each(function () {
                threads_ids.push($(this).val())
            });

            $.ajax({
                type: 'post',
                url: '{{ U('User/Threads/export_threads') }}',
                data: {threads_ids: threads_ids, type: 1, _token: csrf_token},
                success: function (data) {
                    $.fileDownload("{{ U('User/Threads/export_threads') }}", {
                        data: {threads_ids: threads_ids, _token: csrf_token},
                        prepareCallback: function (url) {
                            console.log("开始下载");
                        },
                        successCallback: function (url) {
                            console.log("正在下载，请稍后...");
                            console.log("SUCCESS", "导出完成！");
                        },
                        failCallback: function (html, url) {
                            console.log("正在下载，请稍后...");
                            console.log("ERROR", "导出失败，未知的异常！");
                        }
                    });
                }

            })
        });

        /*线索筛选*/
        function filter_threads() {
            data = $('#filter_threads').serialize()
            $.ajax({
                type: 'get',
                data: data,
            }).done(function (data) {
                $('#threads-list').html(data.html)
            })
        }

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

        });
    </script>
@endsection