<div>
    <input type="hidden" id="lead_id" value='<?php echo $lead_id ?>'>
    <?php echo form_open((admin_url('leads/checklist/' . $lead_id)), ['id' => 'checklist_borrower_form' . $lead_borrower_id]); ?>
    <?php if ($lead_borrower_id) { ?>
        <input type="hidden" name="lead_borrower_id" id="lead_borrower_id" value="<?php echo $lead_borrower_id; ?>">
    <?php } ?>
    <input type="hidden" id="checklist_stage_id" name="checklist_stage_id" value='<?php echo $checklist_id ?>'>
    <input type="hidden" id="lead_url" value="<?php echo admin_url('leads/checklist/' . $lead_id); ?>"></input>
    <div class="col-md-6">
        <!--start render input comment -->
        <!-- <?php echo render_input('name' . $checklist_id, 'Checklist Title'); ?> -->
        <!-- End render input comment -->
        <div class="form-group" app-field-wrapper="name">
            <label for="name" class="control-label"><span style="color:red;">*</span> Item Title & Description</label>
            <input type="text" id="name<?php echo $checklist_id; ?>" name="name<?php echo $checklist_id; ?>"
                class="form-control" placeholder="Please enter title ( ID back, Driving License )">
            <div id="checklist_title_error<?php echo $checklist_id; ?>" class="error-message" style="color:red;"></div>
        </div>
        <div class="form-group">
            <label for="checklist_file<?php echo $checklist_id; ?>" class="control-label">Select Images (PDF, JPEG, JPG,
                PNG, DOCX)</label>
            <input type="file" class="form-control" name="checklist_file<?php echo $checklist_id; ?>[]"
                id="checklist_file<?php echo $checklist_id; ?>" multiple accept=".pdf, .jpeg, .jpg, .png, .doc, .docx"
                style="height:53px;">
            <div id="checklist_file_error<?php echo $checklist_id; ?>" class="error-message" style="color:red;"></div>
        </div>
        <?php
        if ($leads_status_current_id == 1 || ($leads_status_current_id > 1 && $leads_status_current_id != 3)) {
            $imp_html = "<div class='form-group' id='div_outstanding_type' app-field-wrapper='outstanding_type' style='display:none;'>";
            $imp_html .= "<label for='outstanding_type' class='control-label'>Select User Type</label>";
            $imp_html .= "<select name='outstanding_type' id='outstanding_type' class='form-control selectpicker' data-width='100%' data-none-selected-text='Nothing selected' data-live-search='true' tabindex='-98'>";
            $imp_html .= "<option value='client' " . $client_select . " selected>Client</option>";
            if ($leads_status_current_id == 3) {
                $imp_html .= "<option value='ld_team' " . $ld_team_select . ">LD-Team</option>";
            }
            if ($leads_status_current_id == 1 || ($leads_status_current_id > 1 && $leads_status_current_id != 3)) {
                $imp_html .= "<option value='broker' " . $broker_select . ">Broker</option>";
            }
            $imp_html .= "</select></div>";

            echo $imp_html;
        } else if ($leads_status_current_id == 3) {
            ?>
                <input type="hidden" name='outstanding_type' value="client">
            <?php
        } else {
            ?>
                <input type="hidden" name='outstanding_type' value="client">
            <?php
        }
        ?>
        <div style="display:none;">
            <?php echo render_textarea('checklist_description' . $checklist_id, '<span style="color:red;">*</span> Item Description', '', array('placeholder' => 'Please enter the item description.')); ?>
        </div>
    </div>

    <div class="col-md-6">
        <?php
        $imp_html = "<div class='form-group' app-field-wrapper='doc_type' style='display:none;'><label for='doc_type' class='control-label'><span style='color:red;'>*</span> Doc Type</label>";
        $imp_html .= "<select name='doc_type[]' id='doc_type' class='form-control selectpicker' data-width='100%' data-none-selected-text='Nothing selected' data-live-search='true' tabindex='-98' multiple>";
        $imp_html .= "<option value=''>Nothing selected</option>";
        $doc_array = [1 => 'Png', 2 => 'Pdf', 3 => 'Jpeg', 4 => 'Zip', 5 => 'HEIC'];

        $selectedOptions = [1, 2, 3, 4, 5]; // Set the keys of the options you want to be selected
        
        foreach ($doc_array as $key => $value) {
            $selected = in_array($key, $selectedOptions) ? 'selected' : '';
            $imp_html .= "<option value='$value' $selected>$value</option>";
        }

        $imp_html .= "</select></div>";
        echo $imp_html;
        ?>
        <?php if ($checklist_id == 1) { ?>
            <!-- checklist item type -->
            <div class="form-group" style="display:none;">
                <label for="Item_name" class="control-label">ID Type</label>
                <select name="Item_name[]" id="Item_name" class="form-control selectpicker" multiple data-width="100%"
                    data-none-selected-text="Nothing selected" data-live-search="true" tabindex="-98">
                    <!-- <option value="">Nothing selected</option> -->
                    <?php foreach ($checklist_item_type_data as $value): ?>
                        <option value="<?php echo $value['id']; ?>" <?php echo in_array($value['id'], $selectedOptions) ? 'selected' : ''; ?>>
                            <?php echo $value['Item_name']; ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <?php echo form_error('Item_name[]', '<div class="text-danger">', '</div>'); ?>
            </div>

        <?php } ?>
        <div style="display:none;">
            <?php echo render_textarea('checklist_instruction' . $checklist_id, '<span style="color:red;">*</span> Item Instructions', 'Please ensure the IDs show ALL edges, are CLEAR, have no flash spots, and are valid (NOT expired).', array('placeholder' => 'Please enter the item instructions.')); ?>
        </div>
    </div>
    <?php if ($samegroup) { ?>
        <div class="clearfix"></div>
        <div class="lead-edit">
            <hr class="-tw-mx-4 tw-border-neutral-200" />
            <button type="submit" class="btn btn-primary pull-right lead-save-btn"
                id="lead-form-submit<?php echo ($lead_borrower_id) ? $lead_borrower_id : $checklist_id; ?>" <?php
                       //  if($checklist_id <= $lead_status_current_id){ previous condition
                       if ($lead_borrower_id) {
                           echo '';
                       } else {
                           if ($leads_status_current_id == 1 || $leads_status_current_id == 3 || $leads_status_current_id >= $checklist_id) {
                               echo '';
                           } else {
                               echo 'disabled';
                           }
                       }
                       ?>>
                <?php echo _l('submit'); ?>
            </button>
            <button type=" button" class="btn btn-default pull-right mright5" data-dismiss="modal">
                <?php echo _l('close'); ?>
            </button>
            <div style="margin-right: 10px;margin-top:-5px;float:right;" id="checklist_loader<?php echo $checklist_id; ?>">
            </div>
            <div class="clearfix"></div>
            <hr class="-tw-mx-4 tw-border-neutral-200" />
        </div>
    <?php } ?>
    <?php echo form_close(); ?>
