@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"/>
    <link href="/assets/global/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/select2/css/select2-bootstrap.min.css" rel="stylesheet" type="text/css"/>

@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
    <h3 class="inline">Quản lý danh sách chi phí</h3>


    <div class="row">
        <div class="col-md-12" style="margin-bottom: 12px; margin-top: 12px">
            <a href="#add-fee" data-toggle="modal" class="btn btn-success">Thêm</a>
        </div>
        <div class="col-md-12">
            <table class="table table-striped table-bordered table-hover" id="orders-table">
                <thead>
                <tr>
                    {{--<th>Id</th>--}}
                    <th>Tên</th>
                    <th>Giá trị</th>
                    <th>Nhà</th>
                    <th>Phòng</th>
                    <th>Hành động</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>

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

    <div class="modal fade" id="add-fee"  role="dialog" 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">Thêm loại chi phí</h4>
                </div>
                <div class="modal-body">

                    <form id="form-fee">

                        <div class="form-group">
                            <label>Tên</label>
                            <div>
                                <input type="text" name="name" class="form-control"
                                       id="name" value="{{ old('name') }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Giá trị</label>
                            <div>
                                <input type="text" name="fee" class="form-control"
                                       id="fee" value="{{ old('fee') }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Nhà trọ</label>
                            <div>
                                <select class="form-control select2" name="hostel_id" id="hostel_id">
                                    <option>Vui lòng chọn giá trị</option>
                                    @foreach($hostels as $hostel)
                                        <option value="{{ $hostel->id }}">{{ $hostel->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Phòng trọ</label>
                            <div>
                                <select class="form-control select2" name="room_id" id="room_id" multiple>

                                </select>
                            </div>
                        </div>

                    </form>

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

    <div class="modal fade" id="edit-fee"  role="dialog" 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">Cập nhật chi phí</h4>
                </div>
                <div class="modal-body" id="body-edit">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn dark btn-outline" data-dismiss="modal">Đóng</button>
                    <button type="button" class="btn green" id="save-fee-edit">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
@endsection

@push('scripts')

<script src="/assets/global/plugins/bootstrap-wysihtml5/wysihtml5-0.3.0.js" type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.js" type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"
        type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"
        type="text/javascript"></script>
<script src="/assets/global/plugins/moment.min.js" type="text/javascript"></script>
<script src="/assets/global/plugins/jquery.mockjax.js" type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-editable/bootstrap-editable/js/bootstrap-editable.js"
        type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-editable/inputs-ext/address/address.js" type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-editable/inputs-ext/wysihtml5/wysihtml5.js"
        type="text/javascript"></script>
<script src="/assets/global/plugins/bootstrap-typeahead/bootstrap3-typeahead.min.js" type="text/javascript"></script>
<script src="/assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
{{--<script src="/assets/pages/scripts/form-editable.min.js" type="text/javascript"></script>--}}

@endpush


@push('scripts')
<script>

    $(document).on('change', '#hostel_id', function () {

        var hostel_id = $(this).val();

        $.ajax({

            url: '{{ url('admin/hostel/get-rooms-by-hostel') }}',
            type: 'get',
            data: {
                hostel_id: hostel_id,
            },
            success: function (response) {
                $('#room_id').html(response.html).select2();
            }

        });

    });

    $(document).on('change', '#hostel_id_2', function () {

        var hostel_id = $(this).val();

        $.ajax({

            url: '{{ url('admin/hostel/get-rooms-by-hostel') }}',
            type: 'get',
            data: {
                hostel_id: hostel_id,
            },
            success: function (response) {
                $('#room_id_2').html(response.html).select2();
            }

        });

    });

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

        $.ajax({

            url: '{{ url('admin/setting/fee/detail') }}' + '/' + id,
            type: 'get',
            success: function (response) {
                $('#body-edit').html(response.html);
                $('.select2').select2();
            }

        });
    });

    var table;

    function getPostByAttr() {
        var startTime = $('#start_time').val();
        var endTime = $('#end_time').val();
        var timeRange = $('#time_range').val();


        $('#orders-table').DataTable({
            "bDestroy": true,
            processing: true,
            //  serverSide: true,
            "aaSorting": [],
            searching: true,
            ajax: {
                url: '{!! url('admin/setting/fee/fee.data') !!}',
                data: {
                    start_time: startTime,
                    end_time: endTime,
                    time_range: timeRange,
                }
            },
            columns: [
//                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'fee', name: 'fee'},
                {data: 'hostel', name: 'hostel'},
                {data: 'room', name: 'room'},
                {data: 'action', name: 'action'},
            ]
        });
    }

    $(function () {
        table = $('#orders-table').DataTable({
            "bDestroy": true,
            processing: true,
            searching: true,
            "aaSorting": [],
            serverSide: true,
            ajax: {
                url: '{!! url('admin/setting/fee/fee.data') !!}',
            },
            columns: [

                {data: 'name', name: 'name'},
                {data: 'fee', name: 'fee'},
                {data: 'hostel', name: 'hostel'},
                {data: 'room', name: 'room'},
                {data: 'action', name: 'action'},
            ],
            initComplete: function () {
                $('.editable').editable({
                    prepend: "Chưa chọn",
                    source: [{
                        value: 1,
                        text: 'Có'
                    }, {
                        value: 0,
                        text: 'Không'
                    }],
                });
            }
        });
    });

    $(document).on('click', '#save-fee', function (e) {
        var data = $('#form-fee').serialize();
        $.ajax({

            url: '{{url('admin/setting/fee/create')}}',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function (response) {
                if (response.status == 1) {
                    swal('Thành công', '', 'success');
                    $('#add-fee').modal('hide');
                    table.draw();
                } else {
                    swal('Có lỗi xảy ra vui lòng thử lại sau', response.message, 'success');
                }
            }
        });
    });

    $(document).on('click', '#save-fee-edit', function (e) {
        var data = $('#form-fee-edit').serialize();
        $.ajax({

            url: '{{url('admin/setting/fee/edit')}}',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function (response) {
                if (response.status == 1) {
                    swal('Thành công', '', 'success');
                    $('#edit-fee').modal('hide');
                    table.draw();
                } else {
                    swal('Có lỗi xảy ra vui lòng thử lại sau', response.message, 'success');
                }
            }
        });
    });

</script>
@endpush