<template>
  <view class="nav">
		<view class="navbar">
			<button class="scanbara" @click="handleScanbara">复核扫码</button>
			<button class="scanbara1" @click="gotoPrint">打印设置</button>
			<view class="status">{{props.workStatus === '0'?"接单中":"暂停接单"}}</view>
			<view class="userinfo">
				<button class="lef" @click="dialogToggle">{{props.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.name?props.userInfo.name:"张三三"}}</text>
					<view size="mini" @click="handleLogout" class="logout">退出<uni-icons class="item" type="forward" color="#ff" size="16"></uni-icons></view>
				</view>
			</view>
		</view>
  </view>
	
	<view v-if="props.workStatus==='0'">
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="center" cancelText="取消" confirmText="同意" title="温馨提示" content="你确定要暂停接单吗？" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
	<view v-else>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="center" cancelText="取消" confirmText="同意" title="温馨提示" content="你确定要开始接单吗？" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
	<view class="print">
		<uni-popup ref="alertPrint" type="dialog">
			<print-config @emit-close="emitclose"></print-config>
		</uni-popup>
	</view>
</template>

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

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

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

const {proxy} = getCurrentInstance()

const emit = defineEmits(['updateUser'])

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

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

const dialogToggle = ()=>{
	proxy.$refs.alertDialog.open()
}

const dialogConfirm = ()=>{
	handleChange()
}
const dialogClose = ()=>{
	console.log('guanbi')
}

const emitclose =()=>{
	
	proxy.$refs.alertPrint.close()
}

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

const handleScanbara = ()=>{
	
	uni.scanCode({
		scanType: ['barCode'],
		success:  async(res) => {
			console.log('条码类型：' + res.scanType);
			console.log('条码内容：' + res.result);
			const data = JSON.stringify({
					userId:uni.getStorageSync('userId'),
					coreId: res.result
				})
			// neo
			// 调用接口查询这个preNo是否已经被复核，如果已经复核了就不跳
			const {data:res1} = await saveReview({
				token: uni.getStorageSync('token'),
				data: data
			})
			if(res1.code===200){
				console.log('depoly success>>>')
				// proxy.$refs.alertDialog.open()
				console.log('res1',res1)
			}
			
			
			if(res.result) {
				uni.navigateTo({
					url:`/pages/review/review?preNo=${res.result}&depolyStatus=2`
				})
			}
		}
	});
}

const handleLogout = async()=>{
	console.log("handleLogout", 555)
	
	const params1 = {
		token: uni.getStorageSync('token'),
		data: {
			pharmacistUserId: uni.getStorageSync('userId'),
			workStatus: '1'
		}
	}
	console.log('params1.data.workStatus', params1)
	
	const {data:res1} = await updateWorkStatus(params1)
	if (res1.code === 200) {
		uni.setStorageSync('workStatus', '1');
		emit('updateUser', params1.data.workStatus)
		
	} else {
		emit('updateUser', props.workStatus)
	}
	
	
	const params = {
		token: uni.getStorageSync('token'),
		data: {
			pharmacistUserId: uni.getStorageSync('userId'),
			onlineStatus: '1'
		}
	}
	const {data:res} = await updateOnlineStatus(params)
	uni.redirectTo({
		url:"/pages/login/login"
	})
	
}

const gotoPrint = ()=>{
	proxy.$refs.alertPrint.open()
}

</script>

<style lang="scss">
	:deep(.uni-button-color){
		color: #18c7b0!important;
	}
.nav{
	display:flex;
	justify-content:space-between;
	align-items:flex-end;
	width: 750rpx;
	align-items: center;
	height: 75rpx;
	background-color: #18C7B0;
	padding-top: 43rpx;
	.navbar{
		width: 750rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.scanbara{
			display: flex;
			justify-content:center;
			align-items: center;
			
			width: 91rpx;
			height: 32.8rpx;
			font-size: 15rpx;
			color:#18C7B0;
			margin-left: 20.1rpx;
		}
		.scanbara1{
			display: flex;
			justify-content:center;
			align-items: center;
			
			width: 91rpx;
			height: 32.8rpx;
			font-size: 15rpx;
			color:#18C7B0;
			margin-left: 10rpx;
		}
		.status{
			font-size: 18.75rpx;
			color:#fff;
			margin-right: 110.47rpx;
		}
		.userinfo{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-right: 22.19rpx;
			.lef{
				height: 33.75rpx;
				width: 91rpx;
				// padding: 14rpx;
				margin-right: 17.5rpx;
				font-size: 15rpx;
				color:#18C7B0;
				// margin: 0 20px;
			}
			.mid{
				height: 43.75rpx;
				width: 43.75rpx;
				margin-right: 5.94rpx;
				border-radius: 50%;
			}
			.rig{
				// margin-right: 40px;
				display: flex;
				flex-direction: column;
				align-items: center;
				.name{
					// margin-bottom: 10px;
					// width: 50rpx;
					font-size: 17.5rpx;
					color: #fff;
					 text-align: center;
				}
				.logout{
					// width: 50rpx;
					background-color: #18C7B0;
					font-size: 15rpx;
					color: #fff;
					border: 1px solid #18C7B0;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
}
</style>