@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 class="btn btn-primary pull-right fa fa-phone"
                                          data-toggle="modal"
                                          data-target="#myModal2" style="display: none">添加到待导列表</span>

                                <span class="btn btn-success pull-right fa fa-table"
                                      style="display: none" data-toggle="modal"
                                      data-target="#myModal1">导出到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>


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

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

                <div id="threads-list">
                    @include('admin.user.threads.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 class="modal inmodal" id="myModal1" 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">选择要导出的字段</h4></div>
                    <small class="font-bold">
                        <div class="modal-body">
                            <div class="ibox float-e-margins">
                                <div class="ibox-content">

                                    <div class="form-group">
                                        <form action="" id="check-fields">
                                            <div class="row">
                                                <div class="col-sm-6">
                                                    <h4 style="color: #ccc">联系方式：</h4>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="phone" disabled>
                                                        <label>
                                                            电话
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="email">
                                                        <label>
                                                            Email
                                                        </label>
                                                    </div>
                                                </div>
                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="qq">
                                                        <label>
                                                            qq
                                                        </label>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="col-sm-6">
                                                    <h4 style="color: #ccc">其他字段：</h4>
                                                </div>
                                            </div>
                                            <div class="row">

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="company_name" disabled>
                                                        <label>
                                                            企业名称
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="ower_name">
                                                        <label>
                                                            线索拥有者
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="website">
                                                        <label>
                                                            企业网址
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="legal_person">
                                                        <label>
                                                            法人
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="reg_capital">
                                                        <label>
                                                            注册资本
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="reg_addr">
                                                        <label>
                                                            注册地址
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="lastest">
                                                        <label>
                                                            最新跟进
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="checkbox checkbox-success">
                                                        <input class="check-fields" type="checkbox" checked="checked"
                                                               name="created_at">
                                                        <label>
                                                            领取时间
                                                        </label>
                                                    </div>
                                                </div>

                                            </div>
                                        </form>
                                    </div>

                                    <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="export_threads">
                                        </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()
                $('.fa-table').show()
            } else {
                $('.fa-phone').hide()
                $('.fa-table').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]
                $('.fa-table').show()
                $('#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) {
                $('#addCallList').show()
                $('.fa-table').show()
            } else {
                $('#addCallList').hide()
                $('.fa-table').hide()
            }

            if (items.length == checkedLength) {
                $('#checkAll').prop('checked', true)
                $('#addCallList').show()
                $('.fa-table').show()
            } else {
                $('#checkAll').prop('checked', false)
            }

            saveChecked($(this))
            console.log(checkedIds)
        });

        /*保存选中的项*/
        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)
            }

        }

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

            $.ajax({
                type: 'post',
                url: '{{ U('User/Threads/addCallList') }}',
                data: {_token: csrf_token, ip: ip,contact_ids: contact_ids},
                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 = checkedIds
            csrf_token = "{{ csrf_token() }}";
            check_fields = $('#check-fields').serialize()

            $.ajax({
                type: 'post',
                url: '{{ U('User/Threads/export_threads') }}',
                data: {threads_ids: threads_ids, type: 1, _token: csrf_token, check_fields: check_fields},
                success: function (data) {
                    $.fileDownload("{{ U('User/Threads/export_threads') }}", {
                        data: {threads_ids: threads_ids, _token: csrf_token, check_fields: check_fields},
                        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)
                    setChecked()
                })
            }

        });
    </script>
@endsection