<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="getList('reload')">刷新</el-button>
				</div>

			</el-col>
			<!-- 订单表格区域 -->
			<el-table :data="list" border style="width: 100%" max-height="620">
				<el-table-column prop="id" label="id" width="180">
				</el-table-column>
				<el-table-column prop="recharge_num" label="充值金额(U)" width="350"></el-table-column>
				<!-- <el-table-column prop="image" label="充值凭证" width="150">
					<template slot-scope="scope">
						<el-image style="width: 100px; height:100px" :src="scope.row.image"></el-image>
					</template>
				</el-table-column> -->
				<el-table-column prop="status" label="审核结果" width="150">
					<template slot-scope="scope">
						<el-tag type="success" v-if="scope.row.status == 1">已通过</el-tag>
						<el-tag type="info" v-if="scope.row.status == 0">审核中</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="创建时间">
					<template slot-scope="scope">
						{{showTime(scope.row.createtime) }}
					</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="czVisible" width="350px">
			<el-form :model="form">
				<el-form-item label="充值金额" :label-width="formLabelWidth">
					<div class="flex">
						<el-input v-model="form.recharge_num" type="text"></el-input>
					</div>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="czVisible = false">取 消</el-button>
				<el-button type="primary" @click="getInfo">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 修改地址对话框 -->
		<el-dialog title="充值" :visible.sync="dialogTableVisible" width="500px" @close="closeCz">
			<div class="biaoti">
				温馨提示
			</div>
			<div class="tis" style="color: red;">
				充值地址请以当前地址为准，如转错地址导致货币丢失，责任自负
			</div>
			<div class="biaoti">
				钱包二维码
			</div>
			<div class="tis">
				<vueQrCode :size=qrCodeSize :text="form.address"> </vueQrCode>
				<div v-if="timer && time.isend == false" style="color: red;">充值剩余时间 : {{time.minutes + ':' + time.seconds }}</div>
				<div v-if="timer && time.isend" style="color: red;">当前地址已过期</div>
			</div>
			
			<div class="biaoti">
				钱包地址
			</div>
			<div class="flex tis">
				<el-input v-model="form.address" type="text" disabled></el-input>
				<el-button @click="onCopy(form.address)" style="margin-left: 20px;">复制地址</el-button>
			</div>
			<div class="biaoti">
				充值金额
			</div>
			<div class="tis">
				<el-input v-model="form.recharge_num" type="text" disabled></el-input>
			</div>
			<div class="biaoti">
				充值金额提示
			</div>
			<div style="color: red;">
				请按照实际到账金额(扣除手续费之后的到账):{{form.recharge_num}}进行充值,错误到账金额不予到账!
			</div>
			<div class="dialog-footer">
				<el-button @click="dialogTableVisible = false">取 消</el-button>
				<el-button type="primary" @click="dialogTableVisible = false">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import vueQrCode from "@/components/vueQrCode.vue";
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		createCode,
		getCodeList,
		getUserList,
		getIndex
	} from '@/request/agent.js'
	import {
		getRechangeList,
		rechange,
		getUserInfo,
		getrecharge
	} from '@/request/user.js'
	import {
		showTime,
		timeComputed
	} from '@/assets/js/tools.js'
	export default {
		components: {
			vueQrCode,
		},
		name: 'Order',
		data() {
			return {
				czVisible: false,
				qrCodeSize: 150,
				qrCodeText: '',
				loading: false,
				formLabelWidth: '100px',
				form: {
					recharge_num: '',
					image: ''
				},
				dialogTableVisible: false,
				queryInfo: {
					keyword: '',
					page: 1,
					limit: 10
				},
				list: [],
				total: 0,
				dataloading: false,
				headers: {},
				time: {},
				timer: ''
			}
		},
		created() {
			this.getList()
			this.getUserInfo()
			this.getIndex()
			this.headers['token'] = window.localStorage.getItem('token')
		},
		computed: {
			...mapState(['baseInfo', 'www', 'userInfo']),

		},
		methods: {
			...mapMutations(['setBaseInfo', 'setUserInfo']),
			showTime,
			closeCz() {
				clearInterval(this.timer)
				this.dialogTableVisible = false
				this.form = {
					recharge_num: ''
				}
			},
			getInfo() {
				let that = this
				clearInterval(that.timer)
				that.time = {}
				that.timer = null
				getrecharge(that.form).then(res => {
					console.log(res);
					that.czVisible = false
					that.dialogTableVisible = true
					that.form = res.data
					that.qrCodeText = res.data.address
					if(that.timer) {
						clearInterval(that.timer)
					}
					that.timer = setInterval(()=>{
						that.time = timeComputed(res.data.endtime*1000)
					},1000)
					
				})
			},
			onCopy(text) {
				this.$copyText(text).then(
					e => {
						console.log('复制成功：', e);
						this.$msg.success('复制成功')
					},
					e => {
						console.log('复制失败：', e);
						this.$msg.error('复制失败')
					}
				)
			},
			getIndex() {
				getIndex().then(res => {
					this.setBaseInfo(res.data)
					// this.qrCodeText = res.data.trc20
				})
			},
			getUserInfo() {
				getUserInfo().then(res => {
					this.setUserInfo(res.data)
					this.qrCodeText = res.data.withdraw_trc_address
				})
			},
			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
				this.czVisible = true
			},
			goBuy() {
				let that = this
				if (that.loading) {
					return
				}
				if (!that.form.recharge_num) {
					return that.$msg.error('请输入充值金额')
				}
				if (!that.form.image) {
					return that.$msg.error('请上传充值凭证')
				}
				that.loading = true
				rechange(that.form).then(res => {
					console.log(res);
					that.$msg.success('提交成功，请等待审核')
					that.dialogTableVisible = false
					that.loading = false
					that.getList('reload')
				}).catch(err => {
					that.loading = false
				})
			},
			getList(type) {
				let that = this
				if (type == 'reload') {
					that.queryInfo.page = 1
					that.dataloading = false
				}
				if (that.dataloading) {
					return
				}

				that.dataloading = true
				getRechangeList(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;
	}
	.biaoti {
		font-size: 18px;
		color: #000;
		font-weight: bold;
		padding: 20px 0;
	}
	.dialog-footer {
		display: flex;
		justify-content: flex-end;
		// width: 500px;
	}
</style>