<template>
	<view class="container">
		<form @submit="onSubmit">
			<view class="page-block">
				<view class="content">
					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>等级</text>
						</view>
						<view class="desc">
							<input type="number" maxlength="3" :value="info.shared_level?info.shared_level:1"
								name="shared_level" class="input-box" placeholder="请输入等级"
								placeholder-class="desc-placeholder">
						</view>
						<view class="red">数字越大等级越高</view>
					</view>


					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>等级名称</text>
						</view>
						<view class="desc">
							<input type="text" :value="info.name" name="name" class="input-box" placeholder="请输入等级名称"
								placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>进阶费用</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.amount" name="amount" class="input-box"
								placeholder="请输入进阶费用" placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>缴纳费用</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.money" name="money" class="input-box" placeholder="请输入缴纳费用"
								placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>直接佣金比例(%)</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.direct_commission" name="direct_commission"
								class="input-box" placeholder="请输入直接佣金比例" placeholder-class="desc-placeholder">
						</view>
					</view>
					<view class="red">直接+间接比例不能超100%</view>


					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>间接佣金比例(%)</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.indirect_commission" name="indirect_commission"
								class="input-box" placeholder="请输入间接佣金比例" placeholder-class="desc-placeholder">
						</view>
					</view>
					<view class="red">直接+间接比例不能超100%</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>上架开关</text>
						</view>
						<view class="desc">
							<view class="flex-center">
								<view class="checklist-box " @click="chooseSwicth(index)"
									v-for="(item,index) of sjSwitch" :key='index'
									:class="sjSwitchIndex==index?'actt':''">
									<view class="radio__inner" :class="sjSwitchIndex==index?'act_border':''">
										<view :class="sjSwitchIndex==index?'is-default-checked':''"
											class="radio__inner-icon radio__inner-icon ">
										</view>
									</view>
									<text class="checklist-text">{{item}}</text>
								</view>
							</view>
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>显示位置</text>
						</view>
						<view class="desc">
							<view class="flex-center">
								<view class="checklist-box " @click="chooseShowStatus(index)"
									v-for="(item,index) of show_arr" :key='index' :class="showIndex==index?'actt':''">
									<view class="radio__inner" :class="showIndex==index?'act_border':''">
										<view :class="showIndex==index?'is-default-checked':''"
											class="radio__inner-icon radio__inner-icon ">
										</view>
									</view>
									<text class="checklist-text">{{item}}</text>
								</view>
							</view>
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>礼盒数量</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.gift" name="gift" class="input-box" placeholder="请输入礼盒数量"
								placeholder-class="desc-placeholder">
						</view>
					</view>
					<view class="red">成为合伙人以后的礼盒数量</view>


					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>直升会员等级</text>
						</view>
						<view class="desc">
							<picker mode="selector" :range="tableTypeList" range-key="name"
								@change="onTableTypeChange(0,$event)">
								<view class="box-pack-between">
									<text>
										<block v-if="ClassIndex == -1">请选择直升会员等级</block>
										<block v-else>{{tableTypeList[ClassIndex].name}}</block>
									</text>
									<text class="iconfont icongengduo"></text>
								</view>
							</picker>
						</view>
					</view>
					<view class="red">购买此礼包后可直接成为此会员等级</view>


					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>赠送积分</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.integral" name="integral" class="input-box"
								placeholder="请输入赠送积分" placeholder-class="desc-placeholder">
						</view>
					</view>
					<view class="red">成为合伙人以后赠送的积分</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>赠送余额</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.balance" name="balance" class="input-box"
								placeholder="请输入赠送余额" placeholder-class="desc-placeholder">
						</view>
					</view>
					<view class="red">成为合伙人以后赠送的余额</view>

					<view style="padding: 25rpx 0;">
						<view class="title" style="color: blue;">以下为分享礼盒的设置</view>
					</view>


					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>直升会员</text>
						</view>
						<view class="desc">
							<picker mode="selector" :range="tableTypeList" range-key="name"
								@change="onTableTypeChange(1,$event)">
								<view class="box-pack-between">
									<text>
										<block v-if="ClassIndexTwo == -1">请选择直升会员</block>
										<block v-else>{{tableTypeList[ClassIndexTwo].name}}</block>
									</text>
									<text class="iconfont icongengduo"></text>
								</view>
							</picker>
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>赠送积分</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.share_intrgral" name="share_intrgral" class="input-box"
								placeholder="请输入赠送积分" placeholder-class="desc-placeholder">
						</view>
					</view>



					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>礼盒数量</text>
						</view>
						<view class="desc">
							<input type="digit" :value="info.share_gift" name="share_gift" class="input-box"
								placeholder="请输入礼盒数量" placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block">
							<text>礼盒描述</text>
						</view>
						<view class="desc">
							<textarea :value="info.gitf_content" maxlength="500" name="gitf_content"
								class="giftDescribe" placeholder="请输入礼盒描述" placeholder-class="desc-placeholder" />
						</view>
					</view>

				</view>
			</view>

			<view class="footer-bar" style="z-index: 999;">
				<view class="content flex flex-center" style="background: none;">
					<button class="btn" hover-class="none" form-type="submit">
						{{id?'立即修改':'立即添加'}}
					</button>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sjSwitch: [
					'启用', '禁用'
				],
				sjSwitchIndex: -1, //上架开关
				show_arr: [
					'仅微信', '仅支付宝', '微信和支付宝'
				],
				showIndex: -1, //显示位置
				ClassIndex: -1, //直升会员等级索引
				ClassIndexTwo: -1, //直升会员索引
				id: "",
				info: {},
				tableTypeList: [],
				admin_id: '',
				pageType: '', //页面类型1是列表进入的2是详情进入
			}
		},

		onLoad(e) {
			this.admin_id = uni.getStorageSync("admin_id");
			this.id = e.id ? e.id : '';
			uni.setNavigationBarTitle({
				title: e.id ? '编辑合伙人等级' : '添加合伙人等级'
			})
			this.shop_store_index();
		},
		methods: {
			//上架开关
			chooseSwicth(index) {
				this.sjSwitchIndex = index
			},

			//选择显示方式
			chooseShowStatus(index) {
				this.showIndex = index
			},

			//会员等级列表
			shop_store_index() {
				this.request({
					url: 'smdcshop/user_level',
					data: {
						admin_id: this.admin_id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.tableTypeList = res.data;
						if (this.id) {
							this.getData(this.id)
						}
					}
				})
			},

			//选择直升会员以及等级
			onTableTypeChange(index, e) {
				if (index == 0) {
					this.ClassIndex = e.detail.value;
				} else {
					this.ClassIndexTwo = e.detail.value;
				}
			},



			getData(id) {
				this.request({
					url: 'smdcshop/shared_level_info',
					data: {
						id
					}
				}).then(res => {
					if (res.code == "200") {
						this.info = res.data;
						this.sjSwitchIndex = Number(res.data.status) - 1;
						this.showIndex = Number(res.data.location) - 1;
						let classList = this.tableTypeList;
						if (classList.length > 0) {
							for (let i = 0; i < classList.length; i++) {
								console.log(classList[i])
								if (classList[i].id == res.data.level) {
									this.ClassIndex = i;
								}
								if (classList[i].id == res.data.share_level) {
									this.ClassIndexTwo = i;
								}

							}
						}
					}
				})
			},


			onSubmit(e) {
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2]; //上级页面
				let zsClass = this.ClassIndex; //直升会员等级索引
				let zsClassTwo = this.ClassIndexTwo; //直升会员索引
				let params = e.detail.value;
				params.admin_id = this.admin_id ? this.admin_id : '';
				params.id = this.id ? this.id : '';
				params.status = Number(this.sjSwitchIndex) + 1;
				params.location = Number(this.showIndex) + 1;
				params.level = zsClass == -1 ? '' : this.tableTypeList[zsClass].id;
				params.share_level = zsClassTwo == -1 ? '' : this.tableTypeList[zsClassTwo].id;

				if (!params.shared_level) {
					uni.showToast({
						title: "请填写等级",
						icon: 'none'
					})
					return false
				}

				if (!params.name) {
					uni.showToast({
						title: "请填写等级名称",
						icon: 'none'
					})
					return false
				}

				if (!params.amount) {
					uni.showToast({
						title: "请填写进阶费用",
						icon: 'none'
					})
					return false
				}

				if (!params.money) {
					uni.showToast({
						title: "请填写缴纳费用",
						icon: 'none'
					})
					return false
				}

				if (!params.direct_commission) {
					uni.showToast({
						title: "请填写直接佣金比例",
						icon: 'none'
					})
					return false
				}

				if (!params.indirect_commission) {
					uni.showToast({
						title: "请填写间接佣金比例",
						icon: 'none'
					})
					return false
				}

				if (Number(params.direct_commission) + Number(params.indirect_commission) > 100) {
					uni.showToast({
						title: "直接+间接比例不能超100%",
						icon: 'none'
					})
					return false
				}

				if (this.sjSwitchIndex == -1) {
					uni.showToast({
						title: "请选择上架开关",
						icon: 'none'
					})
					return false
				}

				if (this.showIndex == -1) {
					uni.showToast({
						title: "请选择显示位置",
						icon: 'none'
					})
					return false
				}

				if (!params.gift) {
					uni.showToast({
						title: "请填写礼盒数量",
						icon: 'none'
					})
					return false
				}

				if (this.ClassIndex == -1) {
					uni.showToast({
						title: "请选择直升会员等级",
						icon: 'none'
					})
					return false
				}

				if (!params.integral) {
					uni.showToast({
						title: "请填写赠送积分",
						icon: 'none'
					})
					return false
				}

				if (!params.balance) {
					uni.showToast({
						title: "请填写赠送余额",
						icon: 'none'
					})
					return false
				}

				if (this.ClassIndexTwo == -1) {
					uni.showToast({
						title: "请选择直升会员",
						icon: 'none'
					})
					return false
				}

				if (!params.share_intrgral) {
					uni.showToast({
						title: "请填写赠送积分",
						icon: 'none'
					})
					return false
				}

				if (!params.share_gift) {
					uni.showToast({
						title: "请填写礼盒数量",
						icon: 'none'
					})
					return false
				}

				this.request({
					url: 'smdcshop/level_save',
					data: params
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						prevPage.__data__.page = 1;
						prevPage.__data__.list = [];
						prevPage.$vm.getData(1)
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});


			},
		}
	}
