@extends('admin')

@section('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>

@endsection
@section('content')
    @if (session()->has('error'))
        <div class="alert alert-danger">{{ session()->get('error') }}</div>
    @endif
    @if (session()->has('success'))
        <div class="alert alert-success">{{ session()->get('success') }}</div>
    @endif
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <h1 class="page-title">
        Danh sách request tìm kiếm
    </h1>

    <div class="row">
        {{--        <div class="col-md-12">--}}
        {{--            <a class="btn blue" href="" target="_blank">Thêm gói cước</a>--}}
        {{--        </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ố điện thoại</th>
                    <th>Tỉnh tìm kiếm</th>
                    <th>Huyện tìm kiếm</th>
                    <th>Giá thấp nhất</th>
                    <th>Giá cao nhất</th>
                    <th>Loại nhà</th>
                    <th>Ghi chú</th>
                    <th>Trạng thái</th>
                    <th>Số cuộc hội thoại</th>
                    <th>Tạo lúc</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>
                        <select class="form-control">
                            <option value="">Tất cả</option>
                            @php $provinces = App\Models\Province::all();@endphp
                           @foreach($provinces as $province)
                               <option value="{{ $province->provinceid }}">{{ $province->name }}</option>
                               @endforeach
                        </select>
                    </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">
                            @php $types = App\Models\HostelType::all(); @endphp
                            <option value="">Tất cả</option>
                            @foreach($types as $type)
                            <option value="{{ $type->id }}">{{ $type->name }}</option>
                                @endforeach
                        </select>
                    </th>
                    <th><input type="text" class="form-control"></th>
                    <th>
                        <select class="form-control">
                            <option value="">Tất cả</option>
                            <option value="{{ App\Models_v2\FindSession::CANCEL }}">Đã hủy</option>
                            <option value="{{ App\Models_v2\FindSession::SEARCHING }}">Đang tìm kiếm</option>
                            <option value="{{ App\Models_v2\FindSession::FOUND }}">Đã tìm thấy</option>
                            <option value="{{ App\Models_v2\FindSession::NOT_FOUND }}">Không thấy</option>
                        </select>
                    </th>
                    <th>
                        <select class="form-control">
                            <option value="">Tất cả</option>
                            <option value="not-have">Chưa có</option>
                            <option value="have">Đã có</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 bs-modal-lg" id="edit" 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 yêu cầu tìm kiếm</h4>
                </div>

                <div class="modal-body" id="edit-request-find-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-request-find">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
    </div>


@endsection

@push('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 src="/js/cleave.min.js"></script>

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


@endpush


@push('scripts')
    <script>

        var table;

        $(function () {

            table = $('#orders-table').DataTable({
                "bDestroy": true,
                processing: true,
                searching: true,
                serverSide: true,
                aaSorting: [[10, 'desc']],
                ajax: {
                    url: '{!! url('admin/request-find/request.data') !!}',
                    data: function (d) {
                        d.user_id = '{{ request()->input('user_id') }}'
                    }
                },
                columns: [
                    {data: 'name', name: 'name'},
                    {data: 'phone', name: 'phone'},
                    {data: 'province', name: 'province'},
                    {data: 'district', name: 'district'},

                    {data: 'min_price', name: 'min_price'},
                    {data: 'max_price', name: 'max_price'},
                    {data: 'type', name: 'type'},
                    {data: 'note', name: 'note'},
                    {data: 'status', name: 'status'},
                    {data: 'conversations_count', name: 'conversations_count'},
                    {data: 'created_at', name: 'created_at'},
                    {data: 'action', name: 'action'},
                ],
            });

            table.columns().every(function (index) {

                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-edit', function (e) {
            e.preventDefault();

            var id = $(this).attr('data-id');
            $.ajax({
                url: '{{ url('admin/request-find/edit') }}',
                type: 'get',
                data: {
                    id: id
                },
                dataType: 'json',
                beforeSend: function () {
                    $('#edit-request-find-content').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#edit-request-find-content').removeClass('ht-on-loading').html(response.data);
                    $('.select2').select2();
                    var cleave = new Cleave('.price-r', {
                        numeral: true,
                        numeralThousandsGroupStyle: 'thousand',
                        numeralDecimalMark: ',',
                        delimiter: '.'
                    });

                    var cleave = new Cleave('.price-r-2', {
                        numeral: true,
                        numeralThousandsGroupStyle: 'thousand',
                        numeralDecimalMark: ',',
                        delimiter: '.'
                    });
                }
            });
        });

        $(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: '{{ url('admin/request-find/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-edit-request-find').click(function (e) {
            e.preventDefault();

            var formStatus = $('#form-edit-find-session').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: {
                    "province_id": "required",
                    "district_id": "required",
                    "min_price": "required",
                    "max_price": "required"
                },
                messages: {

                }
            }).form();

            if (formStatus) {
                let data = $('#form-edit-find-session').serialize();
                $.ajax({
                    url: '{{ url('admin/request-find/update') }}',
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    beforeSend: function () {
                        $('#form-edit-find-session').addClass('ht-on-loading');
                    },
                    success: function (response) {
                        $('#form-edit-find-session').removeClass('ht-on-loading');
                        $('#edit').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>
@endpush