@extends('layouts.app')

@section('title')
    All Pending Statements
@stop

@section('breadcrumb')
    <ol class="breadcrumb pull-right">
        <li><a href="{{ route('home') }}"><i class="fa fa-dashboard"></i> Home</a></li>

        <li class="active">All Pending Statements</li>
    </ol>
@stop

@section('css')

@stop

@section('content')

   

  

    <div class="container-fluid">
        @can('isSuperAdmin')
            @include('partial.multiproject_select')
        @endcan
        <div id='updatecontent'>
            <div class="row" style="margin-top: 25px;">
                <div class="col-md-12">
                    <div class="panel panel-info">
                        <div class="panel-heading">Search Statements</div>
                        <div class="panel-wrapper collapse in" aria-expanded="true">
                            <div class="panel-body">
                                <form>
                                    <div class="form-body">
                                        <div class="row">
                                            <!--/span-->

                                            <!--/span-->
                                            <!--/span-->
                                            <div class="col-md-3">
                                                <div class="form-group has-success" style="margin-bottom: 15px">
                                                    <label class="control-label">By Unit No.</label>

                                                    <select id="unit" class="form-control chosen-select">
                                                        <option value="0">None</option>
                                                        @foreach ($unitNumbers as $p)
                                                            <option value="{{ $p->id }}">{{ $p->unit_number }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>
                                            <!--/span-->
                                            <!--/span-->
                                            <div class="col-md-3">
                                                <div class="form-group has-success" style="margin-bottom: 15px">
                                                    <label class="control-label">By Customer</label>

                                                    <select id="customer" class="form-control chosen-select">
                                                        <option value="0">None</option>
                                                        @foreach ($customers as $c)
                                                            <option value="{{ $c->id }}">{{ $c->name }}</option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>
                                            <!--/span-->

                                            <!--/span-->
                                            <div class="col-md-3">
                                                <div class="form-group has-success" style="margin-bottom: 15px">
                                                    <label class="control-label">By Dealer</label>

                                                    <select id="dealer" class="form-control chosen-select">
                                                        <option value="0">None</option>
                                                        @foreach ($dealers as $d)
                                                            <option value="{{ $d->id }}">
                                                                {{ $d->name }}-{{ $d->code }}</option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>
                                            <!--/span-->

                                            <!--/span-->
                                            <div class="col-md-3">
                                                <div class="form-group has-success" style="margin-bottom: 15px">
                                                    <label class="control-label">By status</label>
                                                    <select id="status" class="form-control chosen-select">
                                                        <option value="0">None</option>
                                                        <option value="{{ \App\CustomerProperty::pending }}">Pending
                                                        </option>
                                                        <option value="{{ \App\CustomerProperty::cancel }}">Cancelled
                                                        </option>

                                                    </select>
                                                </div>
                                            </div>
                                            <!--/span-->
                                            <div class="col-md-3">
                                                <div class="form-actions">
                                                    <button type="button" class="btn btn-success search-customer"> <i
                                                            class="fa fa-check"></i> Search</button>

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


                                    </div>

                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /row -->
            <div class="row" id="update-table">
                <div class="col-sm-12">
                    <div class="white-box">

                        <h3 class="box-title">Pending Statements ({{ $statementCount }})</h3>

                        <div class="table-responsive">
                            <table id="myTable" class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Project</th>
                                        <th>Unit #</th>
                                        <th>Size</th>
                                        <th>Member Name</th>
                                        <th>Net Amount</th>

                                        <th>Received Amount</th>
                                        <th>Sold By</th>
                                        <th>Booking Date</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $count = 1; ?>
                                    @foreach ($allStatements as $statement)
                                        <tr>
                                            <td>{{ $count++ }}</td>
                                            <td>
                                                @if ($statement->inventory)
                                                    @if ($statement->inventory->multiProject)
                                                        {{ $statement->inventory->multiProject->name }}
                                                    @else
                                                        None
                                                    @endif
                                                @endif
                                            </td>

                                            <td>{{ $statement->inventory ? $statement->inventory->unit_number : '' }}</td>
                                            <td>
                                                @if ($statement->inventory)
                                                    @if ($statement->inventory->size)
                                                        {{ $statement->inventory->size->name }} 
                                                        @php
                                                        $size = '';
                                                        
                                                        if ( $statement->inventory->multiProject->type->id == \App\ProjectType::society) {
                                                            $size = \App\ProjectType::society_size;
                                                        } else {
                                                            $size = \App\ProjectType::high_rise_size;
                                                        }
                                                        
                                                    @endphp
    
                                                        {{ $size }}
                                                       
                                                    @endif
                                                @endif
                                            </td>
                                            <td>{{ $statement->customer ? $statement->customer->name : '' }}</td>

                                            <td>{{ number_format($statement->net_pay) }} PKR</td>


                                            <td>{{ number_format($receivedAmt[$statement->id]) }} PKR</td>

                                            <td>
                                                @if ($statement->dealer)
                                                    {{ $statement->dealer->name }}
                                                    <b>({{ $statement->dealer->dealerType->name }})</b>
                                                @else
                                                    Company
                                                @endif
                                            </td>

                                            <td>{{ date('d-m-Y', strtotime($statement->booking_date)) }}</td>



                                            @if ($statement->status == \App\CustomerProperty::pending)
                                                <td><span class="label label-primary"
                                                        style="background-color:#cd703d;">Pending</span></td>
                                            @elseif($statement->status == \App\CustomerProperty::approved)
                                                <td><span class="label label-success">Approved</span></td>
                                            @elseif($statement->status == \App\CustomerProperty::cancel)
                                                <td><span class="label label-danger">Canceled</span></td>
                                            @else
                                                <td></td>
                                            @endif



                                            <td class="text-nowrap">
                                                <a href="{{ route('statement.detail', ['id' => $statement->id]) }}"
                                                    data-toggle="tooltip" data-original-title="View"> <i
                                                        class="fa fa-eye text-inverse m-r-10"></i> </a>

                                                <a href="{{ route('statement.editPendingView', ['id' => $statement->id]) }}"
                                                    data-toggle="tooltip" data-original-title="Update Reference No."> <i
                                                        class="fa fa-sun-o text-inverse m-r-10"></i> </a>


                                                <a href="{{ route('statement.delete', ['id' => $statement->id]) }}"
                                                    data-toggle="confirmation" data-placement="left"
                                                    data-title="Are you sure to delete?"> <i
                                                        class="fa fa-close text-danger"></i> </a>
                                            </td>

                                        </tr>
                                    @endforeach

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.row -->
        </div>
    </div>
    <!-- /.container-fluid -->

@stop

@section('js')
    <script>
        $(function() {
            $('#myTable').DataTable();


            var table = $('#example').DataTable({
                "columnDefs": [{
                    "visible": false,
                    "targets": 2
                }],
                "order": [
                    [2, 'asc']
                ],
                "displayLength": 25,
                "drawCallback": function(settings) {
                    var api = this.api();
                    var rows = api.rows({
                        page: 'current'
                    }).nodes();
                    var last = null;
                    api.column(2, {
                        page: 'current'
                    }).data().each(function(group, i) {
                        if (last !== group) {
                            $(rows).eq(i).before('<tr class="group"><td colspan="5">' + group +
                                '</td></tr>');
                            last = group;
                        }
                    });
                }
            });
            // Order by the grouping
            $('#example tbody').on('click', 'tr.group', function() {
                var currentOrder = table.order()[0];
                if (currentOrder[0] === 2 && currentOrder[1] === 'asc') {
                    table.order([2, 'desc']).draw();
                } else {
                    table.order([2, 'asc']).draw();
                }
            });
        });

        function callItBack() {
            $('.search-customer').on('click', function() {
                $('.preloader').fadeIn();
                unit = $('#unit').val();
                customer = $('#customer').val();
                dealer = $('#dealer').val();
                status = $('#status').val();
                var multipro_id = "";
                if ($("#multipro").length > 0) {
                    if ($("#multipro").val() !== 'search') {
                        multipro_id = $("#multipro").val();
                    }
                    // Do something with the value
                }


                if (unit == 0 && customer == 0 && status == 0 && dealer == 0 && multipro_id == 0) {
                    toastr.warning("please select any option.");
                    return false;
                }

                var data = {
                    'multipro_id': multipro_id,
                    'unit': unit,
                    'customer': customer,
                    'dealer': dealer,
                    'status': status
                };
                $.get('/pending-statement/updateTable', data, function(data) {
                    document.getElementById('update-table').innerHTML = data;

                    $('#myTable').DataTable({
                        'pageLength': 50,
                        'paging': false,
                        'searching': false,
                        'info': true
                    });
                    toastr.success("Search updated.");


                    $('.preloader').fadeOut();
                });
            });
        }
        callItBack();


        function updateStatus(value) {
            $('.preloader').fadeIn();

            if (value === 'search') {

                $('.preloader').fadeOut();
                return false;
            }
            var data = {
                'multipro_id': value,
            };
            $.get('{{ route('statement.pending') }}', data, function(data) {
                $('.preloader').fadeOut();
                $('#myTable tbody').empty();

                $('.chosen-select').chosen('destroy');

                document.getElementById('updatecontent').innerHTML = data;
                $('.chosen-select').trigger('chosen:updated');
                callItBack();
                $('#myTable').DataTable({
                    'pageLength': 50,
                    'paging': false,
                    'searching': false,
                    'info': true,
                    'dom': 'Bfrtip',
                    'buttons': [
                        'copy', 'csv', 'excel', 'pdf', 'print'
                    ]
                });

                toastr.success("Search updated.");


                $('.preloader').fadeOut();
            });

        }
    </script>
@stop
