<!DOCTYPE html>
<html>
	<head>
		<title>二级市场订单表-电子交易协议</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css">
		<script src="../../static/kj/vue.min.js"></script>
		<script src="../../static/kj/element-ui/index.js"></script>
		<script src="../../static/kj/httpVueLoader.js"></script>
		<script src="../../static/kj/jquery.min.js"></script>
		<script src="../../static/kj/layer/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<script src="../../static/kj/upload-util.js"></script>
		<!-- 引入 html2canvas 和 jsPDF -->
		<script src="../../static/kj/html2canvas.min.js"></script>
		<script src="../../static/kj/jspdf.umd.min.js"></script>
		<style type="text/css">
			.c-panel .c-label{width: 8em;}
			table {
			  font-family: arial, sans-serif;
			  border-collapse: collapse;
			  width: 90%;
			  margin-top: 20px ;
			  margin-left: auto;
			  margin-right: auto;
			}
			th {
			  border: 1px solid darkgray;
			  text-align: left;
			  height: 24px;
			  padding: 8px;
			  background-color: #dddddd;
			}
			td {
			  border: 1px solid darkgray;
			  text-align: left;
			  padding: 8px;
			}
		</style>
	</head>
	<body>
		<div class="vue-box sbot" style="display: none;" :style="'display: block;'">
			<!-- ------- 内容部分 ------- -->
			<div class="s-body">
				<div class="c-panel" id="content-to-convert" ref="contentToConvert">
					<el-form v-if="m">
						<table>
							<tr>
								<td colspan='6' style="text-align:center;font-weight: bold; border: 0 0 1px 0; " >电子交易协议</td>
							</tr>
							<tr>
								<td colspan='6' style="font-weight: bold;" >一、交易信息</td>
							</tr>
							<tr>
								<th>订单编号</th>
								<td colspan='2'>{{m.orderNo}}</td>
								<th>结算单号</th>
								<td colspan='2'>{{m.settleBillNo}}</td>
							</tr>
							<tr>
								<th>交易日期</th>
								<td colspan='2'>{{m.payTime}}</td>
								<th>交货日期</th>
								<td colspan='2'></td>
							</tr>
							<tr>
								<th>银行流水号</th>
								<td colspan='5'>{{m.result}}</td>
							</tr>
							<tr>
								<th>商品名称</th>
								<th>数量</th>
								<th>重量（KG）</th>
								<th>单价（元）</th>
								<th>合计（元）</th>
								<th>备注</th>
							</tr>
							<tr>
								<td>{{m.goodsName}}</td>
								<td>{{m.buyQty}}</td>
								<td>{{m.netWet}}</td>
								<td>{{m.singlePrice}}</td>
								<td>{{m.resalePrice}}</td>
								<td>含税价</td>
							</tr>
							<tr>
								<td>合计</td>
								<td>{{m.buyQty}}</td>
								<td>{{m.netWet}}</td>
								<td>{{m.singlePrice}}</td>
								<td>{{m.resalePrice}}</td>
								<td>含税价</td>
							</tr>
							<tr>
								<td colspan='6'>合计金额人民币（大写）：{{m.resalePrice}}</td>
							</tr>
							<tr>
								<td colspan='6' style="font-weight: bold;" >二、卖方信息</td>
							</tr>
							<tr>
								<th>卖方</th>
								<td colspan='2'>{{m.businessName}}</td>
								<th>联系人</th>
								<td colspan='2'>{{m.contacts}}</td>
							</tr>
							<tr>
								<th>开户行</th>
								<td colspan='2'>{{m.peopleBankName}}</td>
								<th>联系方式</th>
								<td colspan='2'>{{m.phone}}</td>
							</tr>
							<tr>
								<th>银行账号</th>
								<td colspan='2'>{{m.peopleBankCode}}</td>
								<th>统一信用代码</th>
								<td colspan='2'>{{m.peopleDutyParagraph}}</td>
							</tr>
							<tr>
								<td colspan='6' style="font-weight: bold;" >三、买方信息</td>
							</tr>
							<tr>
								<th>买方</th>
								<td colspan='2' v-if="m.purchaserBusinessName">{{m.purchaserBusinessName}}</td>
								<td colspan='2' v-else>{{m.purchaserName}}</td>
								<th>联系人</th>
								<td colspan='2' v-if="m.legalPerson">{{m.legalPerson}}</td>
								<td colspan='2' v-else>{{m.purchaserName}}</td>
							</tr>
							<tr>
								<th>开户行</th>
								<td colspan='2'>{{m.purchaserBankName}}</td>
								<th>联系方式</th>
								<td colspan='2'>{{m.purchaserContact}}</td>
							</tr>
							<tr>
								<th>银行账号</th>
								<td colspan='2'>{{m.purchaserBankAccount}}</td>
								<th>统一信用代码</th>
								<td colspan='2'>{{m.purchaserDutyParagraph}}</td>
							</tr>
							<tr>
								<td colspan='6' style="font-weight: bold;" >四、其它信息</td>
							</tr>
							<tr>
								<th>交货地点</th>
								<td colspan='5'>{{m.unloadingAddress}}</td>
							</tr>
							<tr>
								<th>提货方式</th>
								<td colspan='5'></td>
							</tr>
							<tr>
								<th>付款方式</th>
								<td colspan='5'></td>
							</tr>
							<tr>
								<th>发票类型</th>
								<td colspan='5'></td>
							</tr>
							<tr>
								<th>其它约定</th>
								<td colspan='5'></td>
							</tr>
							<tr>
								<th>补充说明</th>
								<td colspan='5'></td>
							</tr>
							<tr>
								<td colspan='3'>
									卖方：{{m.businessName}} <br>
									法定代表人或授权委托人：{{m.contacts}}
								</td>
								<td colspan='3'>
									<span>
										<span v-if="m.purchaserBusinessName">买方：{{m.purchaserBusinessName}}<br></span>
										<span v-else>买方：{{m.purchaserName}}<br></span>
									</span>
									<span>
										<span v-if="m.legalPerson">法定代表人或授权委托人：{{m.legalPerson}}</span>
										<span v-else>法定代表人或授权委托人：{{m.purchaserName}}</span>
									</span>
								</td>
							</tr>
						</table>
					</el-form>
				</div>
			</div>
			<!-- ------- 底部按钮 ------- -->
			<div class="s-foot">
				<el-button type="success" icon="el-icon-printer" @click="htmlToPdf()">打印协议</el-button>
				<el-button type="primary" icon="el-icon-download" @click="downloadToPdf()">下载协议</el-button>
				<el-button @click="sa.closeCurrIframe()">关闭</el-button>
			</div>
		</div>
		<script>
			var app = new Vue({
				components: {
					"sa-info": httpVueLoader('../../sa-frame/com/sa-info.vue')
				},
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),	// 获取数据ID
					m: null
				},
				methods: {
					downloadToPdf() {
						const element = document.getElementById('content-to-convert');
						html2canvas(element).then(canvas => {
							// 使用 jsPDF 将 canvas 转换成 PDF
							const imgData = canvas.toDataURL('image/png');
							const pdf = new jspdf.jsPDF({
								orientation: 'portrait',
								unit: 'px',
								format: 'a4'
							});

							const imgProps = pdf.getImageProperties(imgData);
							const pdfWidth = pdf.internal.pageSize.getWidth();
							const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;

							// 添加图片，适应页面大小
							pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
							pdf.save(Date.now()+'.pdf');
						});
					},
					htmlToPdf() {
						const element = document.getElementById('content-to-convert');
						html2canvas(element).then(canvas => {
							// 使用 jsPDF 将 canvas 转换成 PDF
							const imgData = canvas.toDataURL('image/png');
							const pdf = new jspdf.jsPDF({
								orientation: 'portrait',
								unit: 'px',
								format: 'a4'
							});

							const imgProps = pdf.getImageProperties(imgData);
							const pdfWidth = pdf.internal.pageSize.getWidth();
							const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;

							// 添加图片，适应页面大小
							pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
							// 将 PDF 保存为 Blob 并处理它  这里需要处理跨域问题
							const pdfBlob = pdf.output('blob');
							var blobURL = window.URL.createObjectURL(pdfBlob);
							var newWindow = window.open(blobURL, '_blank');
							if (newWindow) {
								newWindow.focus();
							} else {
								alert('请允许弹出窗口或在新标签页中手动查看 PDF。');
							}
						});
					},
				},
				mounted: function() {
					sa.ajax('/level-two-server/TbOrders/getSubEmail?id=' + this.id, function(res) {
						this.m = res.data;
						if(res.data == null) {
							sa.alert('未能查找到 id=' + this.id + " 详细数据");
						}
					}.bind(this))
				}
			})

		</script>
	</body>
</html>
