<template>
	<view class="box">
		<view class="search ddflex">
			<image src="../static/images/ssico.png" class="ssico"></image>
			<input v-model="searchTitle" placeholder="手动输入核销码" placeholder-class="placeholder" class="fflex" />
			<image src="../../static/pages/images/clear.png" class="clear" v-if="searchTitle" @click="clearSearchTitle()"></image>
			<view class="go" @click="search">查询</view>
		</view>
		<view class="sao ddflex" @click="scan()">
			<image src="../static/images/sao1.png"></image>
			<view>扫码核销</view>
		</view>
		<navigator url="/merchant/record/index" hover-class="none" class="record ddflex">
			<image src="../static/images/jlico.png" class="jlico"></image>
			<view class="fflex">核销记录</view>
			<image src="../../static/pages/images/more.png" class="rico"></image>
		</navigator>
		<view class="tip ddflex">
			<image src="../static/images/tip.png"></image>
			<view>核销码一经核销，无法撤回，请核实好再进行核销操作</view>
		</view>
	</view>
</template>

<script>
const app = getApp();
const req = require('../../utils/request.js');
export default {
	data() {
		return {
			apiUrl: req.env[req.env.NODE_ENV].apiUrl,
			searchTitle: '',
			code: '',
			url: '',
			merchantId: '',
			params: {
				page: 0,
				limit: 10
			}
		};
	},
	onLoad(options) {
		this.merchantId = options.merchantId;
	},
	onReachBottom() {},
	methods: {
		search() {
			if (this.searchTitle) {
				uni.navigateTo({
					url: '/merchant/writeDetail/index?code=' + this.searchTitle
				});
			} else {
				req.msg('请输入核销码');
			}
		},
		clearSearchTitle() {
			this.searchTitle = '';
		},
		//调用扫码
		// HX//核销
		// CK//次卡核销
		scan() {
			var tah = this;
			uni.scanCode({
				success: function(res) {
					console.log('条码类型：' + res.scanType);
					console.log('条码内容：', res);
					if (res.result.indexOf('HX')!=-1) {
						//核销
						console.log('HX>>>>>', res.result);
						tah.confirm(res.result.replace('HX', ''));
					} else if (res.result.indexOf('CK')!=-1) {
						console.log('CK>>>>>', res.result);
						uni.navigateTo({
							url: '/merchant/writeDetail/index?code=' + res.result
						});
					}
				}
			});
		},

		confirm(value) {
			let that = this;
			let form = this.params;
			form.search = value;
			form.merchantId = this.merchantId;
			req.getRequest(
				'/api/order/merchant/list',
				form,
				data => {
					if (data && data.length > 0) {
						var obj = data[0];
						this.toOrederDel(obj.id);
					}
				},
				true
			);
		},

		toOrederDel(id) {
			this.jumpUrl('/merchant/show?id=' + id + '&merchantId=' +  this.merchantId);
		},
		jumpUrl(url) {
			uni.navigateTo({
				url: url
			});
		},

		scanQRCode() {
			let that = this;
			jWeixin.scanQRCode({
				needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
				scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
				success: function(res) {
					var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
					console.log('res.resultStr==', result);
					if (res.resultStr.indexOf(',') > 0) {
						//条形码时返回的格式类似为【code128,123456789】
						var dealserialNumber = res.resultStr.split(',')[1];
						dealserialNumber = dealserialNumber.replace(/[^a-z\d]/gi, ''); //处理条形码扫描的字符
						that.code = dealserialNumber;
						// alert(dealserialNumber);
						uni.navigateTo({
							url: '/write/detail/detail?orderId=' + dealserialNumber
						});
						// location.href = that.apiUrl + '?orderId=' + dealserialNumber;//扫描结果传递到的处理页面
					} else {
						//二维码时
						that.code = result;
						uni.navigateTo({
							url: '/write/detail/detail?orderId=' + result
						});
						// alert(res.resultStr);
						// location.href = that.apiUrl + '?code=' + res.resultStr;//扫描结果传递到的处理页面
					}
				}
			});
		}
	}
};
</script>

<style>
page {
	background: #fff;
}
</style>
<style>
@import './index.css';
</style>
