<template>
  <view class="nav">
		<view class="navbar">
			<button class="scanbara" @click="handleScanbara">复核扫码</button>
			<view class="status">{{props.userInfo.workStatus === '0'?"接单中":"暂停接单"}}</view>
			<view class="userinfo">
				<button class="lef" @click="handleChange">{{props.userInfo.workStatus === '0'?"暂停接单":"开始接单"}}</button>
				<image class="mid" aspectFill src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/cat-1.png" />
				<view class="rig">
					<text class="name">{{props.userInfo.name?props.userInfo.name:"张三三"}}</text>
					<view size="mini" @click="handleLogout" class="logout">退出<uni-icons class="item" type="forward" color="#ff" size="20"></uni-icons></view>
				</view>
			</view>
		</view>
  </view>
</template>

<script>
  export default {
    name: 'NavBar'
  }
</script>

<script setup>
import { onMounted,ref } from 'vue'
import {updateWorkStatus, updateOnlineStatus } from "@/static/js/api.js"

const props = defineProps({
	userInfo: {
		default: function() {
			return {}
		},
		type: Object
	}
})

const emit = defineEmits(['updateUser'])

console.log("props.userInfo.workStatus", props.userInfo.workStatus)

onMounted(() => {
	// userInfo.value = props.userInfo
	// console.log("props.userInfo.name", props.userInfo.name)
})

const handleChange =async()=>{
	console.log("hangdleChange props.userInfo.workStatus", props.userInfo.workStatus)
	const token = uni.getStorageSync('token')
	const userId = uni.getStorageSync('userId')
	if(props.userInfo.workStatus){
		const params = {
			token: token,
			data: {
				pharmacistUserId: userId,
				workStatus: props.userInfo.workStatus ==='1'?'0':'1'
			}
		}
		console.log('params.data.workStatus', params)
		
		const {data:res} = await updateWorkStatus(params)
		if (res.code === 200) {
			emit('updateUser', params.data.workStatus)
		}
		emit('updateUser', props.userInfo.workStatus)
	}
}

const handleScanbara = ()=>{
	uni.scanCode({
		scanType: ['barCode'],
		success: function (res) {
			console.log('条码类型：' + res.scanType);
			console.log('条码内容：' + res.result);
			if(res.result) {
				uni.navigateTo({
					url:`/pages/edit/edit?preId=${res.result}&depolyStatus=2`
				})
			}
		}
	});
}

const handleLogout = async()=>{
	console.log("handleLogout", 555)
	const tokens = uni.getStorageSync('token')
	const userId = uni.getStorageSync('userId')
	const params = {
		token: tokens,
		data: {
			pharmacistUserId: userId,
			onlineStatus: 1
		}
	}
	const {data:res} = await updateOnlineStatus(params)
	uni.redirectTo({
		url:"/pages/login/login"
	})
	
}

</script>

<style lang="scss">
.nav{
	display:flex;
	justify-content:flex-end;
	align-items:flex-end;
	width: 1200px;
	
	background-color: #18C7B0;
	padding-bottom: 20rpx;
	// position: fixed;
	.navbar{
		width: 1200px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.scanbara{
			// display: flex;
			height: 54px;
			font-size: 20px;
			color:#18C7B0;
			margin: 0 33px;
		}
		.status{
			font-size: 20rpx;
			color:#fff;
			margin-left: 200px;
		}
		.userinfo{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.lef{
				height: 54px;
				// width: 60rpx;
				// padding: 14rpx;
				margin-right: 30rpx;
				font-size: 20px;
				color:#18C7B0;
				margin: 0 20px;
			}
			.mid{
				height: 50rpx;
				width: 50rpx;
				margin-right: 20px;
				border-radius: 50%;
			}
			.rig{
				margin-right: 40px;
				display: flex;
				flex-direction: column;
				align-items: center;
				.name{
					margin-bottom: 10px;
					// width: 50rpx;
					font-size: 28px;
					color: #fff;
					 text-align: center;
				}
				.logout{
					// width: 50rpx;
					background-color: #18C7B0;
					font-size: 24px;
					color: #fff;
					border: 1px solid #18C7B0;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
}
</style>