 <?php

// if ($this->data != '') {
//
// 	print_r($this->data);
//
//
// } else {

// now replacing formCollection :
// echo $this->formCollection($form);
//we call the Form's prepare() method to prepare the form for rendering. Please note that calling this method is very important. If you forget to do that, there may be some undesired rendering problems.
 $form->prepare();

 $product = $form->get('new-product');

 $product->get('id')->setAttributes([
     'class'=>'form-control',
     'placeholder'=>'01'
 ]);

 $product->get('title')->setAttributes([
     'class'=>'form-control',
     'placeholder'=>'Type a title'
 ]);

 $product->get('category')->setAttributes([
     'class'=>'form-control',
     'rows'=>2
 ]);

 $collection = $product->get('items');

 $form->get('submit')->setAttributes(['class'=>'btn btn-primary']);
 ?>

<h1>IndexController::indexAction()</h1>

<p>
  Click on the button to add new rows from fieldset
</p>
    <?= $this->form()->openTag($form); ?>
<div class="row">
  <div class="col-md-6">


    <div class="form-group">
      <?= $this->formLabel($product->get('id')); ?>
      <?= $this->formElement($product->get('id')); ?>
      <?= $this->formElementErrors($product->get('id')); ?>
    </div>

    <div class="form-group">
      <?= $this->formLabel($product->get('title')); ?>
      <?= $this->formElement($product->get('title')); ?>
      <?= $this->formElementErrors($product->get('title')); ?>
    </div>

    <div class="form-group">
      <?= $this->formLabel($product->get('category')); ?>
      <?= $this->formElement($product->get('category')); ?>
      <?= $this->formElementErrors($product->get('category')); ?>
    </div>
  </div>
</div>
<p>
<a class="btn btn-default" id="add" href="#" role="button"><span class="glyphicon glyphicon-plus"></span>&nbsp;Add row</a>
</p>
	<table class="table table-responsive">
		<thead>
		<tr>
			<th>ID</th>
			<th>NAME</th>
			<th>DESCRIPTION</th>
			<th>ATTRIBUTE</th>
                        <th></th>
		</tr>
		</thead>
		<tbody id="fieldset">
	<?php
        $i=1;
	//https://framework.zend.com/manual/2.4/en/modules/zend.form.view.helpers.html
	foreach($collection as $var)
	{

		// you may replace the formInputTd by a forlElement
		// we keep this line as an example of use only
	    echo '<tr>'.$this->formInputTd($var->get('id'));
	    echo '<td>'.$this->formElement($var->get('name')).$this->formElementErrors($var->get('name')).'</td>';
	    echo '<td>'.$this->formElement($var->get('description')).$this->formElementErrors($var->get('description')).'</td>';
	    echo '<td>'.$this->formElement($var->get('attribute')).'</td>';?>
         <?php if($i>2) { ?>
        <?php echo '<td><div class="Xbtn col-md-2" onclick="mcClose($(this).parent())">X</div></td></tr>';
         } ?>
        <?php
        $i++;
	}



	?>

		</tbody>
	</table>

	<?php
	// Render the collection template
	//$this->formCollection()->setTemplateWrapper('<span data-template="%s"></span>');
	//$this->formCollection()->setShouldWrap(false);
	// Change fieldset wrapper to tr rows
	  $this->formCollection()->setWrapper('<tr%4$s>%2$s%1$s%3$s</tr>');

	  //https://github.com/zendframework/zend-form/blob/master/src/View/Helper/FormCollection.php
	  // Set the default input helper as our custom formInputTd instead of formInput
	  $this->formCollection()->setDefaultElementHelper('formRowTd');

	   echo $this->formCollection()->renderTemplate($collection);

    ?>

    <?= $this->formElement($form->get('submit')); ?>
    <?= $this->form()->closeTag(); ?>

<!-- Jquery script to add one more row inside the table -->

<script>

</script>

<?php

 //}

 ?>
