<?php
$menu_modules = unserialize($this->request->session()->read('modules'));
$statues = json_decode(MEMBER_STATUS);

?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-body">
                <div class="row" style="text-align:left;" >
                    <div class="col-md-6 col-xs-6 tile">
                        <div class="icon-frame col-md-2" id="light">
                            <i class="fa fa-folder-open-o" style="color:white"></i>
                        </div>
                        <h3 class="box-title no-margin" style="line-height: 3;float: left;"><?php echo __('&nbspEdit'); ?></h3>
                    </div>

                    <div class="col-md-6">

                        <div class="row">

                            <div class="col-md-12 text-right" style="line-height: 5;">
                                <?php
                                if (array_key_exists("add", $module_pages)):
                                    $page_id = $module_pages["index"];
                                    foreach ($user_permission as $permission):
                                        if ($permission['page_id'] == $page_id):
                                            ?>

                                            <a href="<?php echo $this->Url->build('/report-format-generation-history'); ?>"  class="btn btn-success btn-flat btn-grid">
                                                <i class="fa fa-list"></i>
                                                <?php echo __('LIST'); ?>
                                            </a>

                                            <?php
                                            break;
                                        endif;
                                    endforeach;
                                endif;
                                ?>

                            </div>
                        </div>


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

                <div id="basic_info_div">

                    <div class="col-md-6 col-sm-12 col-xs-12 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['name'])){ echo 'has-error';} ?>">
                            <label class="control-label col-lg-3 required">Name</label>
                            <div class="col-lg-9">
                                <?php echo $this->Form->input('name', ['label' =>false,['class' => 'required' ], 'placeholder'=>'Variable Title', 'class' => 'form-control', 'required' => false]); ?>
                                <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['name']['_empty'])) {
                                                            echo $errors['name']['_empty'];
                                                        }
                                                        ?>
                                        </span>
                            </div>
                        </div>
                        <div class="form-group row <?php if(isset($errors['receiver_emails'])){ echo 'has-error';} ?>">
                            <label class="control-label col-lg-3 required">Receiver Emails</label>
                            <div class="col-lg-9">
                                <?php echo $this->Form->input('receiver_emails', ['label' =>false,['class' => 'required' ], 'placeholder'=>'Receiver Emails', 'class' => 'form-control', 'required' => false]); ?>
                                <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['receiver_emails']['_empty'])) {
                                                            echo $errors['receiver_emails']['_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_generation_history->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-generation-history'); ?>"  class="btn btn-default tab3Save pull-left btn-grid btn-flat btn-orange-color" id="default-btn">
                                    <?php echo __('Back'); ?>
                                </a>
                            </div>
                        </div>



                    </div>

                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="form-group row">
                            <?= $this->Form->textarea('content', ['placeholder' => __('Content'),'label'=>false,'style'=>'height:300px','class' => 'form-control editable required', 'type' => 'text','id'=>'content','value' => $report_format->content, 'required' => false,'name' => 'content' ,'disabled'=>$disabled]); ?>
                        </div>
                    </div>
                    <!-- /.form-group -->
                </div>
                <?= $this->Form->end() ?>
                <!-- /.form-group -->


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

    </div>
</div>
<?php echo $this->element('tinymce_js'); ?>
<script>
    $('#report_format_id').change(function () {
        tinymce.get('content').setContent('');
        if ($(this).val() != '') {
            $.ajax({
                url: '<?php echo $this->Url->build('/report-format/view'); ?>/'+ $(this).val(),
                type: 'GET',
                data: $(this).val(),
                dataType: 'json',
                success: function (response) {
                    if (response != 'error' && response != 'not_allowed') {
                        tinymce.get('content').setContent(response.response.content);

                    }
                },
                error: function (data) {

                }
            });
        }
    });
    $('#report_format_generation_history_form').validate({
        rules:{
            'report_format_id' : 'required',
            'name':'required',
            'receiver_emails':'required',
            'status':'required'
        },
        messages: {
            'name':{
                required: "Please enter name"
            },
            'report_format_id':{
                required: "Please select report format"
            },
            'receiver_emails':{
                required: "Please enter receiver emails"
            },
            'status':{
                required: "Please select status"
            }

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

</script>

