<template>
	<div class="container" style="margin: 10px;">
		<el-form :inline="true" :model="moneymodel" class="demo-form-inline">
			<el-form-item label="用户ID">
				<el-input v-model="moneymodel.UserId" placeholder="用户ID"></el-input>
			</el-form-item>
			<el-form-item label="状态">
				<el-select v-model="moneymodel.status" placeholder="请选择">
					<el-option label="成功" value="0"></el-option>
					<el-option label="失败" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="onSubmit">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="success" @click="AddOrUpdMoney(null)">新增</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" style="width: 100%"  :show-summary="true" :summary-method="getSummaries">
			<el-table-column prop="id" label="ID" v-if="false">
				<template slot-scope="scope">
					{{scope.row.id}}
				</template>
			</el-table-column>
			<el-table-column label="用户编号" align="center" prop="userid">
				<template slot-scope="scope">
					{{scope.row.userid}}
				</template>
			</el-table-column>
			<el-table-column label="头像" align="center" prop="beijintupian">
				<template slot-scope="scope">
					<img :src="scope.row.beijintupian" style="width: 50px;height: 50px;">
				</template>
			</el-table-column>
			<el-table-column label="用户微信名" width="150" align="center" prop="username">
				<template slot-scope="scope">
					{{scope.row.username}}
				</template>
			</el-table-column>
			<el-table-column label="手机号" align="center" prop="phone">
				<template slot-scope="scope">
					{{scope.row.phone}}
				</template>
			</el-table-column>
			<el-table-column prop="openid" label="OpenId" v-if="false">
				<template slot-scope="scope">
					{{scope.row.openid}}
				</template>
			</el-table-column>
			<el-table-column prop="remark" label="备注" v-if="false">
				<template slot-scope="scope">
					{{scope.row.remark}}
				</template>
			</el-table-column>
			<el-table-column prop="userid" label="用户ID" v-if="false">
				<template slot-scope="scope">
					{{scope.row.userid}}
				</template>
			</el-table-column>
			<el-table-column prop="oldmoney" label="原金额" align="center" v-if="false">
				<template slot-scope="scope">
					￥{{scope.row.oldmoney/100}}
				</template>
			</el-table-column>
			<el-table-column prop="nowmoney" label="余额" align="center">
				<template slot-scope="scope">
					￥{{scope.row.nowmoney/100}}
				</template>
			</el-table-column>
			<el-table-column prop="money" label="提现金额" align="center">
				<template slot-scope="scope">
					￥{{scope.row.money/100}}
				</template>
			</el-table-column>
			<el-table-column prop="add_time" label="提现时间" align="center">
				<template slot-scope="scope">
					{{scope.row.add_time}}
				</template>
			</el-table-column>
			<el-table-column prop="status" label="状态" align="center">
				<template slot-scope="scope">
					<el-tag :type="scope.row.status==0?'success':'warn'">
						{{scope.row.status==0?'成功':'失败'}}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="编辑" v-if="false">
				<template slot-scope="scope">
					<el-button type="primary" @click="AddOrUpdMoney(scope.row)">编辑</el-button>
				</template>
			</el-table-column>
			<el-table-column label="删除" >
				<template slot-scope="scope">
					<el-button type="danger" @click="DelMoney(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background layout="prev, pager, next" :total="moneymodel.TotalCount" @current-change="Pages"
			style="text-align:center;margin-top: 10px;">
		</el-pagination>
		<el-dialog title="提示" :visible.sync="dialogVisible" width="30%">

			<el-form ref="form" :model="form" label-width="80px" :rules="rules">
				<el-form-item label="用户编号" prop="userid">
					<el-input v-model="form.userid" placeholder="用户编号"></el-input>
				</el-form-item>
				<el-form-item label="头像" prop="beijintupian">
					<el-input v-model="form.beijintupian" placeholder="头像"></el-input>
				</el-form-item>
				<el-form-item label="微信名" prop="username">
					<el-input v-model="form.username" placeholder="微信名"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="form.phone" placeholder="手机号"></el-input>
				</el-form-item>
				<!-- 	<el-form-item label="用户名" prop="username">
					<el-input v-model="form.username" placeholder="用户名"></el-input>
				</el-form-item> -->
				<el-form-item label="原提现金额" prop="oldmoney" >
					<el-input v-model="form.oldmoney" placeholder="原提现金额" ></el-input>
				</el-form-item>
				<el-form-item label="提现金额" prop="money">
					<el-input v-model="form.money" placeholder="提现金额"></el-input>
				</el-form-item>
				<el-form-item label="剩余金额" prop="nowmoney" v-if="false">
					<el-input v-model="form.nowmoney"></el-input>
				</el-form-item>
			</el-form>



			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="SubmitMoney">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import {
		AddOrUpdMoney,
		DelMoney,
		GetMoneyList
	} from '../../api/money.js'
	import utils from '../../utils/utils.js'
	export default {
		data() {
			return {
				dialogVisible: false,
				rules: {
					username: [{
						required: true,
						message: '请输入用户名',
						trigger: 'change'
					}, ],
					oldmoney: [{
							required: true,
							message: '请输入金额(元)',
							trigger: 'change'
						},
						{
							pattern: /^[1-9][0-9]*(\.[0-9]{1,2})?$/,
							message: '请输入数值和小数',
							trigger: 'blur'
						}
					],
					money: [{
							required: true,
							message: '请输入金额(元)',
							trigger: 'change'
						},
						{
							pattern: /^[1-9][0-9]*(\.[0-9]{1,2})?$/,
							message: '请输入数值和小数',
							trigger: 'blur'
						}
					],
				},
				tableData: [],
				moneymodel: {
					"UserId": 0,
					"status": 0,
					"KeyWord": "",
					"TotalCount": 0,
					"PageIndex": 1,
					"PageSize": 10,
					"Sort": [{
						"Field": "add_time",
						"Type": 1
					}]
				},
				form: {
					"id": 0,
					"openid": "",
					"remark": "",
					"username": "",
					"userid": 0,
					"beijintupian": "",
					"phone": "",
					"oldmoney": 0,
					"nowmoney": 0,
					"money": 0,
					"add_time": "2022-02-10T13:55:23.241Z",
					"status": 0
				}
			}
		},
		created() {
			this.ShowMoneys()
		},
		methods: {
			// 自定义合计
			getSummaries(param){
				console.log(param)
				const {
				columns,
				data
				} = param;
				const sums = [];
				columns.forEach((column, index) => {
				if(index === 0) {
				sums[index] = '合计';
				return;
				}
				if(index==4){
					const values = data.map((item) => Number(item[column.property]));
					let nums=0
					values.forEach((item,index)=>{
						nums+=item/100
					})
					sums[index]=nums
				}
				if(index==5){
					const values = data.map((item) => Number(item[column.property]));
					let mons=0
					values.forEach((item,index)=>{
						mons+=item/100
					})
					sums[index]=mons
				}
				// alert(JSON.stringify(columns[index].nowmoney))
				// alert(JSON.stringify(data))
				// alert(JSON.stringify(column.property))
				// switch(column.property) {
				// case "nowmoney":
				// // alert(sums[index])
				// sums[index] =this.tableData.map(()=>{item.nowmoney})//值取自后台
				// break;
				// default:
				// break;
				// }
				});
				return sums;
			},
			// 提交添加或者修改
			SubmitMoney() {
				this.$refs.form.validate((valid) => {
					if (valid) {
						this.form.oldmoney = this.form.oldmoney * 100
						this.form.money = this.form.money * 100
						this.form.nowmoney = parseInt(this.form.oldmoney - this.form.money)
						AddOrUpdMoney(this.form).then(res => {
							console.log('哈哈', res)
							if (res.data.code == 200) {
								this.$message.success('操作成功')
								this.dialogVisible = false
								this.ShowMoneys()
							} else {
								this.$message.error('操作失败')
							}
						})
					}
				})
			},
			DelMoney(id) {
				DelMoney(id).then(res => {
					console.log('删除数据', res)
					if (res.code == 200) {
						this.$message.success('删除成功')
						this.ShowMoneys()
					}
					this.$message.error('删除失败')
				})
			},
			Pages(e) {
				this.moneymodel.PageIndex = e
				this.ShowMoneys()
			},
			// 展示提现金额
			ShowMoneys() {
				GetMoneyList(this.moneymodel).then(res => {
					console.log('提现记录', res)
					res.data.data.rows.forEach((item, index) => [
						item.add_time = utils.formatTime(item.add_time, "yyyy-MM-dd HH:mm:ss")
					])
					this.tableData = res.data.data.rows
					this.moneymodel.TotalCount = res.data.data.total
				})
			},
			AddOrUpdMoney(obj) {
				this.dialogVisible = true
				if (obj == null) {
					// 新增
					this.form.id = 0
					this.form.username = ''
					this.form.oldmoney = 0
					this.form.money = 0
					this.form.nowmoney = 0
				} else {
					// 修改
					this.form.id = obj.id
					this.form.username = obj.username
					this.form.oldmoney = obj.oldmoney / 100
					this.form.money = obj.money / 100
					this.form.nowmoney = obj.nowmoney / 100
				}
			},
			onSubmit() {
				this.ShowMoneys()
			}
		}
	}
</script>

<style>
</style>
