<?php
    
class attr_icon extends attr {
    function getDBType() {
    	return "TEXT";
    }

    function render_input() {
    	
        $html = '';
        if ( $this->getCurVal() ) {
            $html .= '<img class="mx-2" src="'.$this->getCurVal().'" style="height: 24px; vertical-align: middle;">';
        }
        $html .= '<label class="btn btn-gray"><input class="d-none" type="file"  id="img_' . $this->attr_name . '" name="' . $this->attr_name . '"> Выбрать файл</label>';      
        $html .= '<input class="input" type="hidden" name="' . $this->attr_name . '" value="'.$this->getCurVal().'">';      
        if ( $this->getCurVal() ) {
            $html .= '
                <div class="custom-control custom-checkbox">
                  <input class="checkbox custom-control-input" type="checkbox" id="' . $this->attr_name . '_del" name="' . $this->attr_name . '_del" value="1">
                  <label for="' . $this->attr_name . '_del" class="custom-control-label"> Удалить</label>
                </div>';
        }
        return $html;
    }
    
    function lPost() {
    	
    	if ( substr( $this->attr_name, -8 ) == '_preview' ) {
    		return;	
    	}
        
    	$tmpObj = new cobject( '_sys_files' );
    	$file = $_FILES[$this->attr_name];
    	$nn = md5(microtime(true));
    	
    	if ($_POST[$this->attr_name . '_del']) {
	    	$this->obj->set( $this->attr_name, '' );
    	}elseif ($_POST[$this->attr_name]) {
    		$this->obj->set( $this->attr_name, $_POST[$this->attr_name] );
    	}

    	if (!$file['error']) {
	    	 $this->obj->set($this->attr_name, 'data:'.$file['type'].';base64,' . base64_encode(file_get_contents($file['tmp_name'])) );
    	}
    }


    function render_text( $crop = false ) {
    	if ($crop) {
            if ( $this->getCurVal() ) {
    		    $val = $this->getCurVal();	
            } else {
                $val = '';   
            }
    	} else  {
    	    $val = $this->getCurVal();	
    	}
    	return $val;
    }

}

?>