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

?>

    <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">
                        <div class="col-md-6 col-xs-6 tile">
                            <div class="icon-frame col-md-2" id="light">
                                <i class="fa fa-plus-circle" style="color:white"></i>
                            </div>
                            <h3 class="box-title no-margin" style="line-height: 3;float: left;"><?php echo __('&nbspAdd'); ?></h3>
                        </div>

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



                            <div class="row">

                                <div class="col-md-12 text-right" style="line-height: 5;">
                                    <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>

                                </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">
                    <div id="basic_info_div">
                        <?= $this->Form->create(null, ['id' => 'report_format_generation_history_form', 'class'=> 'input-form-section']); ?>
                        <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->input('report_format_id', ['label' =>false,['class' => 'required' ], 'type' => 'select' , 'options'=>$report_formats, 'empty' => 'Select One', 'class' => 'form-control',  'id' => 'report_format_id', 'required' => true]); ?>
                                    <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'=>'Name', '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 ">
                                <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>

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

                        <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','required' => false,'name' => 'content' ,'disabled'=>$disabled]); ?>
                            </div>
                        </div>
                    </div>

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

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

        </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'

            },
            messages: {
                'name':{
                    required: "Please enter name"
                },
                'report_format_id':{
                    required: "Please select report format"
                },
                'receiver_emails':{
                    required: "Please enter Receiver Email"
                }

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



    </script>
