<template>
	<view class="page">
		<u-navbar :title="tit" :border-bottom="false" :background="bgc" title-color='#fff' back-icon-color="#fff" title-size='36' height='50'></u-navbar>
        <view class="box">
        	<view class="title">
        		基本信息
        	</view>
			<view class="list">
				<view class="list_val">
					<view class="" style="padding-top:12rpx">合伙人名称</view> <input type="text" v-model="level"  placeholder="请输入合伙人名称" />
				</view>
				<view class="list_val" style="position: relative;">
					<view class="" style="padding-top:12rpx">分成比例</view> <input type="text" v-model="ratio" placeholder="请输入分成比例%" /> <view class="baiffen">%</view>
				</view>
				<!-- <view class="list_val">
					<view class="" style="padding-top:12rpx">手机号码</view> <input type="text" v-model="tel" placeholder="请输入手机号码" />
				</view> -->
				<!-- <view class="list_val" style="position: relative;">
					<view class="" style="padding-top:12rpx;width: 176rpx;">绑定店铺</view> <input @click="show = true" type="text" disabled="false" v-model="sheng" :placeholder="sheng" /> 
					<u-icon	name="arrow-down" style="position: absolute;top: 30%;right: 14rpx;" color="#808080"></u-icon>
					<u-select v-model="show" :list="arr" @confirm="confirm"></u-select>
				</view> -->
			</view>
			<view class="title">
				商户权限
			</view>
			<view class="swlist">
				<view class="swlist_val" v-for="(item,index) in list" :key="index">
					<view class="lt">
						<view class="one">
							{{item.name}}
						</view>
						<view class="two">
							{{item.tit}}
						</view>
					</view>
					<view class="rt">
						<u-switch v-model="item.checked"  active-color="#8883F0 " size="36" inactive-color="#eee"></u-switch>
					</view>
				</view>
				
			</view>
			<view class="baocun" @click="btncj">
				{{tit}}
			</view>
        </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[
					{name:'查看店铺',tit:'是否允许查看店铺',checked:false,txt:1},
					{name:'操作店铺',tit:'是否允许操作店铺',checked:false,txt:2},
					{name:'查看设备',tit:'是否允许查看设备',checked:false,txt:3},
					{name:'操作设备',tit:'是否允许操作设备',checked:false,txt:4},
					{name:'查看订单',tit:'是否允许查看订单',checked:false,txt:5},
					{name:'操作订单',tit:'是否允许操作订单',checked:false,txt:6},
				],
				show:false,
				bgc: {
					backgroundColor: " #8883F0",
				},
				level:'',
				name:'',
				ratio:'',
				tel:'',
				sheng:'请选择店铺',
				arr:[],
				arrqx:[],
				agentLevel:'',
				storeId:'',
				employId:'',
				tit:'创建合伙人',
				storeId:''
			}
		},
		onLoad(option) {
			this.storeId = option.storeId
			if(option.employId){
				this.tit = '编辑合伙人'
				this.employId = option.employId
				this.getedit()
			}
			this.getlist()
		},
		methods: {
			// 请求编辑的合伙人
			getedit(){
				this.$u.get(`/mch/storeStaff/${this.employId}`).then(res => {
					if (res.code == 200) {
				        this.storeId = this.storeId
				        this.ratio = res.data.point
				        this.level = res.data.remark
				        this.arrqx = res.data.permissions
						this.sheng = res.data.storeName
						const txtToItemMap = {}
						this.list.forEach(item => {
						  txtToItemMap[item.txt] = item
						})
						this.arrqx.forEach(txt => {
						  const item = txtToItemMap[txt]
						  if (item) {
						    item.checked = true
						  }
						})
						
					}
				})
			},
			
			// 请求所有店铺
            getlist() {
            	this.$u.get(`/app/store/list?pageNum=1&pageSize=990&keyword=`).then(res => {
            		if (res.code == 200) {
                      this.arr = res.rows.map(item => ({ label: item.name, value: item.storeId }))
            		}
            	})
            },
			confirm(e){
				this.sheng = e[0].label
				this.storeId = e[0].value
			},
			// 点击创建
			btncj(){
				this.arrqx = []
				if(this.tit == '创建合伙人'){
					this.list.forEach(item => {
						if(item.checked == true){
							this.arrqx.push(item.txt)
						}
					})
					let data = {
						storeId:this.storeId,
						role:1,
						point:this.ratio,
						remark:this.level,
						enabled: true,
						permissions:this.arrqx
					}
					this.$u.post(`/mch/storeStaff/`,data).then(res => {
						if (res.code == 200) {
					       uni.showToast({
					       	title: '创建成功',
					       	icon: 'success',
					       	duration: 2000
					       })
						   setTimeout(()=>{
							   uni.navigateBack()
						   },2000)
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				}else{
					this.list.forEach(item => {
						if(item.checked == true){
							this.arrqx.push(item.txt)
						}
					})
					let data = {
						employId:this.employId,
						storeId:this.storeId,
						role:1,
						point:this.ratio,
						remark:this.level,
						enabled: true,
						permissions:this.arrqx
					}
					this.$u.put(`/mch/storeStaff/`,data).then(res => {
						if (res.code == 200) {
					       uni.showToast({
					       	title: '编辑成功',
					       	icon: 'success',
					       	duration: 2000
					       })
						   setTimeout(()=>{
							   uni.navigateBack()
						   },2000)
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-title,
	/deep/ .uicon-nav-back {
		padding-bottom: 22rpx;
	}
	page {

	}
    .baiffen{
		position: absolute;
		top: 14rpx;
		right: 20rpx;
	}
	.page {
		width: 750rpx;
		.box{
			width: 750rpx;
			height: 100%;
			overflow-y:scroll;
			background: #F4F5F7;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			padding: 32rpx 36rpx; 
			box-sizing: border-box;
			padding-bottom: 100rpx;
			.baocun{
				width: 584rpx; 
				height: 90rpx;
				margin: auto;
				background: linear-gradient( 270deg, #8883F0 0%, #8883F0 100%);
				border-radius: 54rpx 54rpx 54rpx 54rpx;
				margin-top: 110rpx;
				margin-bottom: 78rpx;
				font-weight: 500;
				font-size: 40rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 90rpx;
			}
			.swlist{
				margin-top: 34rpx;
				display: flex;
				justify-content: space-between;
				 flex-wrap: wrap;
				.swlist_val{
					display: flex;
					justify-content: space-between;
                    width: 324rpx;
                    height: 144rpx;
                    background: #FFFFFF;
                    border-radius: 24rpx 24rpx 24rpx 24rpx;		
					padding-top: 28rpx;
					padding-left: 38rpx;
					box-sizing: border-box;
					margin-top: 18rpx;
				}
				.lt{
					.one{
						font-size: 28rpx;
						color: #3D3D3D;
					}
					.two{
						font-size: 24rpx;
						color: #808080;
						margin-top: 12rpx;
					}
				}
				.rt{
					margin-top: 24rpx;
					padding-right: 12rpx;
				}
			}
			.title{
				font-size: 32rpx;
				color: #3D3D3D;
			}
			.list{
					width: 680rpx;
					max-height: 578rpx;
					background: #FFFFFF;
					border-radius: 24rpx 24rpx 24rpx 24rpx;
					padding: 46rpx 36rpx;
					box-sizing: border-box;
					margin-top: 34rpx;
					margin-bottom: 47rpx;
				.list_val{
					display: flex;
					font-size: 32rpx;
					color: #3D3D3D;
					justify-content: space-between;
					margin-bottom: 32rpx;
					input{
						width: 430rpx;
						height: 70rpx;
						background: #F0F0F0;
						border-radius: 12rpx 12rpx 12rpx 12rpx;
						padding-left: 32rpx;
						box-sizing: border-box;
						font-size: 28rpx;
						color: #808080;
					}
				}
			}
		}
	}
</style>