<template>
	<view class="setting_wrap">
		<u-navbar title="我的设置" :autoBack="true" :placeholder="true" v-hideNav></u-navbar>
		<view class="option_list">
			<!-- 只有当showRoleSwitch为true时才显示切换权限按钮 -->
			<view v-if="$store.state.user.showRoleSwitch" class="option_item" @click="handleSwitchRole">
				<view class="item_left" >
					<image src="@/static/parson/permission.png" mode=""></image>
					<text class="label">切换权限</text>
				</view>
				<view class="item-right">
					<u-icon name="arrow-right" color="#aaa"></u-icon>
				</view>
			</view>
			<view class="option_item" @click="toPersonal">
				<view class="item_left">
					<image src="@/static/parson/icon-gezl.png" mode=""></image>
					<text class="label">个人资料</text>
				</view>
				<view class="item-right">
					<u-icon name="arrow-right" color="#aaa"></u-icon>
				</view>
			</view>
			<view class="option_item" @click="handleEditPassword">
				<view class="item_left">
					<image src="@/static/parson/updatePass.png" mode=""></image>
					<text class="label">修改密码</text>
				</view>
				<view class="item-right">
					<u-icon name="arrow-right" color="#aaa"></u-icon>
				</view>
			</view>

			<option-list></option-list>

		</view>
		<view class="logout_btn">
			<u-button type="warning" :plain="true" text="退出账号" size="large" @click="handleShowModal"></u-button>
		</view>
		<u-picker :show="showSwitchRole" :columns="columns" keyName="label" :defaultIndex="defaultIndex" @confirm="confirmSwitchRole" @cancel="showSwitchRole = false"></u-picker>
		<u-modal :show="showModal" 
			@confirm="confirm" 
			ref="uModal" 
			:asyncClose="true" 
			showCancelButton
			@cancel="showModal = false"
			cancelColor="#409eff"
			:confirmText="'退出登录'" confirmColor="#f8836c">
			<view class="modal_box">
				确定要退出当前账号
			</view>
		</u-modal>
	</view>
</template>
<script>
	import OptionList from '@/pages/person/option/index.vue'
	export default {
		components: {
			OptionList
		},
		data() {
			return {
				showModal: false,
				showSwitchRole: false,
				columns: [],
				defaultIndex: [this.$store.state.user.currentRoleIndex],
				isHaveCRM: false,
				isHaveSalesman: false,
				isHaveWarehouse: false,
			}
		},
		methods: {
			handleSwitchRole(){
					this.columns = [];
					let roles = this.$store.state.user.availableRoles;
					let roleOptions = [];
					const hasAdminRole = roles.some(role => role == 'admin');
					const hasCrmRole = roles.some(role => role.includes('CRM'));
					const hasWarehouser = roles.some(role => role == 'WAREHOUSER');
					const hasSalesman = roles.some(role => role == 'SALESMAN');

					if(hasAdminRole){
						roleOptions.push(
							{ label: 'CRM权限', value: 'CRM' },
							{ label: '仓库权限', value: 'WAREHOUSER' },
							{ label: '销售权限', value: 'SALESMAN' },
						);
					}else if(hasWarehouser && hasSalesman && hasCrmRole){
						roleOptions.push(
							{ label: 'CRM权限', value: 'CRM' },
							{ label: '仓库权限', value: 'WAREHOUSER' },
							{ label: '销售权限', value: 'SALESMAN' }
						);
					}else if(hasCrmRole && hasWarehouser){
						roleOptions.push(
							{ label: 'CRM权限', value: 'CRM' },
							{ label: '仓库权限', value: 'WAREHOUSER' }
						);
					}else if(hasCrmRole && hasSalesman){
						roleOptions.push(
							{ label: 'CRM权限', value: 'CRM' },
							{ label: '仓库权限', value: 'WAREHOUSER' },
							{ label: '销售权限', value: 'SALESMAN' }
						);
					}else if(hasWarehouser && hasSalesman){
						roleOptions.push(
							{ label: '仓库权限', value: 'WAREHOUSER' },
							{ label: '销售权限', value: 'SALESMAN' }
						);
					}else if(hasCrmRole){
						roleOptions.push({ label: 'CRM权限', value: 'CRM' });
					}else if(hasWarehouser){
						roleOptions.push({ label: '仓库权限', value: 'WAREHOUSER' });
					}else if(hasSalesman){
						roleOptions.push({ label: '销售权限', value: 'SALESMAN' },{ label: '仓库权限', value: 'WAREHOUSER' });
					}
					
					this.columns = [roleOptions];
					this.showSwitchRole = true;
				},
			handleEditPassword(){
				uni.navigateTo({
					url : "/pages/changePw/index"
				});
			},
			toPersonal(){
				uni.navigateTo({
					url : "/pages/profile/index"
				})
			},
			handleShowModal() {
				this.showModal = true;
			},
			confirm() {
				// 停止电话监听
				//#ifdef APP-PLUS
				this.$store.dispatch("call/stopPhoneListener", null, { root: true }).catch(error => {
					console.error('退出登录时停止电话监听失败:', error);
				});
				//#endif
				
				this.$store.dispatch("user/logout").then(()=>{
					uni.$u.toast("退出成功");
					setTimeout(()=>{
						this.showModal = false;
						uni.reLaunch({
							url: "/pages/login/index"
						})
					},1000)
				})
			},
			confirmSwitchRole(e) {
				uni.navigateBack({
					delta: 1
				}).then(()=>{
					this.showSwitchRole = false;
					this.$store.commit('user/SET_CURRENT_ROLE_INDEX', e.indexs[0]);
					setTimeout(()=>{
						this.$store.commit('user/SWITCH_ROLE', e.value[0]);
					},50)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.modal_box{
		line-height: 100rpx;
		color: #aaa;
	}
	.logout_btn {
		width: 100%;
		position: absolute;
		left: 50%;
		transform: translate(-50%, 0%);
		bottom: 10%;

		::v-deep .u-button {
			border: none;
		}
	}

	.option_list {

		.option_item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100rpx;
			border-bottom: 2rpx solid #dfdfdf;
			background-color: #fff;
			padding: 0 30rpx;

			&:last-child {
				border: none;
			}

			.item_left {
				display: flex;
				align-items: center;

				image {
					width: 32rpx;
					height: 32rpx;
					margin-right: 20rpx;
				}

				.label {
					font-size: 28rpx;
					color: #202020;
				}
			}

			.item-right {
				display: flex;
				justify-content: space-between;
				align-items: center;

				::v-deep .u-icon__icon {
					font-size: 30rpx !important;
				}

				text {
					font-size: 24rpx;
					color: #999999;
					margin-right: 20rpx;
				}

				.copy_btn {
					display: flex;
					font-size: 24rpx;
					color: #108CFF;
					margin-right: 30rpx;
				}
			}
		}
	}
</style>
