<?php
namespace Bill\Form;

use Zend\Form\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\FileInput;

//use Bill\Validator\UserExistsValidator;

/**
 * This form is used to collect user's email, full name, password and status. The form 
 * can work in two scenarios - 'create' and 'update'. In 'create' scenario, user
 * enters password, in 'update' scenario he/she doesn't enter password.
 */
class BillForm extends Form
{
    /**
     * Scenario ('create' or 'update').
     * @var string 
     */
    private $scenario;
    
    /**
     * Entity manager.
     * @var Doctrine\ORM\EntityManager 
     */
    private $entityManager = null;
    
    /**
     * Current user.
     * @var User\Entity\User 
     */
    private $bill = null;
    
    /**
     * Constructor.     
     */
    public function __construct($scenario = 'create', $entityManager = null, $bill = null, $services)
    {
        // Define form name
        $this->services = $services;
		parent::__construct('Bill-form');
     
        // Set POST method for this form
        $this->setAttribute('method', 'post');
		
		// Set binary content encoding
        $this->setAttribute('enctype', 'multipart/form-data');
        
        // Save parameters for internal use.
        $this->scenario = $scenario;
        $this->entityManager = $entityManager;
        $this->bill = $bill;
        
        $this->addElements();
        $this->addInputFilter();          
    }
    
    /**
     * This method adds elements to form (input fields and submit button).
     */
    protected function addElements() 
    {
        // Add "email" field
        $this->add([            
            'type'  => 'date',
            'name' => 'date',
            'options' => [
                'label' => 'Data',
            ],
        ]);
        
        // Add "full_name" field
        $this->add([            
            'type'  => 'text',
            'name' => 'value',            
            'options' => [
                'label' => 'Valor',
            ],
        ]);
        
        
        // Add "status" field
		//var_dump($this->getOptionsForSelect());exit;
        $this->add([            
            'type'  => 'Zend\Form\Element\Select',
            'name' => 'serviceid',
            'options' => [
                'label' => 'Servicos',
				'empty_option' => 'Servicos',
				'value_options' => $this->getOptionsForSelect(),
            ],
        ]);
	
		// Add "file" field
		$this->add([
            'type'  => 'file',
            'name' => 'name',
            'attributes' => [               
                'id' => 'file'
            ],
            'options' => [
                'label' => 'Image file',
            ],
        ]);	
		

        
        // Add the Submit button
        $this->add([
            'type'  => 'submit',
            'name' => 'submit',
            'attributes' => [                
                'value' => 'Create'
            ],
        ]);
    }
    
	
	 public function getOptionsForSelect()
    {
        $selectData = array();

        foreach ($this->services as $service) {
            $selectData[$service->getId()] = $service->getName();
        }
        return $selectData;
    }


    /**
     * This method creates input filter (used for form filtering/validation).
     */
    private function addInputFilter() 
    {
        // Create main input filter
        $inputFilter = new InputFilter();        
        $this->setInputFilter($inputFilter);
                
      
        
        // Add input for "name" field
        $inputFilter->add([
                'name'     => 'date',
                'required' => true,              
                'name' => 'value',
				'required' => true,
            ]);
		
		// Add validation rules for the "file" field	 
        $inputFilter->add([
                'type'     => FileInput::class,
                'name'     => 'name',
                'required' => true,                           
                'validators' => [
                    ['name'    => 'FileUploadFile'],
                    [
                        'name'    => 'FileMimeType',                        
                        'options' => [                            
                            'mimeType'  => ['image/jpeg', 'image/png']
                        ]
                    ],
                    ['name'    => 'FileIsImage'],                          
                    [
                        'name'    => 'FileImageSize',                        
                        'options' => [                            
                            'minWidth'  => 128,
                            'minHeight' => 128,
                            'maxWidth'  => 4096,
                            'maxHeight' => 4096
                        ]
                    ],                    
                ],
                'filters'  => [                    
                    [
                        'name' => 'FileRenameUpload',
                        'options' => [  
                            'target'=>'./data/upload',
                            'useUploadName'=>true,
                            'useUploadExtension'=>true,
                            'overwrite'=>true,
                            'randomize'=>false
                        ]
                    ]
                ],     
            ]);         
        
       
    }           
}