<template>
	<view>
		<view class="username paddtop10">
			<view class="namecont">卡券选择</view>
			<picker mode="selector" style="width: 74%;" :range="cardList" range-key="couponName" @change="changeUserCard">
				<view class="date text1" style="padding: 0 25rpx;">{{typeName}}</view>
			</picker>
			<image class="flright" style="width: 15px;height: 15px;" src="../../static/img/downj.png"></image>
		</view>
		<view class="username">
			<view class="namecont">库存数量</view>
			<input v-model="stockCount" type="number" disabled="true"
				placeholder-style="color: #bfbfbf;font-size:14px;padding-top:2px;" />
		</view>
		<view class="username">
			<view class="namecont">分配数量</view>
			<input placeholder="请输入分配数量" v-model="cardnums" type="number"
				placeholder-style="color: #bfbfbf;font-size:14px;padding-top:2px;" />
		</view>
		<view class="btn mart50" @click="assignCouponChildAgent()">分 发</view>
	</view>
</template>

<script>
	import {
		getCouponByAgent,
		assignCouponChildAgent
	} from '../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				cardnums: '', //数量
				cardList: [], // 卡列表
				typeName: '', // 卡名称
				stockCount:'', // 库存数量
				type: '' ,//区分类型
				agentid: ''//分发代理商id
			}
		},
		onLoad(options) {
			this.agentid = options.agentid;
			this.getCouponByAgent();
		},
		methods: {
			//查询卡列表
			getCouponByAgent() {
				uni.showLoading({
					title: '加载中...'
				})
				let datas = {
					agentId: app.globalData.bresthighAgent.id,
					pageNum: 1,
					pageSize: 999
				}
				getCouponByAgent(datas).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {
						this.cardList = res.return_data.list;
						this.type = res.return_data.list[0].id;
						this.stockCount = res.return_data.list[0].stockCount;
						this.typeName = res.return_data.list[0].couponName;
					}
				})
			},
			//选择卡类型
			changeUserCard(e) {
				this.type = this.cardList[e.target.value].id;
				this.stockCount = this.cardList[e.target.value].stockCount;
				this.typeName = this.cardList[e.target.value].couponName;
			},
			//分配卡券
			assignCouponChildAgent() {
				let that = this;
				if (that.cardnums == '') {
					uni.showToast({
						title: '请填写分发数量',
						icon: "none",
						duration: 2000
					});
					return;
				}
				uni.showModal({
					title: '分发卡券',
					content: '确认分发数量、卡券是否正确!',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '添加中'
							})
							let datas = {
								"couponAgentRelId": that.type,
								"agentId": that.agentid,
								"stockCount": that.cardnums
							}
							assignCouponChildAgent(datas).then(res => {
								uni.hideLoading();
								if (res.return_code == '000000') {
									uni.showToast({
										title: '分发成功',
										icon: 'none',
										duration: 2000,
										success() {
											setTimeout(() => {
												uni.navigateBack({})
											}, 1500);
										}
									})
									return;
								}
								uni.showToast({
									title: res.return_msg,
									duration: 2000,
									icon: 'none'
								})
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.username {
		width: calc(100% - 90upx);
		height: 100upx;
		display: flex;
		align-items: center;
		background-color: rgba($color: #ffffff, $alpha: 0.1);
		border-bottom: 1px solid #f6f6f6;
		padding: 8upx 45upx;

		input {
			width: 50%;
			height: 50upx;
			font-size: 16px;
			color: #333333;
			font-weight: blod;
		}

		.get-code {
			position: absolute;
			display: flex;
			align-items: center;
			justify-content: center;
			right: 7px;
			z-index: 3;
			border: 1px solid #bfbfbf;
			width: 25%;
			padding: 3px 5px;
			border-radius: 22px;

			&:after {
				content: " ";
				width: 1upx;
				height: 50upx;
				position: absolute;
				z-index: 3;
				margin-right: 100%;
				left: 0;
				top: 20upx;
			}
		}

		.namecont {
			color: #666666;
			width: 35%;
		}
	}

	.btn {
		bottom: 0px;
		position: absolute;
		color: #FFFFFF;
		background-color: #0083f5;
		width: 90%;
		margin-left: 5%;
		margin-top: 80rpx;
		margin-bottom: 50rpx;
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 45rpx;
		font-size: 40rpx;
	}
</style>
