@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 vị trí địa lý
    </h1>
    <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>Kích hoạt</th>
                    <th>Hành động</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th><input type="text" class="form-control"></th>
                    <th></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" 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</h4>
                </div>
                <div class="modal-body" id="edit-content">


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn dark btn-outline" data-dismiss="modal" id="dismiss-modal">Hủy
                    </button>
                    <button type="button" class="btn green" id="btn-save">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </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>
        $.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 getPostByAttr() {
            table.ajax.reload();
        }

        $(document).on('switchChange.bootstrapSwitch', '.btn-is-search', function (e) {
            var id = $(this).attr('data-id');
            $.ajax({
                url: '{{ url('admin/location/update-is-search') }}',
                type: 'post',
                data: {
                    id: id,
                    type: $(this).attr('data-type')
                },
                dataType: 'json',
                success: function (response) {

                }
            });
        });

        $(function () {

            table = $('#orders-table').DataTable({
                "bDestroy": true,
                processing: true,
                searching: true,
                serverSide: true,
                aaSorting: [],
                ajax: {
                    url: '{!! url('admin/location/location.data') !!}',
                    data: function (d) {
                        d.parent_id = '{{ request()->input('parent_id') }}';
                        d.parent_type = '{{ request()->input('parent_type') }}';
                    }
                },
                columns: [
                    {data: 'name', name: 'name'},
                    {data: 'is_search', name: 'is_search'},
                    {data: 'action', name: 'action', 'orderable': false, searchable: false},
                ],
                drawCallback: function () {
                    $(".make-switch").bootstrapSwitch();

                }
            });

            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');
            var type = $(this).attr('data-type');
            $.ajax({
                url: '{{ url('admin/location/edit') }}',
                type: 'get',
                data: {
                    id: id,
                    type: type
                },
                beforeSend: function () {
                    $('#edit-content').addClass('ht-on-loading');
                },
                dataType: 'json',
                success: function (response) {
                    $('#edit-content').removeClass('ht-on-loading').html(response.data);
                }
            });
        });

        $('#btn-save').click(function (e) {
            e.preventDefault();
            let data = new FormData($('#form-edit-location')[0]);
            $.ajax({
                url: '{{ url('admin/location/update') }}',
                type: 'post',
                data: data,
                dataType: 'json',
                processData: false,
                contentType: false,
                beforeSend: function () {
                    $('#edit-content').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#edit-content').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