<template>
	<view>
		<view class="top">
			<view class="title">付款给：<text>{{title}}</text></view>
			<view class="shop">{{options.merchantName ? options.merchantName : ''}}<text v-if="options.merchantName&&options.name">/</text>{{options.name ? options.name : ''}}</view>
		</view>
		<view class="con">
			<view class="tits">付款金额</view>
			<view class="money ddflex">
				<text>￥</text>
				<input v-model="money" :disabled="unEdit?true:false" type="digit" class="fflex" />
			</view>
			<view class="bz">
				<textarea v-model="remarks" placeholder="填写备注" placeholder-class="placeholder" class="bzcon"></textarea>
			</view>
			<view class="btn" @click="pays()">马上付款</view>
			<navigator :url="'/payment/paymentList/paymentList?openId=' + openId" hover-class="none" class="jilu">买单记录</navigator>
		</view>
	</view>
</template>

<script>
const req = require('../../utils/request.js');
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const app = getApp();
export default {
	data() {
		return {
			options: {},
			title: '',
			openId: '',
			money: '',
			remarks: '',
			scene: '',
			unEdit:false,//扫码带金额进来则不允许修改
		};
	},
	async onLoad(options) {
		console.log('options==', options)
		this.options = options;
		app.globalData.getCheckSessoin(json => {
			this.openId = json.openid;
		});
		await req.silenceLogin(options.userId, '');
		if(options.scene){
			this.scene = decodeURIComponent(options.scene);
			await this.loadCodeParams();
		}else{
			await this.getConfig();
			if(options.merchantId && options.merchantId != 'null' && options.merchantId != ''){
				await this.getMerchant();
			}
			if(this.options.uid){
				await this.getUserInfo();
			}
		}
	},
	onShow(){
	},
	methods: {
		getConfig() {
			var _this = this;
			return new Promise((resolve,reject)=>{
				req.getRequest('/api/config', {}, function(res) {
					_this.title = res.CONFIG_PROJECT_TITLE;
					uni.setNavigationBarTitle({
						title: _this.title + '收银台'
					})
					resolve();
				})
			})
		},
		getMerchant(){
			return new Promise((resolve,reject)=>{
				req.getRequest('/api/merchant/getMerchant',{id: this.options.merchantId},res=>{
					this.options.merchantName = res.title;
					resolve();
				})
			})
		},
		getUserInfo(){
			return new Promise((resolve,reject)=>{
				req.getRequest('/api/user/getInfo',{userId: this.options.uid},res=>{
					this.options.name = res.realName ? res.realName : res.nickName;
					resolve();
				})
			})
		},
		pays(){
			let params = {};
			var keys = unescape(this.scene).split('=');
			let apiUrl = '';
			if(keys[0] == 'id'){
				params = {
					openId: this.openId,
					money: this.money,
					id: keys[1]
				}
				if(this.remarks){
					params.note = this.remarks;
				}
				apiUrl = '/api/v3/collection/create';
			}else{
				params = {
					openId: this.openId,
					initiator: this.options.uid,
					money: this.money,
					merchantId: this.options.merchantId,
					type: this.options.type
				}
				if(this.remarks){
					params.remarks = this.remarks
				}
				apiUrl = '/api/receipt/initiateCollection';
			}
			req.postRequest(apiUrl,params,res=>{
				this.payOrder(res)
			})
		},
		payOrder(res) {
			var keys = unescape(this.scene).split('=');
			if(keys[0] == 'id'){
				req.payOrder(res, success => {
					uni.redirectTo({
						url: '/payment/success/success?openId=' + this.openId
					})
				},'payment')
			}else{
				req.receipt(res, success => {
					uni.redirectTo({
						url: '/payment/success/success?openId=' + this.openId
					})
				})
			}
			
		},
		loadCodeParams() {
			let _ts = this;
			return new Promise((resolve, reject) => {
				var keys = unescape(_ts.scene).split('=');
				let form = {
					id: keys[1]
				};
				req.getRequest('/api/v3/collection/params', form, data => {
					this.title = data.authName;
					this.options.merchantName = data.merchantName;
					this.options.name = data.userName;
					if(data.money != 0){
						this.money = data.money;
						if(data.money) this.unEdit = true
					}
					this.options.remarks = data.note ? data.note : '';
					resolve();
				});
			});
		},
	}
};
</script>

<style>
	@import "./payment.css";
</style>

