<?php namespace Eae\Backoffice\Generic\Fields;

use Eae\Support\Contracts\ListableFieldInterface;
use Illuminate\Database\Eloquent\Builder;
use LaravelLocalization;
use View;
use Input;

class CheckBoxField extends BaseField implements ListableFieldInterface{
    
    public function __construct($config){
        unset($config["search"]);
        parent::__construct($config); 
    }

    public function buildSelect(Builder $query){
        if($this->isMultiLanguage()){
            $tablePrefix = $query->getModel()->languages()->getRelated()->getTable();        
        }else{
            $tablePrefix = $query->getModel()->getTable();    
        }
        
        $query->addSelect("{$tablePrefix}.{$this->name}");
        return $query;    
    }
    
    public function buildWhere(Builder $query, $searchPhrase){
        return $query;
    }
    
    public function buildOrder(Builder $query, $sortOrder = "asc"){
        if($this->isMultiLanguage()){
            $tablePrefix = $query->getModel()->languages()->getRelated()->getTable();        
        }else{
            $tablePrefix = $query->getModel()->getTable();    
        }

        $query->orderBy("{$tablePrefix}.{$this->getName()}", $sortOrder); 
        return $query;
    }
    
    public function toGridHeader(){
        return '<th data-column-id="' . $this->getName() . '" data-formatter="yesNo">' . ucfirst($this->getLabel()) . '</th>';    
    }
    
    public function toForm($model, $enabled = true, $hidden = false){
        $html = "";
        if($this->isMultiLanguage()){
            $locales = LaravelLocalization::getSupportedLocales();
            $defaultLocale = LaravelLocalization::getDefaultLocale();
            
            foreach($locales as $locale => $localeData){
                $fieldName = $this->getName();
                $name = $fieldName . "[{$locale}]";
                $id = $fieldName . "_{$locale}";
                $errorKey = $fieldName . "." . $locale;
                $value = null;
                
                if($model->exists){
                    $languageModel = $model->languages->findByLocale($locale);
                    
                    if(!empty($languageModel)){
                        $value = $languageModel->$fieldName;
                    }
                }
                
                $html .= View::make(
                    "admin.generic.components.forms.checkbox", 
                    array(
                        "id"            => $id,
                        "errorKey"      => $errorKey,
                        "name"          => $name,
                        "label"         => $this->getLabel(),
                        "value"         => $value,
                        "language"      => $locale,
                        "defaultLang"   => $defaultLang,
                        "hide"          => ($hidden || $locale != $defaultLocale),
                        "enabled"       => $enabled,
                        "parentHidden"  => ($hidden && !empty($this->parent)),
                        "parent"        => $this->parent,
                        "info"          => $this->getInfo()    
                    ));    
            }    
        }else{
            $fieldName = $this->getName();
            $value = null;
            
            if($model->exists){
                $value = $model->$fieldName;    
            }
            $html .= View::make(
                    "admin.generic.components.forms.checkbox", 
                    array(
                        "id"            => $this->getName(),
                        "errorKey"      => $this->getName(),
                        "name"          => $this->getName(),
                        "label"         => $this->getLabel(),
                        "value"         => $value,
                        "hide"          => $hidden,
                        "enabled"       => $enabled,
                        "parentHidden"  => ($hidden && !empty($this->parent)),
                        "parent"        => $this->parent,
                        "info"          => $this->getInfo()    
                    ));      
        }
        
        return $html;
    }
    
    public function getValidation(){
        $validation = parent::getValidation();
        $rule = "required|in:0,1";
        
        if(empty($validation)){
            return $rule;
        }else{
            return $validation . "|" . $rule;    
        }
        
    }
    
    public function getValidationData($copyAllLang = false){
        $validation = parent::getValidationData(false);
        $fieldName = $this->getName();
        $values = Input::get($fieldName);
        $finalValues = null;
        
        if($this->isMultiLanguage()){
            $locales = LaravelLocalization::getSupportedLocales();
            $finalValues = array();
            if(empty($values)){
                $values = array();    
            }
            
            foreach($locales as $locale => $localeData){
                if(isset($values[$locale])){
                    $finalValues[$locale] = $values[$locale];    
                }else{
                    $finalValues[$locale] = '0';
                }    
            }
            
            if($copyAllLang){
                $defaultLocale = LaravelLocalization::getDefaultLocale();
                
                foreach($locales as $locale => $localeData){
                    if($locale == $defaultLocale){
                        continue;
                    }  

                    if(empty($finalValues[$locale])){
                        $finalValues[$locale] = $finalValues[$defaultLocale];        
                    }
                }        
            }
            
            $validation["values"][$fieldName] = $finalValues;
        }else{
            if(empty($values)){
                $validation["values"][$fieldName] = '0';    
            }else{
                $validation["values"][$fieldName] = $values;
            }       
        }
        
        return $validation;
    }
 
}
  

