<?php
/**
 * 商米POS v1打印机
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Common\Vendor\Printtpl;
class Shangmi{
    private $config=array('wrap'=>"\n",'symbol'=>'&nbsp;&nbsp;');
    private $allowtype=array('text','title','line','table','qrcode');
    private $datalist=array();
    private $josnarray=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=json_encode($this->jsonarray);
        return $this->replaceTags($tpl);
    }
    /**
     * 打印标题
     */
    private function titleType($data){
        if(!$this->checkvalue($data))return ;
        if($data['align']=='left')$this->jsonarray[]=array("rowtype"=>"setAlignment","alignment"=>0);
        if($data['align']=='center')$this->jsonarray[]=array("rowtype"=>"setAlignment","alignment"=>1);
        if($data['align']=='right')$this->jsonarray[]=array("rowtype"=>"setAlignment","alignment"=>2);
        $this->jsonarray[]=array("rowtype"=>"printTextWithFont","text"=>$data['data'].$this->config['wrap'],"fontsize"=>46);
    }
    /**
     * 打印文字
     */
    private function textType($data){
        if(!$this->checkvalue($data))return ;
        if($data['align']=='left')$this->jsonarray[]=array("rowtype"=>"setAlignment","alignment"=>0);
        if($data['align']=='center')$this->jsonarray[]=array("rowtype"=>"setAlignment","alignment"=>1);
        if($data['align']=='right')$this->jsonarray[]=array("rowtype"=>"setAlignment","alignment"=>2);
        $this->jsonarray[]=array("rowtype"=>"printText","text"=>$data['data'].$this->config['wrap']);
    }    
    /**
     * 强制换行
     */
    private function lineType($data){
        $num=intval($data['data']);if(empty($num))$num=1;
        $this->jsonarray[]=array("rowtype"=>"lineWrap","n"=>$num);
    }
    /**
     * 打印表格
     */
    private function tableType($data){
        if(!$this->checkvalue($data))return ;
        if($data['oneline']){
            foreach($data['field'] as $k=> $val){
                if($val==$data['oneline'])$onekey=$k;
            }
        }
        if($data['title']){
            if(!is_array($data['title']))$data['title']=explode(',',$data['title']);
            $title_width=$title_align=array();
            $width=9;$align=0;$onekey=-1;
            if($this->config['type_id']==2 && $this->config['print_tpl_id']==1){$width=15;}
            foreach($data['title'] as $v){$title_width[]=$width;$title_align[]=$align;}
            if(is_numeric($onekey) && $onekey>=0){
                $this->jsonarray[]=array("rowtype"=>"printText","text"=>$data['title'][$onekey].$this->config['wrap']);
                unset($data['title'][$onekey],$title_width[$onekey],$title_align[$onekey]);
                $data['title']=array_values($data['title']);
                $title_width=array_values($title_width);
                $title_align=array_values($title_align);
            }
                $title_width[0]=15;

            $this->jsonarray[]=array("rowtype"=>"printColumnsText","colsTextArr"=>$data['title'],"colsWidthArr"=>$title_width,"colsAlign"=>$title_align);
        }
        foreach($this->datalist[$data['data']] as $v){
            $arr=$arr_width=$arr_align=array();
            $width=9;$align=0;
            if($this->config['type_id']==2 && $this->config['print_tpl_id']==1){$width=15;}
            foreach($data['field'] as $val){$arr[]=$v[$val];$arr_width[]=$width;$arr_align[]=$align;}
            if(is_numeric($onekey) && $onekey>=0){
                $onekeyvalue=$arr[$onekey];
                if($data['onelinewith'] && !empty($v[$data['onelinewith']])){
                    $onekeyvalue.='('.$v[$data['onelinewith']].')';
                }
                $this->jsonarray[]=array("rowtype"=>"printText","text"=>$onekeyvalue.$this->config['wrap']);
                unset($arr[$onekey],$arr_width[$onekey],$arr_align[$onekey]);
                $arr=array_values($arr);
                $arr_width=array_values($arr_width);
                $arr_align=array_values($arr_align);
            }

                $arr_width[0]=15;
            $this->jsonarray[]=array("rowtype"=>"printColumnsText","colsTextArr"=>$arr,"colsWidthArr"=>$arr_width,"colsAlign"=>$arr_align);
        }
    }
    /**
     * 打印二维码
     */
    private function qrcodeType($data){
        if(!$this->checkvalue($data))return ;
        $this->jsonarray[]=array("rowtype"=>"printQRCode","data"=>$data['data'],"size"=>240);
    }
    //替换标签
    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;
    }
}
?>