<div class="mb-3">
    <a href="/bulk_create_files" class="btn btn-outline">
        <i class="fas fa-list me-1"></i> Bulk File Creation
    </a>
</div>

<form id="createFileForm" data-form="register" action="/create_file" method="post" enctype="multipart/form-data">
    <input type="hidden" name="upload_group_key_ifnone" value="{{ upload_group_key }}">
    <input type="hidden" value="na" id="name" name="name">
    
    <div class="row g-4">
        <!-- Left Column: File Sources -->
        <div class="col-lg-6">
            <div class="form-section">
                <h5><i class="fas fa-file me-2"></i> Specify Files</h5>
                <hr>
                
                <div class="mb-3">
                    <label for="file_data" class="form-label">Local File</label>
                    <input type="file" class="form-control" id="file_data" name="file_data">
                </div>
                
                <div class="mb-3">
                    <label for="directory" class="form-label">Directory of Files</label>
                    <input type="file" class="form-control" id="directory" name="directory" webkitdirectory directory multiple>
                    <div class="form-text">Not recursive, max 1k files</div>
                </div>
                
                <div class="mb-3">
                    <label for="urls" class="form-label">Public URLs (one per line)</label>
                    <textarea class="form-control" id="urls" name="urls" rows="3"></textarea>
                </div>
                
                <div class="mb-3">
                    <label for="s3_uris" class="form-label">S3 URIs (file or directory)</label>
                    <textarea class="form-control" id="s3_uris" name="s3_uris" rows="3"></textarea>
                </div>
            </div>
        </div>
        
        <!-- Right Column: Metadata -->
        <div class="col-lg-6">
            <div class="form-section">
                <h5><i class="fas fa-tags me-2"></i> Add Metadata</h5>
                <hr>
                
                <div class="mb-3">
                    <label for="upload_group_key" class="form-label">File Set Name</label>
                    <input type="text" class="form-control" name="upload_group_key">
                </div>
                
                {% for field in ui_fields %}
                    {% if field.name != "creating_user" %}
                    <div class="mb-3">
                        {% if field.type == "select" %}
                            <label for="{{ field.name }}" class="form-label">{{ field.label }}</label>
                            <select class="form-select" name="{{ field.name }}" id="{{ field.name }}">
                                <option value="" selected></option>
                                {% for option in field.options %}
                                    <option value="{{ option }}">{{ option }}</option>
                                {% endfor %}
                            </select>
                        {% elif field.name.endswith('record_datetime') %}
                            <label class="form-label">{{ field.label }}</label>
                            <div class="row g-2">
                                <div class="col">
                                    <input type="date" class="form-control" id="custom-date-start" name="custom-date-start">
                                </div>
                                <div class="col">
                                    <input type="time" class="form-control" id="custom-time-start" name="custom-time-start">
                                </div>
                            </div>
                            <input type="hidden" id="record_datetime" name="record_datetime">
                        {% elif field.name.endswith('record_datetime_end') %}
                            <label class="form-label">{{ field.label }}</label>
                            <div class="row g-2">
                                <div class="col">
                                    <input type="date" class="form-control" id="custom-date-end" name="custom-date-end">
                                </div>
                                <div class="col">
                                    <input type="time" class="form-control" id="custom-time-end" name="custom-time-end">
                                </div>
                            </div>
                            <input type="hidden" id="record_datetime_end" name="record_datetime_end">
                        {% elif field.name.endswith('_date') %}
                            <label for="{{ field.name }}" class="form-label">{{ field.label }}</label>
                            <input type="date" class="form-control" name="{{ field.name }}" id="{{ field.name }}">
                        {% elif not field.name.endswith('_break') %}
                            <label for="{{ field.name }}" class="form-label">{{ field.label }}</label>
                            <input type="{{ field.type }}" class="form-control" name="{{ field.name }}" id="{{ field.name }}">
                        {% endif %}
                    </div>
                    {% endif %}
                {% endfor %}
            </div>
        </div>
    </div>
    
    <div class="mt-3">
        <button type="submit" class="btn btn-primary">
            <i class="fas fa-plus me-1"></i> Create
        </button>
    </div>
</form>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const dateInput = document.getElementById('custom-date-start');
    const timeInput = document.getElementById('custom-time-start');
    const mergedField = document.getElementById('record_datetime');
    const dateInpute = document.getElementById('custom-date-end');
    const timeInpute = document.getElementById('custom-time-end');
    const mergedFielde = document.getElementById('record_datetime_end');

    const updateMergedField = () => {
        if (dateInput && timeInput && mergedField) {
            const dateValue = dateInput.value || '';
            const timeValue = timeInput.value || '';
            mergedField.value = dateValue && timeValue ? `${dateValue}T${timeValue}` : dateValue;
        }
    };

    const updateMergedFielde = () => {
        if (dateInpute && timeInpute && mergedFielde) {
            const dateValuee = dateInpute.value || '';
            const timeValuee = timeInpute.value || '';
            mergedFielde.value = dateValuee && timeValuee ? `${dateValuee}T${timeValuee}` : dateValuee;
        }
    };

    if (dateInput) dateInput.addEventListener('change', updateMergedField);
    if (timeInput) timeInput.addEventListener('change', updateMergedField);
    if (dateInpute) dateInpute.addEventListener('change', updateMergedFielde);
    if (timeInpute) timeInpute.addEventListener('change', updateMergedFielde);
});
</script>

