
<style>
    .checkbox label, .radio label {
        margin-right: 10px !important;
    }
</style>

<?= $this->element('page_top', array('title' => __('Add'), 'permissions' => $user_permission)); ?>
    <div class="box box-primary">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                <div class="col-md-8">
                    <div id="basic_info_div">
                        <?= $this->Form->create(null, ['id' => 'report_format_variable_form', 'class'=> 'input-form-section']); ?>
                        <div class="col-md-9 col-sm-9 col-xs-9 text-left">

                            <div class="form-group row <?php if(isset($errors['report_format_id'])){ echo 'has-error';} ?>">
                                <label class="control-label col-lg-3 required">Report Format</label>
                                <div class="col-lg-9">
                                    <?php echo $this->Form->control('report_format_id', ['label' =>false,['class' => 'required' ], 'type' => 'select' , 'options'=>$report_formats, 'empty' => 'Select One', 'class' => 'form-control', 'required' => false]); ?>
                                    <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['report_format_id']['_empty'])) {
                                                            echo $errors['report_format_id']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                                </div>
                            </div>
                            <div class="form-group row <?php if(isset($errors['variable_name'])){ echo 'has-error';} ?>">
                                <label class="control-label col-lg-3 required">Variable Name</label>
                                <div class="col-lg-9">
                                    <?php echo $this->Form->input('variable_name', ['label' =>false,['class' => 'required' ], 'placeholder'=>'Variable Title', 'class' => 'form-control', 'required' => false]); ?>
                                    <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['variable_name']['_empty'])) {
                                                            echo $errors['variable_name']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                                </div>
                            </div>
                            <div class="form-group row <?php if(isset($errors['table_name'])){ echo 'has-error';} ?>">
                                <label class="control-label col-lg-3 required">Table Name</label>
                                <div class="col-lg-9">
                                    <?php echo $this->Form->control('table_name', ['label' =>false,['class' => 'required' ],'id'=>'table_name' ,'type' => 'select' , 'options'=> ALL_REPORT_TABLES, 'empty' => 'Select One', 'class' => 'form-control', 'required' => false]); ?>
                                    <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['table_name']['_empty'])) {
                                                            echo $errors['table_name']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                                </div>
                            </div>
                            <div class="form-group row <?php if(isset($errors['column_name'])){ echo 'has-error';} ?>">
                                <label class="control-label col-lg-3 required">Column Name</label>
                                <div class="col-lg-9">
                                    <?php echo $this->Form->control('column_name', ['label' =>false,['class' => 'required' ],'id'=>'column_name' ,'type' => 'select' ,  'empty' => 'Select One', 'class' => 'form-control', 'required' => false]); ?>
                                    <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['column_name']['_empty'])) {
                                                            echo $errors['column_name']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                                </div>
                            </div>
                            <div class="form-group row <?php if(isset($errors['data_type'])){ echo 'has-error';} ?>">
                                <label class="control-label col-lg-3 required">Data Type</label>
                                <div class="col-lg-9">
                                    <?php echo $this->Form->input('data_type', ['label' =>false,['class' => 'required' ], 'placeholder'=>'Data Type', 'class' => 'form-control', 'required' => false]); ?>
                                    <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['data_type']['_empty'])) {
                                                            echo $errors['data_type']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                                </div>
                            </div>

                            <div class="form-group row <?php if(isset($errors['is_mandatory'])){ echo 'has-error';} ?>">
                                <label class="control-label col-lg-3 required">Is Mandatory</label>
                                <div class="col-lg-9">
                                    <?php echo $this->Form->control('is_mandatory', ['type' => 'radio', 'label' => false, ['class' => 'required' ] ,'options' => IS_MANDATORY, 'default'=>'1', 'class' => 'form-check-input', 'required' => true]); ?>

                                    <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['is_mandatory']['_empty'])) {
                                                            echo $errors['is_mandatory']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                                </div>
                            </div>

                            <div class="form-group row ">
                                <label class="control-label col-lg-3"></label>
                                <div class="col-lg-9">
                                    <?= $this->Form->button(__('Submit'), ['class' => 'btn btn-success tab3Save pull-left btn-grid btn-flat','id'=>'submit']) ?>
                                    <a href="<?php echo $this->Url->build('/report-format-variable'); ?>"  class="btn btn-default tab3Save pull-left btn-grid btn-flat btn-orange-color" id="default-btn">
                                        <?php echo __('Back'); ?>
                                    </a>
                                </div>
                            </div>
                        </div>




                        <?= $this->Form->end() ?>
                        <!-- /.form-group -->
                    </div>

                    <!-- /.form-group -->


                </div>

                <!-- /.row -->
            </div>

        </div>
    </div>


    <script>

        $('#report_format_variable_form').validate({
            rules:{
                'report_format_id' : 'required',
                'variable_name':'required',
                'table_name':'required',
                'column_name':'required',
                'data_type':'required',
                'is_mandatory':'required'
            },
            messages: {
                'variable_name':{
                    required: "Please enter variable title"
                },
                'report_format_id':{
                    required: "Please select report format"
                },
                'table_name':{
                    required: "Please select table name"
                },
                'column_name':{
                    required: "Please select column name"
                },
                'data_type':{
                    required: "Please enter variable data type"
                },
                'is_mandatory':{
                    required: "Please enter mandatory variable"
                }

            },
            highlight: function(element) {
                $(element).parent().addClass("has-error");
            },
            submitHandler: function(form) {
                form.submit();
            }
        });
        $(document).ready(function() {
            $('#table_name').change(function () {

                if ($(this).val() != '') {
                    $.ajax({
                        url: '<?php echo $this->Url->build('/report-format-variable/add'); ?>/'+ $(this).val(),
                        type: 'GET',
                        data: $(this).val(),
                        dataType: 'json',
                        success: function (response) {
                            if (response.response != 'error' && response.response != 'not_allowed') {
                                let dropdown = $('#column_name');
                                $.each(response.response, function(key, value) {
                                    dropdown.append($('<option></option>').val(key).text(value));
                                });

                            }
                        },
                        error: function(xhr, status, error) {
                            console.error("Error loading dropdown options:", error);
                        }
                    });
                }
            });
        });



    </script>
