@extends('admin2')

@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/typeahead/typeahead.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-select/css/bootstrap-select.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-multiselect/css/bootstrap-multiselect.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">
        Thống kê kinh doanh
    </h1>

    <div class="row"
         style="margin-right: 0px; margin-left: 0px; padding-top: 15px; margin-bottom: 15px; padding-bottom: 15px; border-bottom: 1px solid #e7ecf1; background-color: #EFF2F5">
        <div class="col-md-9">
            <div class="row">


                <div class="col-md-2">
                    <label class="control-label">Chọn nhà</label>
                    <select class="form-control" id="hostels">
                        <option value="">Tất cả nhà</option>
                        @php
                            $ownerId = auth('backend')->user()->id;

                             if(auth('backend')->user()->type == \App\User::STAFF)
	    {
		   $ownerId = auth('backend')->user()->staff_owner_id;
	    }
                                $hostels = \App\Models\Hostel::query()->where('owner_id', $ownerId)->get()->sortBy(function($item) {
                            return $item->name;
                            });

                        @endphp
                        @foreach($hostels as $hostel)
                            <option
                                    value="{{ $hostel->id }}">{{ $hostel->name }}</option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-2">
                    <label class="control-label">Chọn phòng</label>
                    <select class="form-control select2" id="select-rooms">
                        <option value=" " selected>Tất cả phòng</option>
                        @if(!empty($hostels))

                            @php $hostel = $hostels->first();
                            if($hostel){
                            $hostelId = $hostel->id;
                            $rooms = \App\Models\Room::query()->where('hostel_id', $hostelId)->get()->sortBy(function($item) {
                            return $item->name;
                            });
                            }
                            @endphp

                            @if(!empty($rooms))
                                @foreach($rooms as $room)
                                    <option value="{{ $room->id }}"> {{ $room->name }}</option>
                                @endforeach
                            @endif


                        @endif
                    </select>
                </div>

                <div class="col-md-2">
                    <label class="control-label">Bắt đầu</label>
                    <input class="form-control monthpicker" type="text" name="start_date" id="start_date"
                           autocomplete="off">
                </div>


                <div class="col-md-2">
                    <label class="control-label">Kết thúc</label>
                    <input class="form-control monthpicker" type="text" name="end_date" id="end_date"
                           autocomplete="off">
                </div>
                <div class="col-md-2">
                    <label class="control-label">Loại phiếu thu</label>

                    <select class="form-control" id="type">
                        <option value="" selected>Tất cả</option>
                        <option value="{{ \App\Models\MoneyInfo::VOUCHER_CONTRACT }}">Tiền nhà
                        </option>
                        <option value="{{ \App\Models\MoneyInfo::VOUCHER_SERVICE }}">Tiền dịch vụ
                        </option>

                    </select>


                </div>
                <div class="col-md-2">
                    <label class="control-label">Loại phiếu chi</label>
                    <select class="bs-select form-control"  data-deselect-all-text="Bỏ chọn tất cả" data-select-all-text="Chọn tất cả"  data-actions-box="true" multiple="multiple" id="type_spend" name="type_spend">
                        @foreach($typeSpends as $typeSpend)
                            <option value="{{ $typeSpend->id }}" selected>{{ $typeSpend->name }}</option>
                        @endforeach
                    </select>
                </div>

            </div>
        </div>
        <div class="col-md-3">

            <div class="row">
                <div class="col-md-12" id="hostels-button">

                    @if(auth('backend')->user()->can('add-collect'))
                        <div class="col-md-12" style="margin-bottom: 12px; padding-top: 12px">
                            <a style="margin-left: 10px" class="btn green-meadow pull-right btn-export-excel"
                               href="#export-excel" data-toggle="modal" data-type="income">
                                <i class="fa fa-file-excel"></i> Xuất Excel</a>

                        </div>
                    @endif

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


    </div>

    <div class="modal fade bs-modal-lg" id="export-excel" 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" id="voucher-title">Xuất dữ liệu ra Excel</h4>
                </div>
                <div class="modal-body">
                    <form role="form" id="form-export">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="is_export" id="type_export" value="1">
                        <div class="form-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Chọn nhà <span class="required"
                                                                  aria-required="true"> (*) </span></label>
                                        <select class="form-control" name="hostel_id" id="hostel_id_2">
                                            @php
                                                $hostels = \App\Models\Hostel::where('owner_id', auth('backend')->user()->id)->get();

                                            @endphp

                                            <option value="">Vui lòng chọn</option>
                                            @foreach($hostels as $hostel)
                                                <option value="{{ $hostel->id }}"> {{ $hostel->name }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Chọn phòng</label>
                                        <select class="form-control" name="room_id" id="room_id_2">

                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row" style="margin-top: 15px">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Chọn ngày bắt đầu</label>
                                        <input class="form-control monthpicker" name="start_date">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Chọn ngày kết thúc</label>
                                        <input class="form-control monthpicker" name="end_date">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>

                </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-export">Xuất</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <div class="row" id="stat">

    </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>STT</th>
                    <th>Mã phiếu thu / chi</th>
                    <th>Số tiền</th>
                    <th>Loại thu chi</th>
                    <th>Ghi chú</th>
                    <th>Nhà</th>
                    <th>Phòng</th>
                    <th>Hợp đồng</th>
                    <th>Thời gian</th>
                    {{--<th style="width: 10%;">Thao tác</th>--}}
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th></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>
                </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="detail-money-info" 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" id="detail-money-info-title">Thông tin hóa đơn</h4>
                </div>
                <div class="modal-body" id="detail-money-info-content">


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn dark btn-outline" data-dismiss="modal" id="dismiss-modal">Hủy
                    </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 src="/assets/global/plugins/typeahead/handlebars.min.js" type="text/javascript"></script>
    <script src="/assets/global/plugins/typeahead/typeahead.bundle.min.js" type="text/javascript"></script>
    <script src="/assets/global/plugins/bootstrap-select/js/bootstrap-select.min.js" type="text/javascript"></script>
    <script src="/assets/pages/scripts/components-bootstrap-select.min.js" type="text/javascript"></script>
    <script src="/assets/global/plugins/bootstrap-multiselect/js/bootstrap-multiselect.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 type="text/javascript">--}}
    {{--var file1 = $('.add-image-hostel').val();--}}

    {{--$('#file-upload').on('change.bs.fileinput', function(e, file) {--}}
    {{--//console.log(e)--}}
    {{--$('#file1').val(file1)--}}
    {{--});--}}


    {{--$('#file-upload').on('clear.bs.fileinput', function(e, file) {--}}
    {{--//console.log(e)--}}
    {{--$('#file1').val(file1)--}}
    {{--});--}}
    {{--</script>--}}

    <script>


        //    $(document).on('change', '.add-image-hostel', function(e) {
        //        alert('123');
        //       $('.fileinput-exists').css({
        //           'width' : '48%'
        //       });
        //
        //      $('.btn-file').css({
        //          'width' : '100%'
        //      });
        //
        //
        //    });

        var table = null;
        $('#select-rooms').change(function () {
            getPostByAttr();
        });

        $('#hostels').change(function (e) {
            getPostByAttr();
            $.ajax({
                url: '{{ url('admin2/hostel/select-rooms') }}',
                type: 'get',
                data: {
                    hostel_id: $(this).val()
                },
                dataType: 'json',
                success: function (response) {
                    $('#select-rooms').html(response.data);
                }
            })
        });

        //    $('#start_date').change(function (e) {
        //        getPostByAttr();
        //    });

        function getDt() {

        }

        $('.monthpicker').datepicker({
            format: 'mm/yyyy',
            autoclose: true,
            viewMode: "months",
            minViewMode: "months",
        }).on('changeDate', function (e) {

            //   get_stat();
            getPostByAttr();
        });

        $('#type').change(function () {
            //  get_stat();
            getPostByAttr();
        });
        $('#type_spend').change(function () {
            // get_stat();
            getPostByAttr();
        });

        $('#btn-export').click(function (e) {
            var data = $('#form-export').serialize();
            window.location = '{!! url('admin2/money/stat2.data') !!}' + '?' + data;
        });

        function get_stat() {
            $.ajax({
                url: '{!! url('admin2/money/stat2.data') !!}',
                data: {
                    hostel_id: $('#hostels').val(),
                    start_date: $('#start_date').val(),
                    end_date: $('#end_date').val(),
                    room_id: $('#select-rooms').val(),
                    type: $('#type').val(),
                    type_spend: $('#type_spend').val(),
                    is_stat: 1,
                },
                beforeSend: function () {
                    $('#stat').addClass('ht-on-loading');
                },
                dataType: 'json',
                success: function (response) {
                    $('#stat').removeClass('ht-on-loading').html(response.data);
                }
            });
        }

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

        $(function () {
            get_stat();

//        $('#orders-table tfoot th').each( function (index) {
//            if(index != 0 && index != 9) {
//                $(this).html('<input type="text" class="form-control" />');
//            }
//        } );co

            // <th>STT</th>
            //     <th>Mã phiếu thu / chi</th>
            //     <th>Số tiền</th>
            //     <th>Loại thu chi</th>
            //     <th>Nhà</th>
            //     <th>Phòng</th>
            //     <th>Hợp đồng</th>
            //     <th>Thời gian</th>
            //
            table = $('#orders-table').DataTable({
                "bDestroy": true,
                processing: true,
                searching: true,
                "aaSorting": [],
                serverSide: true,
                ajax: {
                    url: '{!! url('admin2/money/stat2.data') !!}',
                    data: function (d) {
                        d.hostel_id = $('#hostels').val();
                        d.start_date = $('#start_date').val();
                        d.end_date = $('#end_date').val();
                        d.room_id = $('#select-rooms').val();
                        d.type = $('#type').val();
                        d.type_spend  = $('#type_spend').val();
                    }
                },
                columns: [

                    {data: 'DT_Row_Index', name: 'DT_Row_Index', orderable: false, searchable: false},
                    {data: 'collect_spend_id', name: 'collect_spend_id'},
                    {data: 'amount', name: 'amount'},
                    {data: 'type', name: 'type'},
                    {data: 'note', name: 'note'},
                    {data: 'hostel_id', name: 'hostel_id'},
                    {data: 'room_id', name: 'room_id'},
                    {data: 'contract_id', name: 'contract_id'},
                    {data: 'date', name: 'date'},
                    // {data: 'action', name: 'action', 'orderable' : false, searchable: false},
                ],
            });


            table.columns().every(function (index) {
                if (index !== 0) {
                    var that = this;
                    $('input', this.footer()).on('keyup change', 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', '.detail-money', function () {
            var money_info_id = $(this).attr('data-id');
            $.ajax({
                url: '{{ url('admin2/money/detail-money-info') }}',
                type: 'get',
                data: {
                    money_info_id: money_info_id
                },
                dataType: 'json',
                beforeSend: function () {
                    $('#detail-money-info-content').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#detail-money-info-content').removeClass('ht-on-loading').html(response.data);
                    $('#detail-money-info-title').text(response.title);
                }
            });

        });


        $(document).on('change', '#hostel_id', function (e) {
            var id = $(this).val();
            var type = $(this).attr('data-type');
            $.ajax({
                url: '{{ url('get-sub-hostel') }}',
                type: 'get',
                data: {
                    'id': $(this).val(),
                    'type': type
                },
                dataType: 'html',
                success: function (response) {
                    $('#room_id').html(response);
                    $('#room_id').select2();

                }
            });
        });

        $('.datepicker').datepicker({
            format: 'dd/mm/yyyy',
            language: 'vi',
            autoclose: true,
        });

        $(document).on('change', '#hostel_id_2', function (e) {
            var id = $(this).val();
            $.ajax({
                url: '{{ url('get-sub-hostel') }}',
                type: 'get',
                data: {
                    'id': $(this).val(),
                },
                dataType: 'html',
                success: function (response) {
                    $('#room_id_2').html(response);
                    $('#room_id_2').select2();

                }
            });
        });


        //$('.bs-select').selectpicker();
    </script>

@endpush