<?php
require '../conn/conn.php';
require '../conn/function.php';
$type=$_GET["type"];
$id=intval($_GET["id"]);
$P=getrs("select * from sl_product where P_id=$id");

$D_domain=splitx($_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"],"/conn",0);
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"> 
	<title>生成海报</title>
	<link rel="stylesheet" href="../css/bootstrap.min.css">
	<script src="../js/jquery.min.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script src="../js/html2canvas.js"></script>
	<script src="../js/qrcode.min.js"></script>
	<link rel="shortcut icon" href="../media/<?php echo $C_ico?>" />
</head>
<body style="background: #EEEEEE;">
<div style="margin: 0px auto;width: 500px;border: solid 1px #DDDDDD" id="content">
	<img src="../media/<?php echo $P["P_pic"]?>" width="500">
	<div style="background: #ffffff;padding: 20px;">
		<div style="font-weight: bold;font-size: 20px;width: 350px;display: inline-block;vertical-align: top;">
			<?php echo $P["P_title"]?>
			<div style="color: #ff0000;font-size: 20px;margin-top:10px; ">
				￥<?php echo $P["P_price"]?>
			</div>
		</div>
		
		<div style="width:90px;text-align: center;font-size: 12px;display: inline-block;">
			<div style="margin-bottom: 5px;" id="billImage"></div>
			长按识别二维码
		</div>
	</div>
</div>

<div style="margin-top: 30px;text-align: center;">
	<div style="font-size: 12px;margin-bottom: 10px;">海报图片可以发布到朋友圈/分享给好友，方便推广</div>
	<p><input type="text" id="myInput" class="form-control" style="width:500px;display:inline-block" value="<?php echo gethttp().$D_domain."/shop/?id=$id";?>"></p>
	<p><button class="btn btn-sm btn-primary" id="btnSave">下载海报</button>
	<button class="btn btn-sm btn-info" type="button" onclick="copylink()">复制链接</button></p>
</div>
<script>
function copylink() {
	var copyText = document.getElementById("myInput");
	copyText.select();
	copyText.setSelectionRange(0, 99999); /* 为移动设备设置 */
	navigator.clipboard.writeText(copyText.value);
	alert("复制成功！")
}

function saveFile(data, filename) {
	var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
	save_link.href = data;
	save_link.download = filename;

	var event = document.createEvent('MouseEvents');
	event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	save_link.dispatchEvent(event);
};
$(function() {
	$('#btnSave')
		.click(function(event) {
			html2canvas(
				$('#content'), {
					onrendered: function(canvas) {
						var pageData = canvas.toDataURL('image/png', 1.0);
						console.log(pageData)
						saveFile(pageData.replace("image/png", "image/octet-stream"), new Date()
							.getTime() + ".png");
					}

				})

		});

	function convertCanvasToImage(canvas) {
		var image = new Image();
		image.src = canvas.toDataURL("image/png");
		document.body.appendChild(image);
		return image;
	}


	var qrcode = new QRCode('billImage', {
		width: 90,
		height: 90,
		colorDark: '#000000',
		colorLight: '#ffffff',
		correctLevel: QRCode.CorrectLevel.H
	});
	qrcode.makeCode("<?php echo gethttp().$D_domain."/shop/?id=$id";?>");

})
</script>
</body>
</html>