<template>
	<div class="order-wrapper">
		<!-- <breadcrumb :item-name="['订单管理', '订单列表']"></breadcrumb> -->
		<el-card>
			<!-- 搜索区域 -->
			<el-col>
				<div class="flex" style="justify-content: space-between;padding-bottom: 20px;">
					<el-button type="primary" @click="openBuy">转账</el-button>
					<el-button type="primary" icon="el-icon-refresh-right" @click="reload">刷新</el-button>
				</div>
			</el-col>
			<!-- 订单表格区域 -->
			<el-table :data="list" border style="width: 100%">
				<el-table-column prop="id" label="id" width="180">
				</el-table-column>
				<el-table-column prop="transfer_num" label="转账金额(元)" width="250"></el-table-column>
				<el-table-column prop="to_user_id" label="收款人ID" width="150">
				</el-table-column>
				<el-table-column prop="status" label="创建时间">
					<template slot-scope="scope">
						{{showTime(scope.row.createtime)}}
					</template>
				</el-table-column>
				<el-table-column prop="balance" label="转账后用户余额">
					<template slot-scope="scope">
						{{userInfo.id == scope.row.to_user_id ? scope.row.balance: ''}}
					</template>
				</el-table-column>
			</el-table>
			<div style="height: 20px;"></div>
			<!-- 分页区域 -->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryInfo.page" :page-sizes="[10, 20, 50, 100]" :page-size="queryInfo.pagesize"
				layout="total, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
		<!-- 修改地址对话框 -->
		<el-dialog title="转账" :visible.sync="dialogTableVisible">
			<el-form :model="form">
				<el-form-item label="收款用户ID" :label-width="formLabelWidth">
					<el-input v-model="form.to_user_id" type="number"></el-input>
				</el-form-item>
				<el-form-item label="转账金额" :label-width="formLabelWidth">
					<el-input v-model="form.transfer_num" type="number"></el-input>
				</el-form-item>
				<el-form-item label="动态口令" :label-width="formLabelWidth" v-if="userInfo.is_binding == 1">
					<el-input v-model="form.code" type="number"></el-input>
				</el-form-item>
			</el-form>
			<div class="t-r">
				当前余额： {{userInfo.money*1}}元
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogTableVisible = false">取 消</el-button>
				<el-button type="primary" @click="goBuy">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
			mapState,
			mapMutations
		} from 'vuex';
	import {
		createCode,
		getCodeList,
		getUserList
	} from '@/request/agent.js'
	import {
		getRechangeList,
		rechange,
		getTransferList,
		transfer,
		getUserInfo
	} from '@/request/user.js'
	import {
		showTime
	} from '@/assets/js/tools.js'

	export default {
		name: 'Order',
		data() {
			return {
				loading: false,
				formLabelWidth: '100px',
				form: {
					to_user_id: '',
					transfer_num: '',
					code: ''
				},
				dialogTableVisible: false,
				queryInfo: {
					type: -1, //-1->全部 0->银行卡 1->微信 2->支付宝 
					page: 1,
					limit: 10
				},
				list: [],
				total: 0,
				dataloading: false,
				headers: {},
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		created() {
			this.getList()
			this.headers['token'] = window.sessionStorage.getItem('token')
		},
		methods: {
			...mapMutations(['setUserInfo']),
			showTime,
			reload() {
				this.getList('reload')
				this.getUserInfo()
			},
			getUserInfo() {
				getUserInfo().then(res => {
					this.setUserInfo(res.data)
				})
			},
			handleAvatarSuccess(res, file) {
				this.form.image = res.data.fullurl;
				console.log(this.form.image, 'this.form.avatar');
			},
			beforeAvatarUpload(file) {
				// const isJPG = file.type === 'image/jpeg/png';
				// const isLt2M = file.size / 1024 / 1024 < 2;

				// if (!isJPG) {
				// 	this.$message.error('上传头像图片只能是 JPG 格式!');
				// }
				// if (!isLt2M) {
				// 	this.$message.error('上传头像图片大小不能超过 2MB!');
				// }
				// return isJPG && isLt2M;
				return true
			},
			handleAvatarError() {

			},
			openBuy() {
				this.dialogTableVisible = true
			},
			goBuy() {
				let that = this
				if (that.loading) {
					return
				}
				if (!that.form.to_user_id) {
					return that.$msg.error('请输入收款人ID')
				}
				if (!that.form.transfer_num) {
					return that.$msg.error('请输入转账金额')
				}
				if(that.userInfo.money < that.form.transfer_num*1) {
					return that.$msg.error('您的余额不足本次转账')
				}
				that.loading = true
				transfer(that.form).then(res => {
					console.log(res);
					that.$msg.success('转账成功')
					that.dialogTableVisible = false
					that.loading = false
					that.getList('reload')
					that.getUserInfo()
				}).catch(err => {
					that.loading = false
				})
			},
			getList(type) {
				let that = this
				if (type == 'reload') {
					that.dataloading = false
					that.queryInfo.page = 1
					that.dataloading = false
				}
				if (that.dataloading) {
					return
				}
				
				that.dataloading = true
				getTransferList(that.queryInfo).then(res => {
					that.total = res.data.count
					that.list = res.data.data
					that.dataloading = false
				}).catch(err => {
					that.dataloading = false
				})
			},
			handleSizeChange(newPageSize) {
				this.queryInfo.limit = newPageSize
				this.getList()
			},
			handleCurrentChange(newPageNum) {
				this.queryInfo.page = newPageNum
				this.getList()
			},
		},

	}
</script>

<style scoped lang="scss">
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
		border: 1px dashed #d9d9d9;
		border-radius: 10px;
	}
	.t-r {
		text-align: right;
		padding-top: 10px;
	}
</style>