<template>
	<view class="page_v u-flex-col">
		<view class="u-m-t-20 u-m-b-20">
			<JnpfAlert type="warning" :title="description" effect="dark" />
		</view>
		<view class="organization_box u-m-b-20" v-if="show" v-for="(item,index) in list" :key="index"
			:class="item.isDefault ? 'active' : '' " @click="clickRadio(item)">
			<view class="icon-checked-box" v-if="item.isDefault">
				<text>默认</text>
				<view class="icon-checked">
					<u-icon name="checkbox-mark" color="#fff" size="28"></u-icon>
				</view>
			</view>
			<view class="list_inner u-flex-col">
				<view class="inner_item">
					<text>所属组织</text>
					<text class="txt u-line-1"
						:style="{color:item.isDefault ? '#2979FF' : ''}">{{item.orgTreeName}}</text>
				</view>
				<view class="inner_item">
					<text>任职岗位</text>
					<text class="txt u-line-1" :style="{color:item.isDefault ? '#2979FF' : ''}">{{item.fullName}}</text>
				</view>
				<view class="inner_item">
					<text>上级岗位</text>
					<text class="txt u-line-1"
						:style="{color:item.isDefault ? '#2979FF' : ''}">{{ item.parentName || '-' }}</text>
				</view>
				<view class="inner_item">
					<text>上级责任人</text>
					<text class="txt u-line-1"
						:style="{color:item.isDefault ? '#2979FF' : ''}">{{item.managerName}}</text>
				</view>
			</view>
		</view>
		<JnpfEmpty v-if="!show"></JnpfEmpty>
	</view>
</template>

<script>
	import {
		getUserPositions,
		setMajor,
	} from '@/api/common'

	export default {
		data() {
			return {
				list: [],
				majorType: '',
				show: true,
				disabled: false,
				userInfo: {},
				description: '默认组织仅逐级审批时使用'
			}
		},
		onLoad(e) {
			this.userInfo = uni.getStorageSync('userInfo') || {}
			this.majorType = e.majorType
			this.getUserPositions()
			uni.setNavigationBarTitle({
				title: this.$t('app.my.organization')
			})
		},

		methods: {
			getUserPositions() {
				getUserPositions().then(res => {
					let data = res.data || []
					if (!data.length) return this.show = this.list.length > 0
					this.list = JSON.parse(JSON.stringify(data));
					this.show = this.list.length > 0
					this.list.map(o => {
						if (o.currentStanding) o.isDefault = true
					})
				})
			},
			clickRadio(item) {
				if (this.disabled || item.isDefault) return
				this.changeMajor(item.id)
			},
			change(id) {
				this.list.map((o, i) => {
					o.isDefault = false;
					if (o.id === id) o.isDefault = true;
				})
			},
			changeMajor(majorId) {
				let query = {
					majorId,
					majorType: this.majorType
				}
				setMajor(query).then(res => {
					this.$u.toast(res.msg)
					if (res.code === 200) {
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/index/index"
							})
						}, 500)
					}
				}).catch(() => {})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f0f2f6;
	}

	.page_v {
		/* #ifdef MP */
		background-color: #f0f2f6;
		/* #endif */
		padding: 0 20rpx;

		.active {
			border: 1rpx solid #2979FF;
			color: #2979FF;
		}

		.organization_box {
			width: 100%;
			border-radius: 8rpx;
			position: relative;
			display: flex;
			flex-direction: column;
			justify-content: center;
			background-color: #FFFFFF;


			.icon-checked-box {
				position: absolute;
				display: flex;
				width: 140rpx;
				height: 80rpx;
				transform: scale(0.9);
				right: -4rpx;
				bottom: -2rpx;
				flex-direction: row;
				align-items: center;

				.icon-checked {
					width: 44rpx;
					height: 44rpx;
					border: 40rpx solid #1890ff;
					border-left: 40rpx solid transparent;
					border-top: 40rpx solid transparent;
					border-bottom-right-radius: 12rpx;
					position: absolute;
					transform: scale(0.95);
					right: -8rpx;
					bottom: -6rpx;
				}
			}

			.list_inner {
				width: 100%;
				min-height: 160rpx;
				padding: 0 20rpx;
				align-items: flex-start;

				.inner_item {
					width: 100%;
					padding: 20rpx 0;
					display: table-row;
				}

				.txt {
					flex: 1.2;
					white-space: nowrap;
					color: #606266;
					width: 400rpx;
					overflow-x: auto;
					display: inline-flex;
					margin-left: 40rpx;
				}
			}
		}
	}
</style>