<?php
$statues = json_decode(MEMBER_STATUS);

?>

<?= $this->element('page_top', array('title' => __('Edit'), 'permissions' => $user_permission)); ?>

<div class="box box-primary">
    <!-- /.box-header -->
    <div class="box-body">
        <div class="row">
            <div class="col-md-12">
                <?= $this->Form->create($report_format_variable, ['id' => 'report_format_variable_form', 'class'=> 'input-form-section']); ?>

                <div id="basic_info_div">

                    <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'=>$report_format_variable->is_mandatory, '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 <?php if(isset($errors['status'])){ echo 'has-error';} ?>">
                            <label class="control-label col-lg-3 required">Status</label>
                            <div class="col-lg-9">
                                <?php echo $this->Form->control('status', ['label' =>false,['class' => 'required' ], 'type' => 'select' , 'options'=>$statues, 'empty' => 'Select One', 'default'=> $report_format_variable->status ,'class' => 'form-control', 'required' => false]); ?>
                                <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['status']['_empty'])) {
                                                            echo $errors['status']['_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>

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


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

    </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',
            'status':'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"
            },
            'status':{
                required: "Please select status"
            }

        },
        highlight: function(element) {
            $(element).parent().addClass("has-error");
        },
        submitHandler: function(form) {
            form.submit();
        }
    });

    $('#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>

