<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%" max-height="620">
				<el-table-column prop="id" label="id" width="180">
				</el-table-column>
				<el-table-column prop="code" label="注册码" width="180">
				</el-table-column>
				<el-table-column prop="status" label="是否已使用">

					<template slot-scope="scope">
						<el-tag type="success" v-if="scope.row.status == 0">待使用</el-tag>
						<el-tag type="info" v-if="scope.row.status == 1">已使用</el-tag>
					</template>

				</el-table-column>
				<el-table-column prop="reg_user_id" label="注册用户ID">
					<template slot-scope="scope">
						{{scope.row.reg_user_id ? scope.row.reg_user_id: ''}}
					</template>
				</el-table-column>
				<el-table-column prop="createtime" 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, sizes,prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
		<!-- 修改地址对话框 -->
		<el-dialog title="购买注册码" :visible.sync="dialogTableVisible">
			<el-form :model="form">
				<el-form-item label="购买数量" :label-width="formLabelWidth">
					<el-input v-model="form.num" type="number"></el-input>
				</el-form-item>
				<el-form-item label="交易密码" :label-width="formLabelWidth">
					<el-input v-model="form.transaction" type="password"></el-input>
				</el-form-item>
			</el-form>
			<div class="t-r">
				合计： {{hj}}元
			</div >
			<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 {
		showTime
	} from '@/assets/js/tools.js'
	import {
		createCode,
		getCodeList,
		getIndex,
		
	} from '@/request/agent.js'
	import { getUserInfo } from '@/request/user.js'
	export default {
		name: 'Order',
		data() {
			return {
				loading: false,
				formLabelWidth: '80px',
				form: {
					num: '',
					transaction: ''
				},
				dialogTableVisible: false,
				queryInfo: {
					keyword: '',
					page: 1,
					limit: 10
				},
				list: [],
				total: 0,
				dataloading: false,
			}
		},
		created() {
			this.getList()
			this.getIndex()
			this.getUserInfo()
		},
		computed: {
			...mapState(['userInfo', 'baseInfo']),
			hj() {
				return this.baseInfo.code_price * 1 * this.form.num
			}
		},
		methods: {
			...mapMutations(['setUserInfo', 'setBaseInfo']),
			showTime,
			reload() {
				this.getList('reload')
				this.getUserInfo()
			},
			getIndex() {
				getIndex().then(res => {
					this.setBaseInfo(res.data)
				})
			},
			getUserInfo() {
				getUserInfo().then(res => {
					this.setUserInfo(res.data)
				})
			},
			openBuy() {
				this.dialogTableVisible = true
			},
			goBuy() {
				let that = this
				if (that.loading) {
					return
				}
				if (!that.form.num) {
					return that.$msg.error('请输入购买数量')
				}
				if (!that.form.transaction) {
					return that.$msg.error('请输入交易密码')
				}
				if(that.hj *1 > that.userInfo.money *1 ) {
					return that.$msg.error('您当前的账号余额不足')
				}
				that.loading = true
				createCode(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
				getCodeList(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">
	.t-r {
		text-align: right;
		padding-top: 10px;
	}
</style>