<template>
	<view class="managementadd">
		<view class="managementadd-header">
			<view class="managementadd-header-text">

				<view class="managementadd-header-text-neishi">
					<view class="managementadd-header-text-icon" @click="topgi">
						<image src="https://cdn.bjtdba.com/vod/image/2022-08-11/20220811133828415.png" mode="widthFix">
						</image>
					</view>
				</view>

				<view class="managementadd-header-text-title">
					添加部门
				</view>
			</view>
		</view>
		<view class="managementadd-list">
			<view class="managementadd-list-let">
				部门
			</view>
			<view class="managementadd-list-rig">
				<input class="uni-input" focus="true" placeholder="请填写" v-model="from.branch" style="text-align:right"
					placeholder-class="plaClss" placeholder-style="color:#333333;font-size:30rpx" />
			</view>
		</view>
		<view class="managementadd-title">
			部门权限
		</view>
		<view class="managementadd-neirong">
			<!-- <radio-group @change="radioChange"> -->
			<label style="" v-for="(item, index) in jurisdictionList" :key="index" @click="oncliki(item)"
				style="display: flex;">
				<view style="margin:32rpx 0 22rpx 32rpx">
					<radio :value="item.name" color="#1F7CFF" :checked="item.checked" />
				</view>
				<view style="margin:32rpx 0 22rpx 32rpx">{{item.name}}</view>
			</label>
			<!-- </radio-group> -->
		</view>


		<view class="managementadd-annkiu" @click="addto">
			确认添加
		</view>
	</view>
</template>

<script>
	import {
		addStoreBranch,
		queryJurisdiction
	} from '@/commit/api.js'
	export default {
		data() {
			return {
				jurisdictionList: [],
				current: false,
				from: {
					merId: "330",
					branch: "",
					jurisdiction: ""
				},
				jurisdictionId: []

			}
		},
		onLoad() {
			let selectmerchant = uni.getStorageSync('selectmerchant')
			this.from.merId = selectmerchant.merId
			this.jurisdiction()
		},
		methods: {
			//查询所有权限
			jurisdiction() {
				queryJurisdiction().then(res => {
					if (res.data.status == 200) {
						this.jurisdictionList = res.data.data.jurisdictionList
					}
				}, fail => {
					console.log(fail)
				})
			},
			//添加部门
			addto() {
				let jurisdictionId = this.jurisdictionId
				this.from.jurisdiction = jurisdictionId.toString()
				if (this.from.branch == "") {
					uni.showToast({
						title: "请选择部门",
						icon: 'none'
					})
				} else if (this.from.jurisdiction == "") {
					uni.showToast({
						title: "请选择部门权限",
						icon: 'none'
					})
				} else {
					addStoreBranch(this.from).then(res => {
						if (res.data.status == 200) {
							uni.showToast({
								title: res.data.message,
								icon: 'success',
							})
							setTimeout(function() {
								uni.redirectTo({
									url: "/shop/department/management"
								})
							}, 600);
						}
					}, fail => {
						console.log(fail)
					})
				}
			},
			oncliki(e) {
				let tath = this
				let id = e.id;
				let a = this.jurisdictionList
				let array = this.jurisdictionId
				a.forEach((item,index) => {
					if (id == item.id) {
						if (item.checked == true) {
							item.checked = false
							array.forEach((itms,inds)=>{
								if(id == itms){
									array.splice(inds, 1);
								}
							})
						} else {
							item.checked = true
							array.push(item.id)
						}
						return item
					}
				})
				this.jurisdictionList = []
				this.jurisdictionList = a
				this.jurisdictionId = array
			},
			topgi() {
				uni.redirectTo({
					url: './management'
				});
			}
		}
	}
</script>

<style>
	page {
		background-color: #F7F8FC;
	}

	.plaClss {
		text-align: right;
	}
</style>
<style lang="scss" scoped>
	.managementadd {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

		.managementadd-header {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 111;
			width: 100%;
			height: 176rpx;
			background: #FFFFFF;
			display: flex;
			justify-content: center;

			.managementadd-header-text {
				width: 686rpx;
				font-size: 36rpx;
				font-weight: 500;
				color: #333333;
				display: flex;
				margin-top: 107rpx;
				align-items: center;

				.managementadd-header-text-neishi {
					width: 45rpx;
					height: 45rpx;
					margin-right: 17rpx;

					.managementadd-header-text-icon {
						width: 36rpx;
						height: 36rpx;
						margin-top: 2rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}
				}
			}
		}

		.managementadd-list {
			width: 686rpx;
			height: 118rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			margin: 202rpx 0 32rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 30rpx;
			font-weight: 400;
			color: #333333;

			.managementadd-list-let {
				margin-left: 32rpx;
			}

			.managementadd-list-rig {
				margin-right: 32rpx;

				input {
					width: 200rpx;
				}
			}
		}

		.managementadd-title {
			width: 686rpx;
			margin: 32rpx 0 26rpx 0;
			font-size: 32rpx;
			font-weight: 500;
			color: #333333;
		}

		.managementadd-neirong {
			width: 686rpx;
			height: 100%;
			background: #FFFFFF;
			border-radius: 8rpx;

		}

		.managementadd-annkiu {
			width: 686rpx;
			height: 98rpx;
			background: #1F7CFF;
			border-radius: 8rpx;
			position: fixed;
			bottom: 94rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 98rpx;
		}
	}
</style>
