<?php
//echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
$PrinterPiSerial="1";
//$printtxt="test print";
$TOKEN_URL="http://densen.es/app/printonline/add.php?MM_insert=add&sn=" . $PrinterPiSerial . "&print=1&print_txt=" . $printtxt;

//prn_txt('你加速度不test%20print%20from%20\n1234567890123456789012345678901234567890ABCDEFGHI\n----5---10----5---10----5---10----5---10----5---102JPDJNP3W1T4VPECQ215');
//print = print.Replace(vbCrLf, "\n").Replace(CRLF, "\n").Replace(vbCr, "\n").Replace(" ", "%20")
        
//"http://densen.es/app/printonline/add.php?MM_insert=add&sn=0&print=1&print_txt=test%20print%20from%20\n1234567890123456789012345678901234567890ABCDEFGHI\n----5---10----5---10----5---10----5---10----5---102JPDJNP3W1T4VPECQ215"
function titlebig(Escpos $printer, $text) {
	$printer -> selectPrintMode(Escpos::MODE_EMPHASIZED);
	$printer -> setTextSize(1, 2);
	$printer -> setJustification(Escpos::JUSTIFY_CENTER);
	$printer -> text(trans($text));
	$printer -> setJustification(); // Reset
}
function title(Escpos $printer, $text) {
	$printer -> selectPrintMode(Escpos::MODE_EMPHASIZED);
	$printer -> setTextSize(1, 1);
	$printer -> setJustification(Escpos::JUSTIFY_CENTER);
	$printer -> text(trans($text));
	$printer -> selectPrintMode(); // Reset
	$printer -> setJustification(); // Reset
}
function txt(Escpos $printer, $text) {
	$printer -> text("\n" . trans($text));
	$printer -> selectPrintMode(); // Reset
}
function total(Escpos $printer, $text) {
	$printer -> setTextSize(2, 2);
	$printer -> setJustification(Escpos::JUSTIFY_RIGHT);
	$printer -> text("\n" . trans($text));
	$printer -> setJustification(); // Reset
}
function foot(Escpos $printer, $text) {
	$printer -> setTextSize(1, 2);
	$printer -> text("\n" .trans($text));
}
function footBig(Escpos $printer, $text) {
	$printer -> setTextSize(2, 3);
	$printer -> text("\n" .trans($text));
}
function totalMin(Escpos $printer, $text) {
	$printer -> setTextSize(1, 2);
	$printer -> setJustification(Escpos::JUSTIFY_RIGHT);
	$printer -> text("\n" . trans($text));
	$printer -> setJustification(); // Reset
}
function footCenter(Escpos $printer, $text) {
	$printer -> setJustification(Escpos::JUSTIFY_RIGHT);
	$printer -> setTextSize(1, 1);
	$printer -> text("\n" .trans($text));
}
function trans($text) {
	$text=str_replace("<br>","\n",str_replace("\\n","\n",$text));
	$text=iconv("UTF-8","GB2312", $text);
	return $text;
}
function prn_txt($titleBig1,$title1,$txt1,$total1,$foot1,$CFG_Printer, $port = "9100",$footOption = false, $totalOption  = false, $PrintCookFontSize = false) {


require_once(dirname(__FILE__) . "/Escpos.php");
try {
	// Enter the share name for your USB printer here
	//$connector = new WindowsPrintConnector("x");
	$connector = new NetworkPrintConnector($CFG_Printer, $port); //"jingdu.dnsdojo.com");

	/* Print some bold text */
	$printer = new Escpos($connector); 
	//$printer -> text(trans($printtxt)); 
	if (strlen($titleBig1) > 0) titlebig($printer,$titleBig1);
	if (strlen($title1) > 0) title($printer,$title1);
	if (strlen($txt1) > 0) txt($printer, $txt1);
	if (strlen($total1) > 0) if ($totalOption) totalMin($printer,$total1); else total($printer,$total1) ;
	if (strlen($foot1) > 0)  if ($footOption) footCenter($printer,$foot1); else  if ($PrintCookFontSize) footBig($printer,$foot1); else foot($printer,$foot1);
	$printer -> feed(4);

	/* Bar-code at the end */
	//$printer -> setJustification(Escpos::JUSTIFY_CENTER);
	//$printer -> barcode("06902083887407",Escpos::BARCODE_ITF);
	$printer -> cut();
	//$printer -> pulse();
	
	/* Close printer */
	$printer -> close();
	return 'Print sucess!';
} catch(Exception $e) {
	return "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}
}
function prn_txt2($printtxt) {
	$printtxt=mb_convert_encoding($printtxt, "UTF-8", "gb2312"); 
	$printtxt=urlencode($printtxt);
	//$printtxt=str_replace(" ", "%20",$printtxt);
	global $PrinterPiSerial;
	$TOKEN_URL="http://densen.es/app/printonline/add.php?MM_insert=add&sn=" . $PrinterPiSerial . "&print=1&print_txt=" . $printtxt;
	$json=file_get_contents($TOKEN_URL);
	//echo $TOKEN_URL;
	//$result=json_decode($json,true);
	//$ACC_TOKEN=$result['access_token'];
	//$expires_in=$result['expires_in']; //凭证有效时间，单位：秒
	//print_r($json);
	if ($json=='ADD OK') die ('成功下单！');
}

if (!empty($_POST[prnarr])) {
	$prnarr=json_decode($_POST[prnarr], true);
	//调用打印接口
	echo prn_txt($prnarr['titleBig'],$prnarr['title'],$prnarr['printtxt'],$prnarr['total'],$prnarr['foot'],$prnarr['CFG_Printer']);
}
?>