@extends('backend.app')
@section('title',' Daily Amimal Report')
@push('styles')
    <style>
        .accordion-button:not(.collapsed) {
            color: var(--bs-accordion-active-color);
            background-color: #fff !important;
            box-shadow: none;
        }



        .accordion-item label {
            font-size: 14px !important;
        }

        .disease-select, .disease-select option {
            font-size: 14px;
        }

        .accordion-button {
            padding: 5px !important;

        }

        .accordion-body {
            padding: 5px !important;
        }

        li {
            list-style-type: none;
        }

        .cm_grid {
            display: flex;
        }

        .full_report {
            position: relative;
        }

        .print-button {
            position: absolute;
            top: 8vh;
            right: 16px;
        }

        #printsection strong {
            color: #6E7796;
            font-size: 14px;
            font-style: normal;
            font-weight: 600;
            line-height: normal;
        }

        #printsection span {
            color: #0050C9;
            font-size: 14px;
            font-style: normal;
            font-weight: 400;
            line-height: normal;
        }

        .report-status {
            color: #000;
            font-size: 14px;
            font-style: normal;
            font-weight: 700;
            margin: 0;
            line-height: 20px; /* 142.857% */
        }

        .filemanager-sidebar {
            width: 250px;
        }

        table th, table td {
            color: #1C274D;
            font-size: 14px;
            font-style: normal;
            font-weight: 600;
            line-height: 24px; /* 171.429% */
        }
    </style>
@endpush