</script>

<style>
	@import url("../static/css/common.css");

	page {
		background-color: #F4F5F7;
	}

	.checklist-box {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-webkit-flex-direction: row;
		flex-direction: row;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		margin: 5px 0;
		margin-right: 22px;
	}

	.radio__inner {
		-webkit-flex-shrink: 0;
		flex-shrink: 0;

		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-pack: center;
		-webkit-justify-content: center;
		justify-content: center;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		position: relative;
		border: 1px solid #DCDFE6;
		border-radius: 2px;
		box-sizing: border-box;
		width: 16px;
		height: 16px;
		border-radius: 16px;
		background-color: #fff;
		z-index: 1;
		-webkit-transition: border-color .3s;
		transition: border-color .3s;
	}

	.radio__inner-icon {
		width: 8px;
		height: 8px;
		border-radius: 10px;
		opacity: 0;
		-webkit-transition: -webkit-transform .3s;
		transition: -webkit-transform .3s;
		transition: transform .3s;
		transition: transform .3s, -webkit-transform .3s;
	}

	.is-default-checked {
		opacity: 1;
		background-color: #007aff;
		-webkit-transform: rotate(45deg) scaleY(1);
		transform: rotate(45deg) scaleY(1);
	}

	.checklist-text {
		font-size: 28rpx;
		margin-left: 5px;
		-webkit-transition: color 0.2s;
		transition: color 0.2s;
		color: #000;
	}

	.act_border {
		border: 1rpx solid #007aff;
	}

	.giftDescribe {
		width: 100%;
		padding: 30rpx;
	}

	.red {
		color: red;
		padding: 10rpx 25rpx;
		font-size: 26rpx;
	}
</style>
