@extends('layouts.app')

@section('title')
    Map Inventory
@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 Map File Inventory</li>
    </ol>
@stop
@section('css')
    <style type="text/css">
        .bg-color {
            background-color: #1c3752;
        }
    </style>
@stop
@section('content')


    <div class="container-fluid">
        <!-- /row -->
        <div class="row">
            <div class="col-sm-12">
                <div class="white-box">
                    <div class="pull-right">

                        {{-- <button class="btn  btn-primary" id='postbutton' type='button'
                            onclick='callApproveFunc({{$balloting_file->id }})'>
                            <i class="fa fa-plus-square"></i>&nbsp;Approve All
                        </button> --}}




                    </div>
                    <h3 class="box-title">File Detail</h3>
                    @permission('edit-balloting')
                    @endpermission
                    <div class="table-responsive">


                        <table id="myTable" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>
                                        Sr No</th>
                                    <th> Plot No </th>
                                    <th>Category(Area)</th>


                                    <th>Feature</th>
                                    <th>
                                        Exact/Odd</th>
                                    <th> Sector </th>
                                    <th>Road Category</th>
                                    <th>Street</th>
                                    <th>Type</th>
                                    <th>Cnic</th>
                                    <th>MemberShip No</th>

                                    <th>Inventory</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $count = 1; ?>
                                @foreach ($balloting_file->mapDetail as $file)
                                    <tr>
                                        <td>{{ $count++ }}</td>
                                        <td>{{ $file->sr_no }} </td>
                                        <td>{{ $file->plot_no }} </td>
                                        <td>{{ $file->area }} Marla </td>
                                        <td>{{ $file->feature }} </td>
                                        <td>{{ $file->exact_odd }} </td>
                                        <td>{{ $file->sector }} </td>
                                        <td>{{ $file->road_category }} </td>

                                        <td>{{ $file->street }} </td>
                                        <td>{{ $file->type }} </td>
                                        <td>{{ $file->cnic }} </td>
                                        <td>{{ $file->membershipno }} </td>
                                        @if ($file->Inventory)
                                            <td><a href="{{ route('inventory.detail', $file->Inventory->id) }}">{{ $file->Inventory->unit_number }}
                                                </a></td>
                                        @else
                                            <td>NIL</td>
                                        @endif

                                        <td>
                                            @if (!$file->Inventory)
                                                @permission('edit-balloting')
                                                    <a href="{{ route('balloting.direct.map.inventory.edit', $file->id) }}"
                                                        data-toggle="tooltip" datza-original-title="Edit"> <i
                                                            class="fa fa-pencil text-inverse m-r-10"></i>&nbsp;&nbsp;</a>
                                                @endpermission
                                            @endif
                                            @if ($file->Inventory)
                                                @if ($file->balloting_status == \App\BallotingMapDetail::balloting_pending)
                                                    @permission('edit-balloting')
                                                        <a href="{{ route('balloting.direct.approve', $file->id) }}"
                                                            data-toggle="tooltip" datza-original-title="approve"> <i
                                                                class="fa fa-check-circle text-inverse m-r-10"></i>&nbsp;&nbsp;</a>
                                                    @endpermission

                                                    @permission('delete-balloting')
                                                        <button class="btn  btn-primary" id='postbutton'
                                                            onclick='callFunc({{ $file->id }})' type='button'>
                                                            <i class="fa fa-trash-o"></i>&nbsp;Remove
                                                        </button>
                                                    @endpermission
                                                @else
                                                    <span class='label label-success'>Approved</span>
                                                @endif
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach

                            </tbody>

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

@stop

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

            //     "displayLength": 25000,
            // });

            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();
                }
            });
        });
        $('#example23').DataTable({
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        });


        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        // $("#postbutton").click(function() {
        function callFunc(val) {
            Swal.fire({
                    title: "Are you sure?",
                    text: "you want to remove inventory from map !",
                    icon: "warning",
                    buttons: true,
                    dangerMode: true,
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, Sure!'
                })
                .then((result) => {
                    if (result.isConfirmed) {
                        $('.preloader').fadeIn();
                        $.ajax({
                            /* the route pointing to the post function */
                            url: '{{ route('balloting.direct.map.inventory.remove') }}',
                            type: 'POST',
                            /* send the csrf-token and the input to the controller */
                            data: {
                                _token: CSRF_TOKEN,
                                id: val
                            },
                            dataType: 'JSON',
                            /* remind that 'data' is the response of the AjaxController */
                            success: function(data) {
                                $('.preloader').fadeOut();
                                if (data.success) {
                                    Swal.fire({
                                            icon: 'success',
                                            title: 'Your Data has been removed',
                                            confirmButtonText: 'ok!'
                                        })
                                        .then((result) => {
                                            if (result.isConfirmed) {
                                                location.reload();

                                            }
                                        })

                                }



                            },
                            error: function(xhr, status, error) {
                                $('.preloader').fadeOut();
                                Swal.fire({
                                    icon: 'error',
                                    title: 'Oops...',
                                    text: 'Something went wrong!',

                                })
                            }

                        });
                    } else {
                        $('.preloader').fadeOut();
                        Swal.fire("Your Data is safe!");
                    }
                });

        }

        function callApproveFunc(val) {
            Swal.fire({
                    title: "Are you sure?",
                    text: "you want to remove approve map inventory !",
                    icon: "warning",
                    buttons: true,
                    dangerMode: true,
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, Sure!'
                })
                .then((result) => {
                    if (result.isConfirmed) {
                        $('.preloader').fadeIn();
                        $.ajax({
                            /* the route pointing to the post function */
                            url: '{{ route('balloting.direct.approve.all') }}',
                            type: 'POST',
                            /* send the csrf-token and the input to the controller */
                            data: {
                                _token: CSRF_TOKEN,
                                id: val
                            },
                            dataType: 'JSON',
                            /* remind that 'data' is the response of the AjaxController */
                            success: function(data) {
                                $('.preloader').fadeOut();
                                if (data.success) {
                                    Swal.fire({
                                            icon: 'success',
                                            title: 'Balloting Approved Successfully',
                                            confirmButtonText: 'ok!'
                                        })
                                        .then((result) => {
                                            if (result.isConfirmed) {
                                                location.reload();

                                            }
                                        })

                                }



                            },
                            error: function(xhr, status, error) {
                                $('.preloader').fadeOut();
                                Swal.fire({
                                    icon: 'error',
                                    title: 'Oops...',
                                    text: 'Something went wrong!',

                                })
                            }

                        });
                    } else {
                        $('.preloader').fadeOut();
                        Swal.fire("Your Data is safe!");
                    }
                });

        }

        // });
    </script>
@stop
