<?php
    $hideLabel = $hideLabel ?? false;
    $wrapperClass = $wrapperClass ?? '';
    $fieldKey = 'field_'.$field->id;
    $columns = $field->tableColumns();
    $rowsData = $value ?? [];

    if (is_string($rowsData)) {
        $rowsData = json_decode($rowsData, true) ?? [];
    }
    if (! is_array($rowsData)) {
        $rowsData = [];
    }
    if (empty($rowsData) && ! $readOnly) {
        $emptyRow = [];
        foreach ($columns as $ci => $col) {
            $emptyRow['col_'.$ci] = '';
        }
        $rowsData = [$emptyRow];
    }

    $theadBg = $readOnly ? 'bg-slate-50' : 'bg-transtev-50';
    $thCell = $readOnly ? 'px-4 py-2' : 'px-3 py-2';
    $tdCell = $readOnly ? 'px-4 py-2' : 'px-2 py-2';
?>

<div class="<?php echo e($hideLabel ? '' : 'mb-5'); ?>" <?php if(! $readOnly): ?> x-data="tableField(<?php echo \Illuminate\Support\Js::from($columns)->toHtml() ?>, <?php echo \Illuminate\Support\Js::from($rowsData)->toHtml() ?>, '<?php echo e($fieldKey); ?>')" <?php endif; ?>>
    <?php if (! ($hideLabel)): ?>
        <label class="block text-sm font-medium text-slate-700 mb-2">
            <?php echo e($field->label); ?>

            <?php if($field->required && ! $readOnly): ?><span class="text-red-500">*</span><?php endif; ?>
        </label>
        <?php if(!empty($field->description)): ?>
            <p class="text-xs text-slate-500 mb-3 -mt-1"><?php echo e($field->description); ?></p>
        <?php endif; ?>
    <?php endif; ?>

    <div class="table-scroll-wrap <?php echo e($wrapperClass); ?>">
        <table>
            <thead class="<?php echo e($theadBg); ?>">
                <tr>
                    <?php $__currentLoopData = $columns; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $col): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <th class="<?php echo e($thCell); ?> text-left font-semibold text-slate-600 whitespace-nowrap <?php echo e($theadBg); ?> min-w-[120px]"><?php echo e($col['label']); ?></th>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php if (! ($readOnly)): ?>
                        <th class="px-2 py-2 w-16 <?php echo e($theadBg); ?>"></th>
                    <?php endif; ?>
                </tr>
            </thead>
            <tbody class="<?php echo e($readOnly ? 'divide-y divide-slate-100' : ''); ?>">
                <?php if($readOnly): ?>
                    <?php $__empty_1 = true; $__currentLoopData = $rowsData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <?php $__currentLoopData = $columns; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ci => $col): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php $cell = $row['col_'.$ci] ?? null; ?>
                                <td class="<?php echo e($tdCell); ?> text-slate-700 min-w-[120px] align-top">
                                    <?php if($col['type'] === 'file'): ?>
                                        <?php if (isset($component)) { $__componentOriginale428ff3664af329988e8d13451374b90 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginale428ff3664af329988e8d13451374b90 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.file-preview','data' => ['path' => $cell,'compact' => true]] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('file-preview'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['path' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($cell),'compact' => true]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginale428ff3664af329988e8d13451374b90)): ?>
<?php $attributes = $__attributesOriginale428ff3664af329988e8d13451374b90; ?>
<?php unset($__attributesOriginale428ff3664af329988e8d13451374b90); ?>
<?php endif; ?>
<?php if (isset($__componentOriginale428ff3664af329988e8d13451374b90)): ?>
<?php $component = $__componentOriginale428ff3664af329988e8d13451374b90; ?>
<?php unset($__componentOriginale428ff3664af329988e8d13451374b90); ?>
<?php endif; ?>
                                    <?php else: ?>
                                        <?php echo e($cell ?: '—'); ?>

                                    <?php endif; ?>
                                </td>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="<?php echo e(max(count($columns), 1)); ?>" class="px-4 py-4 text-slate-400 text-center">Aucune ligne</td>
                        </tr>
                    <?php endif; ?>
                <?php else: ?>
                    <template x-for="(row, rowIndex) in rows" :key="rowIndex">
                        <tr class="border-t border-slate-100">
                            <template x-for="(col, colIndex) in columns" :key="colIndex">
                                <td class="<?php echo e($tdCell); ?> align-top min-w-[120px]">
                                    <template x-if="col.type === 'textarea'">
                                        <textarea :name="fieldKey + '[rows][' + rowIndex + '][col_' + colIndex + ']'" x-model="row['col_' + colIndex]" rows="2" class="input-modern text-sm w-full min-w-[100px]"></textarea>
                                    </template>
                                    <template x-if="col.type === 'select'">
                                        <select :name="fieldKey + '[rows][' + rowIndex + '][col_' + colIndex + ']'" x-model="row['col_' + colIndex]" class="input-modern text-sm w-full min-w-[100px]">
                                            <option value="">—</option>
                                            <template x-for="opt in (col.options || [])" :key="opt">
                                                <option :value="opt" x-text="opt"></option>
                                            </template>
                                        </select>
                                    </template>
                                    <template x-if="col.type === 'file'">
                                        <div class="space-y-1 min-w-[160px]">
                                            <template x-if="row['col_' + colIndex]">
                                                <a :href="'<?php echo e(route('files.show')); ?>?path=' + encodeURIComponent(row['col_' + colIndex]) + '&d=attachment'" class="file-preview-link file-preview-link--muted text-xs block truncate max-w-[160px]">
                                                    <span x-text="baseName(row['col_' + colIndex])"></span>
                                                </a>
                                            </template>
                                            <label class="file-dropzone file-dropzone--sm" :class="localFiles[rowIndex + '_' + colIndex] ? 'file-dropzone--filled' : ''">
                                                <svg class="file-dropzone-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M21.44 11.05 12.25 20.24a5.5 5.5 0 0 1-7.78-7.78l9.19-9.19a3.5 3.5 0 0 1 4.95 4.95l-9.2 9.19a1.5 1.5 0 0 1-2.12-2.12l8.49-8.49"/></svg>
                                                <span class="file-dropzone-text">
                                                    <template x-if="! localFiles[rowIndex + '_' + colIndex]">
                                                        <span class="file-dropzone-accent">Choisir un fichier</span>
                                                    </template>
                                                    <template x-if="localFiles[rowIndex + '_' + colIndex]">
                                                        <span class="file-dropzone-filename" x-text="localFiles[rowIndex + '_' + colIndex]"></span>
                                                    </template>
                                                </span>
                                                <input type="file" :name="fieldKey + '[rows][' + rowIndex + '][col_' + colIndex + ']'" class="sr-only" @change="pickFile(rowIndex, colIndex, $event)">
                                            </label>
                                        </div>
                                    </template>
                                    <template x-if="!['textarea','select','file'].includes(col.type)">
                                        <input :type="col.type === 'number' ? 'number' : (col.type === 'date' ? 'date' : 'text')"
                                            :name="fieldKey + '[rows][' + rowIndex + '][col_' + colIndex + ']'"
                                            x-model="row['col_' + colIndex]"
                                            class="input-modern text-sm w-full min-w-[100px]">
                                    </template>
                                </td>
                            </template>
                            <td class="px-2 py-2 align-top w-16">
                                <button type="button" @click="removeRow(rowIndex)" class="text-red-500 text-xs hover:underline mt-2 whitespace-nowrap" x-show="rows.length > 1">Suppr.</button>
                            </td>
                        </tr>
                    </template>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (! ($readOnly)): ?>
        <button type="button" @click="addRow()" class="mt-3 inline-flex items-center gap-1 text-sm font-medium text-transtev-600 hover:text-transtev-700">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
            Ajouter une ligne
        </button>
    <?php endif; ?>

    <?php if (! ($hideLabel)): ?>
        <?php $__errorArgs = [$fieldKey];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
            <p class="mt-1.5 text-sm text-red-600"><?php echo e($message); ?></p>
        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
        <?php $__errorArgs = [$fieldKey.'.*'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
            <p class="mt-1.5 text-sm text-red-600"><?php echo e($message); ?></p>
        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
    <?php endif; ?>
</div>

<?php if (! $__env->hasRenderedOnce('bf538844-11eb-4077-9714-922010b8813a')): $__env->markAsRenderedOnce('bf538844-11eb-4077-9714-922010b8813a'); ?>
<script>
function tableField(columns, initialRows, fieldKey) {
    const emptyRow = () => {
        const row = {};
        columns.forEach((_, i) => { row['col_' + i] = ''; });
        return row;
    };
    return {
        columns,
        fieldKey,
        rows: initialRows.length ? JSON.parse(JSON.stringify(initialRows)) : [emptyRow()],
        localFiles: {},
        addRow() {
            this.rows.push(emptyRow());
        },
        removeRow(index) {
            if (this.rows.length > 1) this.rows.splice(index, 1);
        },
        pickFile(rowIndex, colIndex, event) {
            const file = event.target.files[0];
            this.localFiles[rowIndex + '_' + colIndex] = file ? file.name : '';
        },
        baseName(path) {
            return path ? String(path).split('/').pop() : '';
        },
    };
}
</script>
<?php endif; ?>
<?php /**PATH C:\xampp\htdocs\teyssir\resources\views/partials/dynamic-table-field.blade.php ENDPATH**/ ?>