<div class="row">
    <div class="col-md-6 mb-3">
        <label class="form-label">Category</label>
        <select name="cattle_category_id" class="form-control" required>
            <option value="">Select Category</option>
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($category->id); ?>"
                    <?php echo e(old('cattle_category_id', $animal->cattle_category_id ?? '') == $category->id ? 'selected' : ''); ?>>
                    <?php echo e($category->name); ?>

                </option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
        </select>

        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__errorArgs = ['cattle_category_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
            <small class="text-danger"><?php echo e($message); ?></small>
        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    </div>

    <div class="col-md-6 mb-3">
        <label class="form-label">Animal Name</label>
        <input type="text"
               name="animal_name"
               class="form-control"
               value="<?php echo e(old('animal_name', $animal->animal_name ?? '')); ?>"
               required>

        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__errorArgs = ['animal_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
            <small class="text-danger"><?php echo e($message); ?></small>
        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    </div>

    <div class="col-md-6 mb-3">
        <label class="form-label">Buy Date</label>
        <input type="date"
               name="buy_date"
               class="form-control"
               value="<?php echo e(old('buy_date', $animal->buy_date ?? '')); ?>">
    </div>

    <div class="col-md-6 mb-3">
        <label class="form-label">Buy Price</label>
        <input type="number"
               step="0.01"
               name="buy_price"
               class="form-control"
               value="<?php echo e(old('buy_price', $animal->buy_price ?? 0)); ?>">
    </div>

    <div class="col-md-6 mb-3">
        <label class="form-label">Sale Date</label>
        <input type="date"
               name="sale_date"
               class="form-control"
               value="<?php echo e(old('sale_date', $animal->sale_date ?? '')); ?>">
    </div>

    <div class="col-md-6 mb-3">
        <label class="form-label">Sale Price</label>
        <input type="number"
               step="0.01"
               name="sale_price"
               class="form-control"
               value="<?php echo e(old('sale_price', $animal->sale_price ?? 0)); ?>">
    </div>

    <div class="col-md-4 mb-3">
        <label class="form-label">Breed</label>
        <input type="text"
               name="breed"
               class="form-control"
               value="<?php echo e(old('breed', $animal->breed ?? '')); ?>">
    </div>

    <div class="col-md-4 mb-3">
        <label class="form-label">Weight</label>
        <input type="number"
               step="0.01"
               name="weight"
               class="form-control"
               value="<?php echo e(old('weight', $animal->weight ?? '')); ?>">
    </div>

    <div class="col-md-4 mb-3">
        <label class="form-label">Tag Number</label>
        <input type="text"
               name="tag_number"
               class="form-control"
               value="<?php echo e(old('tag_number', $animal->tag_number ?? '')); ?>">
    </div>

    <div class="col-md-12 mb-3">
        <label class="form-label">Notes</label>
        <textarea name="notes"
                  class="form-control"
                  rows="3"><?php echo e(old('notes', $animal->notes ?? '')); ?></textarea>
    </div>

    <div class="col-md-12 mb-3">
        <label class="form-label">Images</label>
        <input type="file"
               name="images[]"
               id="images"
               class="form-control"
               multiple
               accept="image/*">

        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__errorArgs = ['images.*'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
            <small class="text-danger"><?php echo e($message); ?></small>
        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    </div>

    <div class="col-md-12">
        <div id="preview-area" class="d-flex flex-wrap gap-3"></div>
    </div>
</div>

<?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(isset($animal) && $animal->images->count() > 0): ?>
    <hr>

    <h6>Uploaded Images</h6>

    <div class="d-flex flex-wrap gap-3">
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $animal->images; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $img): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="border rounded p-2 text-center">
                <img src="<?php echo e(asset($img->image)); ?>"
                     width="120"
                     height="120"
                     style="object-fit:cover;border-radius:8px;">

                <div class="mt-2">
                    <a href="<?php echo e(asset($img->image)); ?>"
                       target="_blank"
                       class="btn btn-sm btn-info">
                        View
                    </a>

                    <form action="<?php echo e(route('animal-images.destroy', $img->id)); ?>"
                          method="POST"
                          class="d-inline"
                          onsubmit="return confirm('Delete this image?')">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('DELETE'); ?>

                        <button type="submit" class="btn btn-sm btn-danger">
                            Delete
                        </button>
                    </form>
                </div>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    </div>
<?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

<?php $__env->startPush('script'); ?>
<script>
let selectedFiles = [];

const imageInput = document.getElementById('images');
const previewArea = document.getElementById('preview-area');

if (imageInput) {
    imageInput.addEventListener('change', function (event) {
        selectedFiles = Array.from(event.target.files);
        renderPreview();
    });
}

function renderPreview() {
    previewArea.innerHTML = '';

    selectedFiles.forEach((file, index) => {
        let reader = new FileReader();

        reader.onload = function (e) {
            let previewBox = document.createElement('div');
            previewBox.classList.add('border', 'rounded', 'p-2', 'text-center');

            previewBox.innerHTML = `
                <img src="${e.target.result}"
                     width="120"
                     height="120"
                     style="object-fit:cover;border-radius:8px;">

                <div class="mt-2">
                    <a href="${e.target.result}" target="_blank" class="btn btn-sm btn-info">
                        View
                    </a>

                    <button type="button"
                            class="btn btn-sm btn-danger"
                            onclick="removePreview(${index})">
                        Delete
                    </button>
                </div>
            `;

            previewArea.appendChild(previewBox);
        };

        reader.readAsDataURL(file);
    });
}

function removePreview(index) {
    selectedFiles.splice(index, 1);

    let dataTransfer = new DataTransfer();

    selectedFiles.forEach(file => {
        dataTransfer.items.add(file);
    });

    imageInput.files = dataTransfer.files;

    renderPreview();
}
</script>
<?php $__env->stopPush(); ?><?php /**PATH F:\xampp\htdocs\Cattle-Farm-Management-System\resources\views/animals/form.blade.php ENDPATH**/ ?>