<?php
/**
 * 易联云K4打印机
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 * http://doc2.10ss.net/336832
 */
namespace Common\Vendor\Printtpl;
class Yilianyun{
    private $config=array('wrap'=>"\n",'symbol'=>'&nbsp;&nbsp;');
    private $allowtype=array('text','title','line','table','qrcode');
    private $datalist=array();
    public function __construct($config) {
        $this->config = !empty($this->config) && !empty($config)? array_merge($this->config, $config):!empty($config)?$config:$this->config;
    }
    public function setConfig($config){
        if(empty($config) && !is_array($config))return false;
        $this->config = !empty($this->config)? array_merge($this->config, $config):$config;
    }
    /**
     * 编译模板
     * @param $AppName 应用名称
     */
    public function fetch($array='',$data=''){
        if(empty($array))return false;
        $tpl=array();
        $this->datalist=$data;
        foreach($array as $v){
            if(in_array($v['type'],$this->allowtype)){
                $class=$v['type'].'Type';$tpl[]=$this->$class($v);
            }
        }
        $tpl=implode('',$tpl);
        return $this->replaceTags($tpl);
    }
     /**
     * 打印标题
     */
    private function titleType($data){
        if(!$this->checkvalue($data))return ;
        $str='<FB><FS>';
        if($data['align']=='center')$str.="<center>";
        if($data['align']=='right')$str.="<right>";
        $str.= $data['data'];        
        if($data['align']=='center')$str.="</center>";
        if($data['align']=='right')$str.="</right>";
        $str.='</FS></FB>'.$this->config['wrap'];
        return $str;
    }
    /**
     * 打印文字
     */
    private function textType($data){
        if(!$this->checkvalue($data))return ;
        $str='';
        if($data['align']=='center')$str.="<center>";
        if($data['align']=='right')$str.="<right>";
        $str.=$data['data'];
        if($data['align']=='center')$str.="</center>";
        if($data['align']=='right')$str.="</right>";
        $str.= $this->config['wrap'];
        return $str;
    }
    /**
     * 强制换行
     */
    private function lineType($data){
        $str='';$num=intval($data['data']);if(empty($num))$num=1;
        for($i=0;$i<$num;$i++){
            $str.=$this->config['wrap'];
        }
        return $str;
    }
    /**
     * 打印表格
     */
    private function tableType($data){
        if(!$this->checkvalue($data))return ;
        $str='<table>';
        if($data['title']){
            if(!is_array($data['title']))$data['title']=explode(',',$data['title']);
            $str.='<tr><td>'.implode('</td><td>',$data['title']).'</td></tr>'.$this->config['wrap'];
        }
        foreach($this->datalist[$data['data']] as $v){
            $arr=array();
            foreach($data['field'] as $val){
                if(!empty($data['oneline']) && $val==$data['oneline']){
                    $oneline=$v[$val];
                }else {
                    $arr[]=$v[$val];
                }                
            }
            if($oneline){
                if($data['onelinewith'] && !empty($v[$data['onelinewith']])){
                    $oneline.='('.$v[$data['onelinewith']].')';
                }
                $str.='<tr><td>'.$oneline.'</td></tr>';
            }
            $str.='<tr><td>'.implode('</td><td>',$arr).'</td></tr>';
        }
        $str.='</table>';
        return $str;
    }
    /**
     * 打印二维码
     */
    private function qrcodeType($data){
        if(!$this->checkvalue($data))return ;
        return '<center><QR>'.$data['data'].'</QR></center>'.$this->config['wrap'];
    }
    //替换标签
    private function replaceTags($data){
        $rep=array();
        foreach($this->datalist as $k => $v){
            if(is_array($v))continue;
            $rep['['.$k.']']=$v;
        }
        if(!empty($rep)){$data=strtr($data,$rep);}
        return $data;
    }
    //检查是否为空值
    private function checkvalue($data){
        if(empty($data['checkvalue']))return true;
        $field=$data['checkvalue'];
        $type=$data['checkvalue_type'];
        if(empty($type))$type='number';
        if(!empty($field)){
            if(empty($this->datalist[$data['checkvalue']]))return false;
            $datavalue=$this->datalist[$data['checkvalue']];
            if(is_array($datavalue)){
                $type=='string';
            }else{
                $datavalue=trim(str_replace('￥','',$this->datalist[$data['checkvalue']]));
            }
            $checkval=$type=='string'?$datavalue:floatval($datavalue);
            if(empty($checkval))return false;
        }
        return true;
    }
}
?>