<?php $__env->startSection('styles'); ?>

    <link href="/assets/global/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" rel="stylesheet"
          type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css" rel="stylesheet"
          type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-editable/bootstrap-editable/css/bootstrap-editable.css"
          rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-editable/inputs-ext/address/address.css" rel="stylesheet"
          type="text/css"/>


    <style>

        .pac-container {
            z-index: 10052 !important;
        }

        #map {
            width: auto !important;
            height: 500px !important;
        }

        #map_edit {
            width: auto !important;
            height: 500px !important;
        }

        tfoot {
            display: table-header-group;
        }

        tfoot input {
            font-weight: normal !important;
        }

        tfoot select {
            font-weight: normal !important;
        }

        .dataTables_filter {
            display: none;
        }

        #orders-table_length {
            display: none;
        }

        .ws-nr {
            width: 100% !important;
        }

    </style>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
    <?php if(session()->has('error')): ?>
        <div class="alert alert-danger"><?php echo e(session()->get('error')); ?></div>
    <?php endif; ?>
    <?php if(session()->has('success')): ?>
        <div class="alert alert-success"><?php echo e(session()->get('success')); ?></div>
    <?php endif; ?>
    <?php if(count($errors) > 0): ?>
        <div class="alert alert-danger">
            <ul>
                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <li><?php echo e($error); ?></li>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </ul>
        </div>
    <?php endif; ?>
    <h1 class="page-title">
        Danh sách khách hàng tiềm năng
    </h1>

    <div class="row">
        <div class="col-md-12">
            <a class="btn red" id="btn-add" data-toggle="modal" href="#add-lead-form">
                <i class="fa  fa-plus"></i> Thêm
            </a>
            <button class="btn green-meadow fileinput-button" id="btn-add-excel" data-toggle="modal" href="#add-lead">
                <i class="fa  fa-upload"></i> Nhập excel
            </button>

            <a class="btn blue fileinput-button" href="/sample_lead.xlsx" target="_blank"><i
                        class="fa  fa-download"></i> Tải mẫu nhập</a>
        </div>
    </div>
    <div class="row">
        <div class="portlet-body form">
            <div class="form-body">
                <div class="form-group">
                    <form action="#" class="mt-repeater form-horizontal">
                        <div data-repeater-list="group-a">
                            <div data-repeater-item="" class="mt-repeater-item">
                                <div class="mt-repeater-input">
                                    <label class="control-label">Tỉnh / Thành phố</label>
                                    <br>
                                    <select name="province" class="form-control select2" id="province">
                                        <option value="">Chọn tỉnh / thành phố</option>
                                        <?php $provinces = App\Models\Province::all(); ?>
                                        <?php $__currentLoopData = $provinces; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $province): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($province->provinceid); ?>"><?php echo e($province->name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                                <div class="mt-repeater-input">
                                    <label class="control-label">Quận / huyện</label>
                                    <br>
                                    <select name="district" class="form-control select2" id="district">
                                    </select>
                                </div>
                                <div class="mt-repeater-input">
                                    <label class="control-label">Phường / xã</label>
                                    <br>
                                    <select name="ward" class="form-control select2" id="ward">

                                    </select>
                                </div>
                                <div class="mt-repeater-input">
                                    <a href="javascript:;" onclick="getPostByAttr()" data-repeater-delete=""
                                       class="btn btn-danger mt-repeater-delete">
                                        <i class="fa fa-check"></i> Lọc</a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-striped table-bordered table-hover" id="orders-table">
                <thead>
                <tr style="background-color: #5376B9; color: white">
                    <th>Tên</th>
                    <th>SĐT</th>
                    <th>Email</th>
                    <th>Địa chỉ</th>
                    <th>Thành phố</th>
                    <th>Quận huyện</th>
                    <th>Phường xã</th>
                    <th>Nguồn</th>
                    <th>Link nguồn</th>
                    <th>Giá tối thiểu</th>
                    <th>Giá tối đa</th>
                    <th>Nội dung</th>
                    <th>Trạng thái</th>
                    <th>Ngày tạo</th>
                    <th>Hành động</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th>
                        <select class="form-control">
                            <option value="">Tất cả</option>
                            <option value="<?php echo e(App\Models\Lead::ACTIVE); ?>">Kích hoạt</option>
                            <option value="<?php echo e(App\Models\Lead::INACTIVE); ?>">Không kích hoạt</option>
                        </select>
                    </th>
                    <th><input type="text" class="form-control"></th>
                    <th></th>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12" id="detail">
        </div>
    </div>

    <div class="modal fade" id="add-lead" tabindex="-1" role="basic" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title">Nhập file excel</h4>
                </div>

                <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                <div class="modal-body" id="add-lead-content">
                    <form action="<?php echo e(url('admin/lead/excel')); ?>" method="post" id="form-add-lead"
                          enctype="multipart/form-data">
                        <div class="form-group">
                            <label>Chọn file<span class="required" aria-required="true"> (*) </span></label>
                            <input type="file" id="nhap-mvd-input" name="file">
                            <p class="help-block"> (.xls, .xlsx) </p>
                        </div>
                    </form>

                </div>
                <div class="modal-footer">

                    <button type="button" class="btn dark btn-outline" data-dismiss="modal">Đóng</button>
                    <button type="submit" class="btn green" id="btn-save-lead">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
    </div>

    <div class="modal fade bs-modal-lg" id="add-lead-form" role="basic" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title">Thêm lead</h4>
                </div>
                <div class="modal-body" id="add-lead-form-content">

                </div>
                <div class="modal-footer">

                    <button type="button" class="btn dark btn-outline" data-dismiss="modal">Đóng</button>
                    <button type="submit" class="btn green" id="btn-save-lead-form">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
    </div>


    <div class="modal fade bs-modal-lg" id="edit-lead" role="basic" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title">Cập nhật lead</h4>
                </div>

                <div class="modal-body" id="edit-lead-form-content">

                </div>
                <div class="modal-footer">

                    <button type="button" class="btn dark btn-outline" data-dismiss="modal">Đóng</button>
                    <button type="submit" class="btn green" id="btn-save-edit-lead-form">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
    </div>




