<?php

namespace Mshop\Lib;

use Dever;

class Feieyun
{
    private $config = array
    (
        //飞鹅云后台注册账号
        'user' => 'liuxuemei@zsma.com.cn',
        //飞鹅云后台注册账号后生成的UKEY
        'ukey' => 'pMsXhZWu5F65MYyV',
        //host地址
        'host' => 'http://api.feieyun.cn:80/Api/Open/',
        //可打印的长度
        'length' => 32,
    );
    private $text = '';
    # 生成参数
    public function param($api, $send = array())
    {
        $time = time();
        $param['user'] = $this->config['user'];
        $param['stime'] = $time;
        $param['sig'] = sha1($this->config['user'] . $this->config['ukey'] . $time);
        $param['apiname'] = $api;
        $param = array_merge($param, $send);
        return $param;
    }

    # 发送
    public function send($api, $send)
    {
        $url = $this->config['host'];
        $param = $this->param($api, $send);
        $result = Dever::curl($url, $param, 'post');
        $result = Dever::json_decode($result);
        if (isset($result['data']) && $result['data'] && isset($result['data'])) {
        	return $result['data'];
        }
        if (isset($result['msg'])) {
        	return $result['msg'];
        }
        return 'error';
    }

    # 添加打印机
    public function add($data)
    {
      	$content = array();
      	foreach ($data as $k => $v) {
      		$content[] = $v['number'] . '#' . $v['key'] . '#' . $v['name'] . '#' . $v['phonenum'];
      	}
      	$content = implode('\n', $content);
      	$send['printerContent'] = $content;
      	$result = $this->send('Open_printerAddlist', $send);
        return $result;
    }

    # 编辑打印机
    public function edit($sn, $name, $phonenum)
    {
      	$send['sn'] = $sn;
      	$send['name'] = $name;
      	if ($phonenum && $phonenum != 'null') {
      		$send['phonenum'] = $phonenum;
      	}
      	
      	$result = $this->send('Open_printerEdit', $send);
        return $result;
    }

    # 删除打印机
    public function del($data)
    {
      	$content = array();
      	foreach ($data as $k => $v) {
      		$content[] = $v['number'];
      	}
      	$content = implode('-', $content);
      	$send['snlist'] = $content;
      	$result = $this->send('Open_printerDelList', $send);
        return $result;
    }

    # 打印订单
    public function printOrder($sn, $content, $times)
    {
    	$send['sn'] = $sn;
    	$send['content'] = $content;
    	$send['times'] = $times;
      	$result = $this->send('Open_printMsg', $send);
      	return $result;
    }

    # 打印标签机订单
    public function printLabelOrder($sn, $content, $times)
    {
    	$send['sn'] = $sn;
    	$send['content'] = $content;
    	$send['times'] = $times;
      	$result = $this->send('Open_printLabelMsg', $send);
      	return $result;
    }

    # 清空待打印订单接口
    public function delOrder($sn)
    {
    	$send['sn'] = $sn;
      	$result = $this->send('Open_delPrinterSqs', $send);
      	return $result;
    }

    # 查询订单是否打印成功接口
    public function order($id)
    {
    	$send['orderid'] = $id;
      	$result = $this->send('Open_queryOrderState', $send);
      	return $result;
    }

    # 查询指定打印机某天的订单统计数接口
    public function stat($sn, $date)
    {
		$send['sn'] = $sn;
		$send['date'] = $date;
		$result = $this->send('Open_queryOrderInfoByDate', $send);
		return $result;
    }

    # 获取某台打印机状态接口
    public function status($sn)
    {
		$send['sn'] = $sn;
		$result = $this->send('Open_queryPrinterStatus', $send);
		return $result;
    }

    public function getText()
    {
        return $this->text;
    }

    public function text($text = '', $align = '', $br = 1)
    {
        if ($align) {
        	$text = '<'.$align.'>'.$text.'</'.$align.'>';
        }
        $this->text .= $text;
        if ($br > 0) {
        	$this->br($br);
        }
        return $this;
    }

    public function left($text)
    {
        $this->text($text);
        return $this;
    }

    public function right($text)
    {
        $this->text($text, 'RIGHT');
        return $this;
    }

    public function center($text)
    {
        $this->text($text, 'C');
        return $this;
    }

    public function hr($str = '-', $name = '')
    {
        $text = str_pad($name, $this->config['length'], $str, STR_PAD_BOTH);
        $this->text($text);
        return $this;
    }

    public function br($num = 1)
    {
        for ($i = 0; $i < $num; $i++) {
            $this->text .= '<BR>';
        }
        return $this;
    }

    # 左右结构
    public function lr($s1, $s2)
    {
    	$l1 = strlen(Dever::convert($s1));
        $n = '';
        $max = $this->config['length'] - 6;
        if ($l1 > $max) {
            $max = $max/2;
            $t = mb_substr($s1, 0, $max, 'utf-8');
            $n = mb_substr($s1, $max, $l1, 'utf-8');
            $s1 = $t;
            $l1 = strlen(Dever::convert($s1));
        }

        $l2 = strlen(Dever::convert($s2));
        $l = $this->config['length'] - ($l1+$l2);
        $k = '';
        for($i = 0; $i < $l; $i++) {
            $k .= ' ';
        }
        $text = $s1 . $k . $s2;
        $this->text($text);
        return $n;
    }

    # 左中右结构
    public function lcr($s1, $s2, $s3, $s1_s2 = 80, $s2_s3 = 20)
    {
        $s1_s2 = round(($s1_s2/100)*$this->config['length']);
        $s2_s3 = round(($s2_s3/100)*$this->config['length']);

        $l1 = strlen(Dever::convert($s1));
        $n = '';
        $max = $s1_s2 - ($s1_s2 - $s2_s3)/2;
        if ($l1 > $max) {
            $max = $max/2;
            $t = mb_substr($s1, 0, $max, 'utf-8');
            $n = mb_substr($s1, $max, $l1, 'utf-8');
            $s1 = $t;
            $l1 = strlen(Dever::convert($s1));
        }

        $l2 = strlen(Dever::convert($s2));
        $l3 = strlen(Dever::convert($s3));
        $l = $s1_s2 - ($l1+$l2);
        $k = '';
        for($i = 0; $i < $l; $i++) {
            $k .= ' ';
        }
        $text = $s1 . $k . $s2;
        
        $l = $s2_s3 - $l3;
        $k = '';
        for($i = 0; $i < $l; $i++) {
            $k .= ' ';
        }
        $text .= $k . $s3;
        $this->text($text);
        return $n;
    }

    # 表格
    public function table($head, $body, $name, $method = 'lcr')
    {
        $this->hr();
        if ($name) {
            $this->center($name);
        }
        call_user_func_array(array($this, $method), $head);
        $next = '';
        foreach ($body as $k => $v) {
            $next = call_user_func_array(array($this, $method), $v);
            if ($next) {
                $this->left($next);
            }
        }

        $this->hr();
    }
}