</div>
<script>
    $(document).ready(function () {
        var checklistid = <?php echo $checklist_id; ?>;
        var lead_borrower_id = <?php echo $lead_borrower_id; ?>;

        $('#checklist_borrower_form' + lead_borrower_id).submit(function (e) {
            e.preventDefault();

            // var checklistStageId = $('#checklist_stage_id').val();
            var checklistStageId = checklistid;
            $('#checklist_title_error' + checklistStageId.toString()).text('');
            $('#checklist_file_error' + checklistStageId).text('');

            var checklist_title = $('#name' + checklistStageId.toString()).val().trim();
            var fileInput = $('#checklist_file' + checklistStageId);
            var files = fileInput[0].files;
            var valid = true;
            if(checklist_title == ""){
            $('#checklist_title_error' + checklistStageId.toString()).text('Title field is required.');
                valid = false;
            }
            var allowedExtensions = ['pdf', 'jpeg', 'jpg', 'png', 'doc', 'docx'];
            if(checklistStageId == 0){
                if (files.length === 0) {
                    $('#checklist_file_error' + checklistStageId).text('Please select at least one file.');
                    valid = false;
                } else {
                    $.each(files, function (index, file) {
                        var fileExtension = file.name.split('.').pop().toLowerCase();
                        if ($.inArray(fileExtension, allowedExtensions) === -1) {
                            $('#checklist_file_error' + checklistStageId).text('Invalid file type: ' + file.name);
                            valid = false;
                            return false;
                        }
                    });
                }
            }
            if (!valid) {
                return;
            }

            $('#checklist_loader'+ checklistStageId.toString()).addClass('loader');
            $('#lead-form-submit' + checklistStageId.toString()).prop('disabled', true);
            var formData = new FormData(this);

            $.ajax({
                url: "<?php echo admin_url('leads/checklist/' . $lead_id); ?>",
                type: "POST",
                data: formData,
                contentType: false,
                processData: false,
                success: function (response) {
                    $('#checklist_loader' + checklistStageId.toString()).removeClass('loader');
                    $('#lead-form-submit' + checklistStageId.toString()).prop('disabled', false);
                    // var sameGroup = <?php echo $samegroup ? 'true' : 'false'; ?>;
                    var sameGroup = <?php echo $samegroup; ?>;
                    // Create a success alert and append it to the body 
                    alert_float('success', 'ID added successfully.');
                    // Remove the success alert after a delay
                    console.warn('borrower samgroup check ' + sameGroup);
                    if(sameGroup != 2){
                        setTimeout(function () {
                            _lead_init_data(response, response.id);
                            $('a[href="' + response.leads_status + '"]').tab('show');
                        }, 500);
                    }else{
                        setTimeout(function() {
                            _lead_init_data(response, response.id);
                            var anchor = document.querySelector('a[href="#file_opened"]');
                            if (anchor) {
                                anchor.click();
                            }
                            $('a[href="#file_opened"]').tab('show');
                        }, 500);
                    }
                },
                error: function (xhr, status, error) {
                    $('#checklist_loader' + checklistStageId.toString()).removeClass('loader');
                    $('#lead-form-submit' + checklistStageId.toString()).prop('disabled', false);
                    console.log(error);
                    // Handle the error as needed
                }
            });
        });
    });
</script>