@section('content')
    <div class="row mb-4">
        <div class="col-12">
            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                <h4 class="mb-sm-0 font-size-18">Daily Animal Report</h4>
            </div>
        </div>
    </div>
    <div class="d-xl-flex">
        <div class="w-100">
            <div class="d-md-flex">
                <div class="card filemanager-sidebar me-md-2">
                    <div class="card-body table_card">
                        <div class="d-flex flex-column h-100">
                            <div class="mb-4">
                                <form action="{{ route('daily-report') }}" method="GET" role="form" id="form1"
                                      enctype="multipart/form-data" autocomplete="off">
                                    <div class="accordion accordion-flush" id="accordionPanelsStayOpenExample">
                                        <div class="accordion-item p-2">
                                            <h2 class="accordion-header">
                                                <button class="accordion-button" type="button" data-bs-toggle="collapse"
                                                        data-bs-target="#panelsStayOpen-collapseOne"
                                                        aria-expanded="true" aria-controls="panelsStayOpen-collapseOne">
                                                    Organizations
                                                </button>
                                            </h2>
                                            <div id="panelsStayOpen-collapseOne"
                                                 class="accordion-collapse collapse show">
                                                <div class="accordion-body">
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="checkbox"
                                                               id="organizationCheckAll">
                                                        <label class="form-check-label" for="organizationCheckAll"> All</label>
                                                    </div>
                                                    @foreach ($organizations as $organization)
                                                        <div class="form-check">
                                                            <input class="form-check-input organization-list" type="checkbox"
                                                                   name="organizationCheck[]" id="organizationCheck{{$organization->id}}"
                                                                   value="{{$organization->id}}" onChange="getHerd();">
                                                            <label class="form-check-label"
                                                                   for="organizationCheck{{$organization->id}}"> {{$organization->name}}</label>
                                                        </div>
                                                    @endforeach
                                                </div>
                                            </div>
                                        </div>
                                        <div class="accordion-item p-2 ">
                                            <h2 class="accordion-header">
                                                <button class="accordion-button " type="button"
                                                        data-bs-toggle="collapse"
                                                        data-bs-target="#panelsStayOpen-collapseTwo"
                                                        aria-expanded="false"
                                                        aria-controls="panelsStayOpen-collapseTwo">
                                                    Herds
                                                </button>
                                            </h2>
                                            <div id="panelsStayOpen-collapseTwo"
                                                 class="accordion-collapse collapse show">
                                                <div class="accordion-body" id="herd">
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="checkbox"
                                                               id="herdCheckAll">
                                                        <label class="form-check-label" for="herdCheckAll"> All</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="accordion-item p-2">
                                            <h2 class="accordion-header">
                                                <button class="accordion-button " type="button"
                                                        data-bs-toggle="collapse"
                                                        data-bs-target="#panelsStayOpen-collapseThree"
                                                        aria-expanded="false"
                                                        aria-controls="panelsStayOpen-collapseThree">
                                                    Lots
                                                </button>
                                            </h2>
                                            <div id="panelsStayOpen-collapseThree"
                                                 class="accordion-collapse collapse show">
                                                <div class="accordion-body" id="lot">
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="checkbox"
                                                               id="lotCheckAll">
                                                        <label class="form-check-label" for="lotCheckAll"> All</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="mt-3 p-2">
                                        <button type="submit" class="btn btn-primary btn-sm"
                                                style="font-size: 0.9vw;">Search
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="w-100">
                    <div class="card">
                        <div class="card-body table_card">
                            <div class="mt-2 full_report">
                                <div class="">
                                    <div class="print-button">
                                        <button class="button print_btn" style="height: 45px;"><i
                                                class="print-ico"></i> Print
                                        </button>
                                    </div>
                                </div>
                                <div class="table-responsive">
                                    <div id="printsection">
                                        <div class="px-3">
                                            <h5 class="report-status me-3">Daily Animal Report</h5>
                                            <p class="font-size-14 me-3"><strong style="font-weight: 600;color: black">Date: {{date('d-m-Y')}}</strong>
                                            </p>
                                        </div>
                                        <div class="px-3">
                                            @if($empty_check)
                                                <div class="cm_grid ">
                                                    <div>
                                                        <strong>Showing data for -</strong>
                                                    </div>
                                                    <div>
                                                        @if(!empty($organizationSelected))
                                                            <strong>Organization Name: </strong>
                                                            @foreach($organizationSelected as $f_select)
                                                                @php $myArray[] = ucfirst($f_select->name); @endphp
                                                            @endforeach
                                                            <span style="color: #0050C9">
                                                        {{implode( ',', $myArray )}}
                                                        </span>
                                                        @else
                                                            <strong>Organization Name: </strong><span style="color: #0050C9">All Organizations</span>
                                                        @endif
                                                        <br>
                                                        @if(!empty($herdSelected))
                                                            <strong>Herd Name: </strong>
                                                            @foreach($herdSelected as $h_select)
                                                                @php $myArray1[] = ucfirst($h_select->name); @endphp
                                                            @endforeach
                                                            <span style="color: #0050C9">
                                                        {{implode( ',', $myArray1 )}}
                                                            </span>
                                                        @else
                                                            <strong>Herd Name: </strong><span style="color: #0050C9">All Herds</span>
                                                        @endif
                                                        <br>
                                                        @if(!empty($lotSelected))
                                                            <strong>Lot Name: </strong>
                                                            @foreach($lotSelected as $l_select)
                                                                @php $myArray2[] = ucfirst($l_select->name); @endphp
                                                            @endforeach
                                                            <span style="color: #0050C9">
                                                        {{implode( ',', $myArray2 )}}
                                                                <span style="color: #0050C9">
                                                        @else
                                                           <strong>Lot Name: </strong><span
                                                                            style="color: #0050C9">All Lots</span>
                                                        @endif
                                                    </div>
                                                </div>
                                            @endif
                                        </div>
                                        <div class="cm_grid p-3">
                                            <div>
                                                <strong>New Registration: </strong>
                                                <br>
                                                <strong>Total Registered Animal: </strong>
                                            </div>
                                            <div>
                                                <span style="color: #0050C9">{{$rData['newRegistration'] ?? 0}}</span>
                                                <br>
                                                <span
                                                    style="color: #0050C9">{{$rData['totalRegisteredAnimal'] ?? 0}}</span>
                                            </div>
                                        </div>
                                        <h6 class="text-muted text-center text-700">Breeding Details</h6>
                                        <div>
                                            <table class="table table-bordered" >
                                                <tr>
                                                    <th style="width: 200px"></th>
                                                    <th style="font-weight: 600">Expected</th>
                                                    <th style="font-weight: 600">Observed</th>
                                                </tr>
                                                <tr>
                                                    <td>Heat/A.I</td>
                                                    <td>{{$expectedData['ai'] ?? 0}}</td>
                                                    <td>{{$rData['aiCount'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>Heat/AI Heifer</td>
                                                    <td>{{$expectedData['heifer_ai'] ?? 0}}</td>
                                                    <td>{{$rData['aiHeiferCount'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>PD (1st and 2nd)</td>
                                                    <td>{{$expectedData['pd'] ?? 0}}</td>
                                                    <td>{{$rData['pdCount'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>Calving</td>
                                                    <td>{{$expectedData['calving'] ?? 0}}</td>
                                                    <td>{{$rData['calvingCount'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>Dry Off</td>
                                                    <td>{{$expectedData['calving'] ?? 0}}</td>
                                                    <td>{{$rData['dryOffCount'] ?? 0}}</td>
                                                </tr>
                                            </table>
                                        </div>
                                        <h6 class="text-muted text-center text-700">Health Details</h6>
                                        <div>
                                            <table class="table table-bordered">
                                                <tr>
                                                    <th style="width: 200px"></th>
                                                    <th style="font-weight: 600">Expected</th>
                                                    <th style="font-weight: 600">Observed</th>
                                                </tr>
                                                <tr>
                                                    <td>Vaccination</td>
                                                    <td>{{$expectedData['vaccination'] ?? 0}}</td>
                                                    <td>{{$rData['vaccinationCount'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>Deworming</td>
                                                    <td>{{$expectedData['deworming'] ?? 0}}</td>
                                                    <td>{{$rData['dewormingCount'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>Total Animal Treated</td>
                                                    <td colspan="6"
                                                        class="text-center">{{$rData['totalDisposal'] ?? 0}}</td>
                                                </tr>
                                            </table>
                                        </div>
                                        <h6 class="text-muted text-center text-700">Milking Details</h6>
                                        <div>
                                            <table class="table table-bordered">
                                                <tr>
                                                    <th style="width: 50%; font-weight: 600;height: 40px"></th>
                                                    <th style="font-weight: 600;height: 40px"></th>
                                                </tr>
                                                <tr>
                                                    <td>No of Milking Animal</td>
                                                    <td>{{$rData['numberOfMilkingAnimal'] ?? 0}}</td>
                                                </tr>
                                                <tr>
                                                    <td>Morning Milk</td>
                                                    <td>{{$rData['eveningMilk'] ?? 0}} Kg</td>
                                                </tr>
                                                <tr>
                                                    <td>Evening Milk</td>
                                                    <td>{{$rData['morningMilk'] ?? 0}} Kg</td>
                                                </tr>
                                                <tr>
                                                    <td>Total Milk</td>
                                                    <td>{{$rData['totalMilk'] ?? 0}} Kg</td>

                                                </tr>
                                            </table>
                                        </div>
                                        <div class="row">
                                            <div class="col-6"><h6>Total Disposal</h6></div>
                                            <div class="col-6"><h6>{{$rData['totalDisposal'] ?? 0}}</h6></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
@push('scripts')
    <script type="text/javascript">
        $(document).ready(function ($) {
            $('.print_btn').click(function (e) {
                e.preventDefault();
                $("#printsection").print();
            });
            $("#start_date").datepicker({autoHide: true, format: 'dd/mm/yyyy'});
            $("#end_date").datepicker({autoHide: true, format: 'dd/mm/yyyy'});
            $('#organizationCheckAll').click(function () {
                if ($(this).is(':checked')) {
                    $('input[name="organizationCheck[]"]').prop('checked', true);
                } else {
                    $('input[name="organizationCheck[]"]').prop('checked', false);
                }
            });
            $('#herdCheckAll').click(function () {
                if ($(this).is(':checked')) {
                    $('input[name="herdCheck[]"]').prop('checked', true);
                } else {
                    $('input[name="herdCheck[]"]').prop('checked', false);
                }
            });

            $('#lotCheckAll').click(function () {
                if ($(this).is(':checked')) {
                    $('input[name="lotCheck[]"]').prop('checked', true);
                } else {
                    $('input[name="lotCheck[]"]').prop('checked', false);
                }
            });

            $("input[name$='sex']").click(function () {
                var sex = $(this).val();
                if (sex == 'male') {
                    $('.male_selected').attr("disabled", true);
                    $('.option_hide').addClass("d-none");
                    $('#adult').attr("disabled", true);
                } else {
                    $('.male_selected').attr("disabled", false);
                    $('.option_hide').removeClass("d-none");
                    $('#adult').attr("disabled", false);
                }
            });

            $("input[name$='status']").click(function () {
                var status = $(this).val();
                if (status === 'heifer' || status === 'calf') {
                    $('.male_selected').attr("disabled", true);
                    $('.option_hide').addClass("d-none");
                } else {
                    $('.male_selected').attr("disabled", false);
                    $('.option_hide').removeClass("d-none");
                }
            });
        });

        function getHerd() {
            var str = '';
            var val = document.getElementsByClassName('organization-list');
            for (i = 0; i < val.length; i++) {
                if (val[i].checked) {
                    str += val[i].value + ',';
                }
            }

            $.ajax({
                type: "GET",
                url: "{{url('system/report/general/get-herd-list')}}",
                data: 'organization_id=' + str,
                success: function (res) {
                    console.log(res);
                    if (res) {
                        $("#herd").empty().html(res.herdoutput);
                        $("#lot").empty().html(res.lotoutput);
                    } else {
                        $("#herd").empty();
                        $("#lot").empty();
                    }
                }
            });
        }

        function getLot() {
            var str = '';
            var val = document.getElementsByClassName('herd-list');
            for (i = 0; i < val.length; i++) {
                if (val[i].checked) {
                    str += val[i].value + ',';
                }
            }

            $.ajax({
                type: "GET",
                url: "{{url('system/report/general/get-lot-list')}}",
                data: 'herd_id=' + str,
                success: function (res) {
                    if (res) {
                        $("#lot").empty().html(res.lotoutput);
                    } else {
                        $("#lot").empty();
                    }
                }
            });
        }

        $("#print").click(function () {
            var printContents = document.getElementById("generalReportPdf").innerHTML;
            var printStyle = '<style>@page {size: auto;margin: 0mm;}</style>';
            var printDiv = '<div id="order-details-wrapper" style="padding: 20px;">' + printStyle + printContents + '</div>';
            document.body.innerHTML = printDiv;
            var originalContents = document.body.innerHTML;
            window.print();
            document.body.innerHTML = originalContents;
            location.reload();
        });
    </script>
@endpush