<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>

    <script>
        $.validator.addMethod("greaterThan",
            function (value, element, param) {
                var $otherElement = $(param);
                return parseInt(value, 10) >= parseInt($otherElement.val(), 10);
            }
        );
    </script>


<?php $__env->stopPush(); ?>


<?php $__env->startPush('scripts'); ?>
    <script>
        var table;

        function getPostByAttr() {
            table.ajax.reload();
        }

        $(function () {

            table = $('#orders-table').DataTable({
                "bDestroy": true,
                processing: true,
                searching: true,
                serverSide: true,
                aaSorting: [],
                ajax: {
                    url: '<?php echo url('admin/lead/lead.data'); ?>',
                    data: function (d) {
                        d.province_id = $('#province').val();
                        d.district_id = $('#district').val();
                        d.ward_id = $('#ward').val();
                    }
                },
                columns: [
                    {data: 'name', name: 'name'},
                    {data: 'phone', name: 'phone'},
                    {data: 'email', name: 'email'},
                    {data: 'address', name: 'address'},

                    {data: 'province_id', name: 'province_id'},
                    {data: 'district_id', name: 'district_id'},
                    {data: 'ward_id', name: 'ward_id'},

                    {data: 'source', name: 'source'},
                    {data: 'source_link', name: 'source_link'},

                    {data: 'price_min', name: 'price_min'},
                    {data: 'price_max', name: 'price_max'},

                    {data: 'note', name: 'note'},
                    {data: 'status', name: 'status'},
                    {data: 'created_at', name: 'created_at'},
                    {data: 'action', name: 'action', 'orderable': false, searchable: false},
                ],
            });

            table.columns().every(function (index) {
                if (index !== 0) {
                    var that = this;
                    $('input', this.footer()).on('keyup', function () {
                        if (that.search() !== this.value) {
                            that
                                .search(this.value)
                                .draw();
                        }
                    });

                    $('select', this.footer()).on('change', function () {
                        if (that.search() !== this.value) {
                            that
                                .search(this.value)
                                .draw();
                        }
                    });
                }
            });

        });

        $(document).on('click', '#btn-add', function (e) {
            e.preventDefault();

            $.ajax({
                url: '<?php echo e(url('admin/lead/create')); ?>',
                type: 'get',
                dataType: 'json',
                beforeSend: function () {
                    $('#add-lead-form-content').addClass('ht-on-loading');
                    $('#edit-lead-form-content').html('');
                },
                success: function (response) {
                    $('#add-lead-form-content').removeClass('ht-on-loading').html(response.data);
                    $('.select2').select2();
                }
            });
        });

        $(document).on('click', '.btn-edit', function (e) {
            e.preventDefault();

            var id = $(this).attr('data-id');
            $.ajax({
                url: '<?php echo e(url('admin/lead/edit')); ?>',
                type: 'get',
                data: {
                    id: id
                },
                dataType: 'json',
                beforeSend: function () {
                    $('#edit-lead-form-content').addClass('ht-on-loading');
                    $('#add-lead-form-content').html('');
                },
                success: function (response) {
                    $('#edit-lead-form-content').removeClass('ht-on-loading').html(response.data);
                    $('.select2').select2();
                }
            });
        });

        $(document).on('change', '#province_id,#province', function (e) {
            var id = $(this).val();
            $.ajax({
                url: '<?php echo e(url('get-sub-location')); ?>',
                type: 'get',
                data: {
                    'id': $(this).val(),
                    'type': 'province',
                },
                dataType: 'html',
                success: function (response) {
                    $('#district_id').html(response);
                    $('#ward_id').html('');
                    $('#district').html(response);
                    $('#ward').html('');
                }
            });
        });

        $(document).on('change', '#district_id,#district', function (e) {
            var id = $(this).val();
            $.ajax({
                url: '<?php echo e(url('get-sub-location')); ?>',
                type: 'get',
                data: {
                    'id': $(this).val(),
                    'type': 'district'
                },
                dataType: 'html',
                success: function (response) {
                    $('#ward_id').html(response);
                    $('#ward').html(response);
                }
            });
        });


        $(document).on('click', '.btn-delete', function (e) {
            e.preventDefault();
            var id = $(this).attr('data-id');

            bootbox.confirm({
                message: "Bạn có chắc chắn muốn xóa",
                buttons: {
                    confirm: {
                        label: 'Có',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: 'Không',
                        className: 'btn-danger'
                    }
                },


                callback: function (result) {
                    if (result == true) {

                        $.ajax({
                            url: '<?php echo e(url('admin/lead/delete')); ?>',
                            type: 'post',
                            data: {
                                id: id,
                            },
                            dataType: 'json',
                            success: function (response) {
                                if (response.status == 1) {
                                    swal('Thành công', '', 'success');
                                } else if (response.status == 2) {
                                    swal('Thông báo', response.message, 'info');
                                } else {
                                    swal('Thông báo', response.message, 'warning');
                                }
                                table.ajax.reload();
                            }
                        });

                    }
                }
            });
        });

        $('#btn-save-lead-form').click(function (e) {
            e.preventDefault();

            var formStatus = $('#form-add-lead-item').validate({
                errorElement: "span",
                errorClass: "help-block help-block-error",

                highlight: function (e) {
                    $(e).closest(".form-group").addClass("has-error")
                },
                unhighlight: function (e) {
                    $(e).closest(".form-group").removeClass("has-error")
                },
                success: function (e) {
                    e.closest(".form-group").removeClass("has-error")
                },
                rules: {
                    'name': "required",
                    "phone": "required",
                    "source_link": "required"
                },
                messages: {
                    'name': "Không được bỏ trống tên",
                    "phone": "Không được bỏ trống SĐT",
                    "source_link": "Không được bỏ trống link nguồn"
                }
            }).form();

            if (formStatus) {
                let data = $('#form-add-lead-item').serialize();
                $.ajax({
                    url: '<?php echo e(url('admin/lead/create')); ?>',
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    beforeSend: function () {
                        $('#add-lead-form-content').addClass('ht-on-loading');
                    },
                    success: function (response) {
                        $('#add-lead-form-content').removeClass('ht-on-loading');
                        $('#add-lead-form').modal('hide');
                        if (response.status == 1) {
                            swal('Thành công', response.message, 'success');

                        } else if (response.status == 0) {
                            swal('Thông báo', response.message, 'warning');
                        } else if (response.status == 2) {
                            swal('Thông báo', response.message, 'info');
                        }
                        table.ajax.reload();
                    }
                });
            }
        });

        $('#btn-save-edit-lead-form').click(function (e) {
            e.preventDefault();

            var formStatus = $('#form-edit-lead-item').validate({
                errorElement: "span",
                errorClass: "help-block help-block-error",

                highlight: function (e) {
                    $(e).closest(".form-group").addClass("has-error")
                },
                unhighlight: function (e) {
                    $(e).closest(".form-group").removeClass("has-error")
                },
                success: function (e) {
                    e.closest(".form-group").removeClass("has-error")
                },
                rules: {
                    'name': "required",
                    "phone": "required",
                    "source_link": "required"
                },
                messages: {
                    'name': "Không được bỏ trống tên",
                    "phone": "Không được bỏ trống SĐT",
                    "source_link": "Không được bỏ trống link nguồn"
                }
            }).form();

            if (formStatus) {
                let data = $('#form-edit-lead-item').serialize();
                $.ajax({
                    url: '<?php echo e(url('admin/lead/edit')); ?>',
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    beforeSend: function () {
                        $('#edit-lead-form-content').addClass('ht-on-loading');
                    },
                    success: function (response) {
                        $('#edit-lead-form-content').removeClass('ht-on-loading');
                        $('#edit-lead').modal('hide');
                        if (response.status == 1) {
                            swal('Thành công', response.message, 'success');

                        } else if (response.status == 0) {
                            swal('Thông báo', response.message, 'warning');
                        } else if (response.status == 2) {
                            swal('Thông báo', response.message, 'info');
                        }
                        table.ajax.reload();
                    }
                });
            }
        });

        $('#btn-save-lead').click(function (e) {
            e.preventDefault();

            var formStatus = $('#form-add-lead').validate({
                errorElement: "span",
                errorClass: "help-block help-block-error",

                highlight: function (e) {
                    $(e).closest(".form-group").addClass("has-error")
                },
                unhighlight: function (e) {
                    $(e).closest(".form-group").removeClass("has-error")
                },
                success: function (e) {
                    e.closest(".form-group").removeClass("has-error")
                },
                rules: {
                    'file': "required",
                },
                messages: {
                    'file': "Không được bỏ trống file",
                }
            }).form();

            if (formStatus) {
                let data = new FormData($('#form-add-lead')[0]);
                $.ajax({
                    url: '<?php echo e(url('admin/lead/excel')); ?>',
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    processData: false,
                    contentType: false,
                    beforeSend: function () {
                        $('#add-lead-content').addClass('ht-on-loading');
                    },
                    success: function (response) {
                        $('#add-lead-content').removeClass('ht-on-loading');
                        $('#add-lead').modal('hide');
                        if (response.status == 1) {
                            swal('Thành công', response.message, 'success');

                        } else if (response.status == 0) {
                            swal('Thông báo', response.message, 'warning');
                        } else if (response.status == 2) {
                            swal('Thông báo', response.message, 'info');
                        }
                        table.ajax.reload();
                    }
                });
            }
        });

    </script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('admin', \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>