
<?php $__env->startSection('title','A.I'); ?>
<?php $__env->startSection('content'); ?>
    <h2 class="ms-2 mb-3 card-title">AI List</h2>
    <div class="card mt-3 m-lg-2">
        <div class="card-body table_card mt-2">
            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('AI.create')): ?>
                <div class="add-new-button-hidden">
                    <button class="btn add_new_btn" data-bs-toggle="modal" data-bs-target="#dryoff_add_modal"><i class="plus-ico"></i> <?php echo e(__('messages.add').' '. __('messages.ai')); ?>

                    </button>
                </div>
            <?php endif; ?>
            <table id="myTable" class="table table-bordered dt-responsive nowrap w-100">
                <thead>
                <tr>
                    <th><?php echo e(__('messages.sl')); ?></th>
                    <th><?php echo e(__('messages.tag_id')); ?></th>
                    <th><?php echo e(__('messages.sire').' '.__('messages.code')); ?></th>
                    <th><?php echo e(__('messages.ai').' '.__('messages.date')); ?></th>
                    <th><?php echo e(__('messages.service_type')); ?></th>
                    <th><?php echo e(__('messages.action')); ?></th>
                </tr>
                </thead>
                <tbody>
                <?php $__currentLoopData = $inseminations; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td><?php echo e(++$key); ?></td>
                        <td><?php echo e(($item->cattle_id) ? $item->cattle->tag->uid : 'N/A'); ?></td>
                        <td><?php echo e(($item->sire_id) ? $item->sire->code : 'N/A'); ?></td>
                        <td><?php echo e(($item->heat_date) ? \Carbon\Carbon::createFromFormat('Y-m-d', $item->heat_date)->format('d-m-Y') : 'N/A'); ?></td>
                        <td>
                            <?php if($item->service_type === 'AI'): ?>
                                <span class="badge badge-pill badge-soft-info"><?php echo e(__('messages.ai')); ?></span>
                            <?php elseif($item->service_type === 'NATURAL'): ?>
                                <span class="badge badge-pill badge-soft-success"><?php echo e(__('messages.natural')); ?></span>
                            <?php elseif($item->service_type === 'SKIP'): ?>
                                <span class="badge badge-pill badge-soft-warning"><?php echo e(__('messages.skip')); ?></span>
                            <?php else: ?>
                                <span class="badge badge-pill badge-soft-danger">ET</span>
                            <?php endif; ?>
                        </td>
                        <td class="center">
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('AI.edit')): ?>
                                <a data-id="<?php echo e($item->id); ?>" class="btn edit-btn" data-bs-toggle="modal"
                                   data-bs-target="#dryoff_edit_modal">
                                    <i class="edit-ico"></i>
                                </a>
                            <?php endif; ?>
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('AI.delete')): ?>
                                <a href="#" class="btn" onclick="deletePost(<?php echo e($item->id); ?>)">
                                    <i class="delete-ico"></i>

                                    <form id="delete-form-<?php echo e($item->id); ?>"
                                          action="<?php echo e(route('ai.destroy', $item->id)); ?>" method="POST"
                                          style="display: none;">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('DELETE'); ?>
                                    </form>
                                </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="modal fade" id="dryoff_add_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content p-5">
                <h4 class="card-title"><?php echo e(__('messages.add').' '.__('messages.ai')); ?></h4>
                <div class="modal-body p-0 mt-3">
                    <form action="<?php echo e(route('ai.store')); ?>" method="POST" role="form" id="form1"
                          enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>
                        <div class="row">
                            <div class="col-md-6">
                                <label class="form-label" for="cattle_id"><?php echo e(__('messages.tag_id')); ?> <span
                                        class="text-danger">*</span></label>
                                <select class="form-select mySelect2 <?php $__errorArgs = ['cattle_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                        name="cattle_id" id="cattle_id" required>
                                    <option value=""><?php echo e(__('messages.select').' '.__('messages.tag_id')); ?></option>
                                    <?php $__currentLoopData = $cattles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php
                                            $today = Carbon\Carbon::now()->addDays(1)->format('Y-m-d');
                                            $adult_age_start_day = config('perameters.adult_age_start');
                                            $adult_date = Carbon\Carbon::createFromFormat('Y-m-d', $item->birth_date)->addDays($adult_age_start_day)->format('Y-m-d');
                                        ?>
                                        <?php if($today >=  $adult_date): ?>
                                            <option
                                                value="<?php echo e($item->id); ?>"><?php echo e($item->tag->uid); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label" for="sire_id"> <?php echo e(__('messages.sire').' '.__('messages.code')); ?> <span
                                        class="text-danger">*</span></label>
                                <select class="form-control select2-multiple <?php $__errorArgs = ['sire_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                        name="sire_id" id="sire_id" required>
                                    <option value=""><?php echo e(__('messages.select').' '.__('messages.sire').' '.__('messages.code')); ?></option>
                                    <?php $__currentLoopData = $sires; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($item->id); ?>"><?php echo e($item->code); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <label class="form-label" for="heat_date"> <?php echo e(__('messages.ai').' '.__('messages.date')); ?> <span
                                        class="text-danger">*</span></label>
                                <input type="text"
                                       class="form-control custom-date-format <?php $__errorArgs = ['heat_date'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                       name="heat_date" id="heat_date" value="<?php echo e(old('heat_date')); ?>"
                                       placeholder="Pick heat date" required/>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label" for="service_type"> <?php echo e(__('messages.service_type')); ?> <span
                                        class="text-danger">*</span></label>
                                <select class="form-control  <?php $__errorArgs = ['service_type'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                        name="service_type" id="service_type" required>
                                    <option value=""><?php echo e(__('messages.select').' '.__('messages.service_type')); ?></option>
                                    <option value="AI"><?php echo e(__('messages.ai')); ?></option>
                                    <option value="NATURAL"><?php echo e(__('messages.natural')); ?></option>
                                    <option value="SKIP"> <?php echo e(__('messages.skip')); ?></option>
                                    <option value="ET">ET</option>
                                </select>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="inseminated_by"><?php echo e(__('messages.inseminated_by')); ?></label>
                                    <select class="form-control" name="inseminated_by" id="inseminated_by">
                                        <option value=""><?php echo e(__('messages.select').' '.__('messages.inseminated_by')); ?></option>
                                        <?php $__currentLoopData = $inseminators; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($item->id); ?>"><?php echo e($item->name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label" for="total_dose"><?php echo e(__('messages.total_dose')); ?></label>
                                <input type="text" class="form-control" name="total_dose" id="total_dose"
                                       value="<?php echo e(old('total_dose')); ?>" placeholder="<?php echo e(__('messages.enter').' '.__('messages.total_dose')); ?>"/>
                            </div>
                        </div>
                        <div class="mt-5 d-flex justify-content-end mt-5">
                            <button type="button" class="btn  me-2" data-bs-dismiss="modal"
                                    style="background: #EAECF0;"> <?php echo e(__('messages.close')); ?>

                            </button>
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('AI.create')): ?>
                                <div class="d-flex flex-flex-row-reverse gap-2">
                                    <button type="submit" class="btn btn-primary waves-effect waves-light"> <?php echo e(__('messages.save')); ?></button>
                                </div>
                            <?php endif; ?>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    
    <div class="modal fade" id="dryoff_edit_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content p-5">
                <h4 class="card-title"><?php echo e(__('messages.update').' '.__('messages.ai').' '.__('messages.information')); ?></h4>
                <div class="modal-body p-0 mt-3">
                    <form action="<?php echo e(route('ai.new.update')); ?>" method="POST" role="form" id="form1"
                          enctype="multipart/form-data">
                        <?php echo method_field('PUT'); ?>
                        <?php echo csrf_field(); ?>
                        <div class="row">
                            <div class="col-md-6">
                                <input type="hidden" name="item_id" id="item_id">
                                <label class="form-label" for="cattle_edit_id"> <?php echo e(__('messages.tag_id')); ?> <span class="text-danger">*</span></label>
                                <select class="form-control mySelect2e <?php $__errorArgs = ['cattle_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                        name="cattle_id" id="cattle_edit_id" required>
                                    <option value=""><?php echo e(__('messages.select').' '.__('messages.tag_id')); ?></option>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label" for="sire_edit_id"> <?php echo e(__('messages.sire_code')); ?> <span
                                        class="text-danger">*</span></label>
                                <select class="form-control select2-multiple <?php $__errorArgs = ['sire_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                        name="sire_id" id="sire_edit_id" required>
                                    <option value=""><?php echo e(__('messages.select').' '.__('messages.sire_code')); ?></option>
                                </select>
                            </div>
                        </div>
                        <div class="row mt-2">
                            <div class="col-md-6">
                                <label class="form-label" for="heat_edit_date"> <?php echo e(__('messages.ai').' '.__('messages.date')); ?> <span
                                        class="text-danger">*</span></label>
                                <input type="text"
                                       class="form-control custom-date-format <?php $__errorArgs = ['heat_date'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                       name="heat_date" id="heat_edit_date" placeholder="Pick heat date" value=""
                                       required/>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label" for="service_edit_type"> <?php echo e(__('messages.service_type')); ?> <span
                                        class="text-danger">*</span></label>
                                <select class="form-control <?php $__errorArgs = ['service_type'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                        name="service_type" id="service_edit_type" required></select>
                            </div>
                        </div>
                        <div class="row mt-2">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="inseminated_by"> <?php echo e(__('messages.inseminated_by')); ?> </label>
                                    <select class="form-control" name="inseminated_by" id="inseminated_by">
                                        <option value=""> <?php echo e(__('messages.select').' '. __('messages.inseminated_by')); ?> </option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label" for="total_edit_dose"> <?php echo e(__('messages.total_dose')); ?> </label>
                                <input type="text" class="form-control" name="total_dose" id="total_edit_dose"
                                       placeholder="Enter total dose" value=""/>
                            </div>
                        </div>
                        <div class="row mt-2 mb-2">
                            <div class="mt-5 d-flex justify-content-end mt-5">
                                <button type="button" class="btn  me-2" data-bs-dismiss="modal"
                                        style="background: #EAECF0;"> <?php echo e(__('messages.close')); ?>

                                </button>
                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('AI.create')): ?>
                                    <div class="d-flex flex-flex-row-reverse gap-2">
                                        <button type="submit" class="btn btn-primary waves-effect waves-light"> <?php echo e(__('messages.update')); ?>

                                        </button>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script type="text/javascript">
        $('.mySelect2').select2({
            dropdownParent: $('#dryoff_add_modal'),
            width: 'resolve',

        });
        $('.mySelect2e').select2({
            dropdownParent: $('#dryoff_edit_modal'),
            width: 'resolve',

        });

        function changeDateFormat(dateValue) {
            const dateComponents = dateValue.split("-");
            const day = dateComponents[2],
                month = dateComponents[1],
                year = dateComponents[0];
            return `${day}-${month}-${year}`;
        }

        $(document).on('click', '.edit-btn', function () {
            let id = $(this).attr('data-id');
            $('#cattle_edit_id').empty();
            $('#heat_edit_date').empty();
            $('#service_edit_type').empty();
            $('#dose').empty();
            $.ajax({
                type: "GET",
                url: "<?php echo e(url('system/ai')); ?>/" + id + "/edit",
                success: function (res) {
                    $('#item_id').val(res.ai.id);
                    $('#cattle_edit_id').empty();
                    $.each(res.cattles, function (index, value) {
                        $('#cattle_edit_id').append('<option value="' + value.cattle_id + '" ' + (value.cattle_id === res.ai.cattle_id ? 'selected' : '') + '>' + value.uid + '</option>');
                    });
                    $('#heat_edit_date').val(changeDateFormat(res.ai.heat_date));
                    $('#total_edit_dose').val(res.ai.total_dose);
                    $('#service_edit_type').append('<option value="" selected disabled>--Select Route--</option>' +
                        '<option value="AI" ' + ("AI" === res.ai.service_type ? 'selected' : '') + '>AI</option>' +
                        '<option value="NATURAL" ' + ("NATURAL" === res.ai.service_type ? 'selected' : '') + '>NATURAL</option>' +
                        ' <option value="SKIP" ' + ("SKIP" === res.ai.service_type ? 'selected' : '') + '>SKIP</option>' +
                        '<option value="ET" ' + ("ET" === res.ai.service_type ? 'selected' : '') + '>ET</option>');

                    $('#sire_dit_id').append('<option value="">Select Type</option>' +
                        '<option value="First Dose" ' + ("First Dose" === res.ai.sire_id ? 'selected' : '') + '>First Dose</option>' +
                        '<option value="Booster Dose"' + ("Booster Dose" === res.ai.sire_id ? 'selected' : '') + '>Booster Dose</option>' +
                        ' <option value="Regular Dose"' + ("Regular Dose" === res.ai.sire_id ? 'selected' : '') + '>Regular Dose</option>')
                    $.each(res.inseminators, function (index, value) {
                        $('#vaccinated_by').append('<option value="' + value.id + '" ' + (value.id === res.ai.inseminated_by ? 'selected' : '') + '>' + value.name + '</option>');
                    })
                    $('#sire_edit_id').empty();
                    $.each(res.sires, function (index, value) {
                        $('#sire_edit_id').append('<option value="' + value.id + '" ' + (value.id === res.ai.sire_id ? 'selected' : '') + '>' + value.code + '</option>');
                    })
                },
                error: function (response) {
                    console.log(response);
                }
            });
        });
    </script>

<?php $__env->stopPush(); ?>

<?php echo $__env->make('backend.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\laragon\www\ml_firm\resources\views/backend/breeding/ai/index.blade.php ENDPATH**/ ?>