<template>
	<div>
		<div style="float: right;margin-right:2%;">
			<el-button style='margin: 10px 0;' size="mini" type="primary" icon="document" @click="Notice">刷新</el-button>
			<el-button style='margin: 10px 0;' :disabled="!isAuth('vipPrivilege:add')" size="mini" type="primary"
				icon="document" @click="addNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="tableData.list">

			<el-table-column fixed prop="payClassifyId" label="编号" width="50">
			</el-table-column>
			<el-table-column prop="price" label="充值金额">
			</el-table-column>
			<el-table-column prop="money" label="到账金额">
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间">
			</el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('vipPrivilege:update')"
						@click="amendBanner(scope.$index, scope.row)">修改
					</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('vipPrivilege:delete')"
						@click="deletes(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 50, 100]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="tableData.totalCount">
			</el-pagination>
		</div>
		<el-dialog :title="title" :visible.sync="dialogFormVisible" center>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">充值金额：</span>
				<el-input style="width:50%;" v-model="price" placeholder="请输入充值金额"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">到账金额：</span>
				<el-input style="width:50%;" v-model="money" placeholder="请输入到账金额"></el-input>
			</div>
			<div style="margin-bottom: 10px;" v-if="yhjday">
				<span style="width: 200px;display: inline-block;text-align: right;">赠送多少天：</span>
				<el-input style="width:50%;" v-model="dayNum" placeholder="请输入赠送多少天"></el-input>
			</div>
			<div style="margin-bottom: 10px;" v-if="yhjday">
				<span style="width: 200px;display: inline-block;text-align: right;">每天赠送盛安豆：</span>
				<el-input style="width:50%;" v-model="dayMoney" placeholder="请输入每天赠送盛安豆"></el-input>
			</div>
			<el-button type="primary" plain class="button-new-tag" size="small" @click="couponIdBtn()">选择优惠券</el-button>
			<el-button type="primary" plain class="button-new-tag" size="small" @click="handleDay()">赠送盛安豆</el-button>
			
			<el-table v-loading="tableDataLoading" :data="yhjTableData" v-if="!yhjday">
				<el-table-column fixed prop="couponId" label="编号" width="50">
				</el-table-column>
				<el-table-column prop="couponName" label="优惠卷名称">
				</el-table-column>
				<el-table-column prop="giveNum" label="优惠卷数量">
					<template slot-scope="scope">
						<el-input type="number" v-model="scope.row.giveNum" placeholder="请输入优惠券数量" @change="handleYhjNum(scope.row)"></el-input>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="180">
					<template slot-scope="scope">
						<el-button size="mini" type="danger" 
							@click="deletesYhj(scope.row)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 优惠券列表 -->
		<el-dialog title="优惠券列表" :visible.sync="dialogFormVisible4" center>

			<el-table v-loading="tableDataLoading" :data="tableDataY.list">
				<el-table-column prop="couponId" label="编号" fixed>
				</el-table-column>
				<el-table-column prop="couponName" label="优惠券名称">
				</el-table-column>
				<el-table-column prop="money" label="优惠券面值">
				</el-table-column>
				<el-table-column prop="minMoney" label="优惠券最低消费">
				</el-table-column>
				<el-table-column prop="createTime" label="操作">
					<template slot-scope="scope">
						<el-button class="button-new-tag" size="small" @click="couponIdBtnQ(scope.row)">添加</el-button>
					</template>
				</el-table-column>

			</el-table>
			<div style="text-align: center;margin-top: 10px;">
				<el-pagination @size-change="handleSizeChangeY" @current-change="handleCurrentChangeY"
					:page-sizes="[10, 20, 30, 50, 100]" :page-size="size1" :current-page="page1"
					layout="total,sizes, prev, pager, next" :total="tableDataY.totalCount">
				</el-pagination>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 10,
				page1: 1,
				size1: 10,
				tableDataLoading: true,
				dialogFormVisible: false,
				tableData: [],
				yhjTableData: [],
				tableDataY: {},
				price: '',
				memberImg: '',
				memberId: '',
				money: '',
				couponId: '',
				couponName: '',
				sort: '',
				payClassifyId: '',
				dayNum:'',
				dayMoney:'',
				title: '添加',
				dialogFormVisible4: false,
				yhjday:false,
			}
		},
		methods: {
			// 获取数据列表
			dataSelect() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('payClassify/selectPayClassifyList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit
					})
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log('会员列表', data)
						this.tableDataLoading = false
						let returnData = data.data;
						this.tableData = returnData;
						if(this.tableData.list.length==0){
							if(this.page>1){
								this.page = this.page-1
								this.dataSelect()
							}
						}
					}

				})
			},
			//优惠卷数量校验
			handleYhjNum(row){
				var giveNum = row.giveNum
				if(giveNum<1){
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '优惠券数量不能小于1',
						type: 'warning'
					});
					row.giveNum = 1;
					return
				} 
			},
			Notice() {
				this.page = 1
				this.dataSelect()
			},
			handleDay(){
				this.dayNum = ''
				this.dayMoney = ''
				this.yhjday = true;
				this.yhjTableData = [];
			},
			addNotice() {
				this.money = ''
				this.memberId = ''
				this.price = ''
				this.couponId = ''
				this.sort = ''
				this.couponName = ''
				this.dayNum = ''
				this.dayMoney = ''
				this.yhjday = false;
				this.yhjTableData = [];
				this.title = '添加'
				this.dialogFormVisible = true
			},
			handleCurrentChange(val) {
				this.page = val
				this.dataSelect()
			},
			handleSizeChange(val) {
				this.limit = val
				this.dataSelect()
			},
			handleSizeChangeY(val) {
				this.size1 = val;
				this.dataSelectY()
			},
			handleCurrentChangeY(val) {
				this.page1 = val;
				this.dataSelectY()
			},
			// 添加banner图
			addNoticeTo() {
				if (this.price == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入充值金额',
						type: 'warning'
					});
					return
				}
				if(this.couponName==''){
					this.couponId = ''
				}
				if(this.couponId != ''||this.money != ''){
					if (this.money == '') {
						this.$notify({
							title: '提示',
							duration: 1800,
							message: '请输入到账金额',
							type: 'warning'
						});
						return
					}
					// if (this.couponId == '') {
					// 	this.$notify({
					// 		title: '提示',
					// 		duration: 1800,
					// 		message: '请选择赠送的优惠券',
					// 		type: 'warning'
					// 	});
					// 	return
					// }
				}
				
				// this.classify = 1
				if (this.title == '添加') {
					var url = 'payClassify/insertPayClassify'
				} else {
					var url = 'payClassify/updatePayClassify'
				}
				this.$http({
					url: this.$http.adornUrl(url),
					method: 'post',
					data: this.$http.adornData({
						'money': this.money,
						'memberId': this.memberId,
						'price': this.price,
						'sort': this.sort,
						'couponId': this.couponId,
						'payClassifyId': this.payClassifyId,
						'detailList':this.yhjTableData,
						'dayNum':this.dayNum,
						'dayMoney':this.dayMoney,
					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.dialogFormVisible = false
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
						
								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
							}
						})
					}
					
				})
			},
			// 修改
			amendBanner(index, rows, a) {
				if (a == 1) {
					this.pl = 1
				} else {
					this.pl = 0
				}
				this.dialogFormVisible = true
				this.payClassifyId = rows.payClassifyId
				if(rows.money){
					this.money = rows.money
				}else{
					this.money = ''
				}
				if(rows.sort){
					this.sort = rows.sort
				}else{
					this.sort = ''
				}
				
				
				if(rows.coupon){
					this.couponName = rows.coupon.couponName
				}else{
					this.couponName = ''
				}

				this.yhjTableData = rows.detailList;
				
				this.price = rows.price
				this.title = '修改'

				this.dayMoney = rows.dayMoney
				this.dayNum = rows.dayNum
				if(this.dayNum){
					this.yhjday = true;
				}else{
					this.yhjday = false;
				}

			},
			//删除一级
			deletes(row) {
				let delid = row.payClassifyId
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl(`payClassify/deletePayClassify/?payClassifyId=${delid}`),
						method: 'post',
						params: this.$http.adornParams({})
					}).then(({
						data
					}) => {
						this.$message({
							message: '删除成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					})
				}).catch(() => {})
			},
			//删除一级
			deletesYhj(row) {
				let arr = this.yhjTableData;
				let newArr = arr.filter(item => item !== row);
				this.yhjTableData = newArr;
			},
			//获取优惠券数据
			dataSelectY() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/coupon/seleteAllCoupon'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page1,
						'limit': this.size1
					})
				}).then(({
					data
				}) => {
					this.tableDataLoading = false
					let returnData = data.data;
					this.tableDataY = returnData
				})
			},
			// 打开优惠券列表
			couponIdBtn() {
				this.dayNum = ''
				this.dayMoney = ''
				this.yhjday = false;
				this.dataSelectY()
				this.dialogFormVisible4 = true
			},
			couponIdBtnQ(rows) {
				var list = {couponName:rows.couponName,couponId:rows.couponId,giveNum:1};

				var yhjTableData = this.yhjTableData;
				var sfjx = 0;
				for(var i=0;i<yhjTableData.length;i++){
					if(rows.couponId == yhjTableData[i].couponId){
						sfjx = 1;
					}
				}
				if(sfjx==1){
					this.$message({
							message: "此优惠卷重复，不可添加！",
							type: 'warning',
							duration: 1500,
							onClose: () => {
							}
						})
					return;
				}
				var yn = this.yhjTableData.includes(list);
				this.yhjTableData.push(list)
				this.dialogFormVisible4 = false
			},
			closes(){
				this.couponName = ''
				this.couponId = ''
			}
		},
		mounted() {
			this.dataSelect()
		}
	}
</script>

<style>
</style>
