<template>
  <view class="nav">
		<view class="navbar">
			<view class="status">{{props.userInfo.workStatus === '0'?"接单中":"暂停接单"}}</view>
			<view class="userinfo">
				<button type="primary" 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>
					<button size="mini" type="primary" @click="handleLogout"  class="logout">退出</button>
				</view>
			</view>
		</view>
  </view>
</template>

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

<script setup>
import { onMounted,ref } from 'vue'
import {updateWorkStatus, updateOnlineStatus } from "@/static/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 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: #cfcfcf;
	padding-bottom: 20rpx;
	// position: fixed;
	.navbar{
		width: 440rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.status{
			font-size: 20rpx;
		}
		.userinfo{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.lef{
				height: 60rpx;
				width: 60rpx;
				padding: 14rpx;
				margin-right: 30rpx;
				font-size: 15rpx;
				line-height: 15rpx;
			}
			.mid{
				height: 50rpx;
				width: 50rpx;
				margin-right: 30rpx;
			}
			.rig{
				margin-right: 10rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				.name{
					margin-bottom: 10rpx;
					width: 50rpx;
					 text-align: center;
				}
				.logout{
					width: 50rpx;
				}
			}
		}
	}
}
</style>