<template>
	<view class="choosePrescription">
		<!-- 搜索框 -->
		<view class="searchPrescription">
			<uni-forms class="search_forms">
				<uni-forms-item label="处方编号">
					<uni-easyinput v-model="pageData.cfbh" placeholder="请输入处方编号" />
				</uni-forms-item>
				<uni-forms-item label="患者编码">
				  <uni-easyinput v-model="pageData.djdsh" placeholder="请输入患者编码" />
				</uni-forms-item>
				<uni-forms-item label="患者姓名">
					<uni-easyinput v-model="pageData.hzxm" placeholder="请输入患者姓名" />
				</uni-forms-item>
				<uni-forms-item label="医疗机构">
					<superwei-combox :candidates="yljgList.value" :isJSON="true" keyName="name" placeholder="请输入医疗机构"
					            v-model="pageData.deptName" @input="comboxInput" @select="comboxInput"></superwei-combox>
				</uni-forms-item>
			</uni-forms>
			<view class="search_button_group">
				<button class="reset_button" @click="reset">重置</button>
				<button class="search_button" @click="search">搜索</button>
			</view>
		</view>
		<!-- 处方列表 -->
		<uni-card v-for="(item,index) in pageData.cfInfo" class="cfInfo" @click="chooseCf(item)">
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">处方编号</view>
				<view class="cfInfo_text">{{item.cfbm}}</view>
			</view>
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">院内处方编号</view>
				<view class="cfInfo_text">{{item.yncfbh}}</view>
			</view>
      <view class="cfInfo_title">
        <view class="cfInfo_text_name">患者编码</view>
        <view class="cfInfo_text">{{item.djdsh}}</view>
      </view>
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">患者姓名</view>
				<view class="cfInfo_text">{{item.hzxm}}</view>
			</view>
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">医疗机构</view>
				<view class="cfInfo_text">{{item.yljgmc}}</view>
			</view>
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">开方日期</view>
				<view class="cfInfo_text">{{item.kfrq}}</view>
			</view>
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">贴数</view>
				<view class="cfInfo_text">{{item.ts}}</view>
			</view>
			<view class="cfInfo_title">
				<view class="cfInfo_text_name">处方内容</view>
				<view class="cfInfo_text">{{item.cfnr}}</view>
			</view>
		</uni-card>
		<!-- 对话框 -->
		<uni-popup ref="chooseDialog" type="dialog">
			<uni-popup-dialog type="info" confirmText="确定" cancelText="取消" title="选择处方" :content="pageData.popupMessage.showMessage" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<view>
			<uni-load-more :status="loadingData.status" :contentText="loadingData.contentText"></uni-load-more>
		</view>
	</view>
</template>

<script setup>
import { reactive,ref } from "vue";
import {onLoad,onShow,onUnload,onPullDownRefresh,onReachBottom} from "@dcloudio/uni-app";
import http from '@/utils/request';
import uniComboxVue from "../../../uni_modules/uni-combox/components/uni-combox/uni-combox.vue";
import uniCombox from "../../../uni_modules/uni-combox/components/uni-combox/uni-combox";

const pageData=reactive({
	//机构名称
	deptName:'',
	//机构Code
	deptCode:'',
	// 处方编号
	cfbh:'',
	// 平台唯一编号
	ptwybh:'',
	// 患者编码
	djdsh:'',
	// 患者姓名
	hzxm:'',
	// 处方信息列表
	cfInfo:[],
	// 处方状态
	cfStatus:'',
	// 发药方式类型
	fyfslx:'',
	chooseDialog:'',
	// 弹出框内容
	popupMessage:{
		showMessage:''
	}
})

const formData=reactive({
	cfbm:'',
	djdsh:'',
	hzxm:'',
	yljgdm:'',
	cflx:[],
	pageNum:1,
	pageSize:10
})

const orgName = reactive({});

const yljgList = reactive([]);

// 医疗机构选择事件
const comboxInput =(e)=>{
	formData.yljgdm = e.id
	console.log(e.id);
}

const comboxSelect =(e)=>{
	formData.yljgdm = e.id
}

// 获取医疗机构列表
const getYljgList = () =>{
	http.get("app-api/cf/findYljgList").then(res=>{
		yljgList.value = res
	})
}

const loadingData = reactive({
	// 加载true，到底false
	reload: false,
	status: 'more',
	contentText: {
		contentdown: '上拉加载更多',
		contentrefresh: '加载中',
		contentnomore: '已经全部加载完毕'
	}
})

// 定义弹出框
const chooseDialog = ref()

// 弹出框确定事件
const dialogConfirm=()=>{
	dialogClose()
	let url=''
	// 跳转到下一个页面
	if(pageData.fyfslx=='2' || pageData.fyfslx=='3'){
		url='/pages/ypczk/zkTask/djTask?cfbh='+pageData.cfbh+'&fyfslx='+pageData.fyfslx+'&ptwybh='+pageData.cfbh+'&state=add'
	}else if(pageData.fyfslx=='4' || pageData.fyfslx=='5'){
		url='/pages/ypczk/zkTask/dpTask?cfbh='+pageData.cfbh+'&fyfslx='+pageData.fyfslx+'&ptwybh='+pageData.cfbh+'&state=add'
	}
	uni.navigateTo({
		url:url,
	})
}

// 弹出框取消事件
const dialogClose=()=>{
	chooseDialog._value.close()
}

// 重置
const reset=()=>{
	pageData.cfbh=''
	pageData.hzxm=''
	pageData.djdsh=''
	getUserInfo()
	formData.yljgdm=pageData.deptCode
	search()
}

// 搜索
const search=()=>{
	formData.cfbm=pageData.cfbh
	formData.hzxm=pageData.hzxm
	formData.djdsh=pageData.djdsh
	formData.pageNum=1
	pageData.cfInfo=[]
	getCfData()
}

// 选择处方
const chooseCf=(e)=>{
	// 点击处方，弹出对话框
	chooseDialog._value.open()
	pageData.popupMessage.showMessage="处方编号："+e.cfbm+"\n"+"患者姓名："+e.hzxm+"\n"+"是否确认选择该处方？"
	pageData.cfbh=e.cfbm
	pageData.fyfslx=e.cflx[0]
	pageData.ptwybh=e.ptwybh
}

// 获取处方信息
const getCfData=()=>{
	let fyfslxList=[]
	if(pageData.cfStatus=='dj'){
		fyfslxList=[2,3]
	}else{
		fyfslxList=[4,5]
	}
	formData.cflx=fyfslxList
	// pageData.cfInfo=[]
	http.get("app-api/cf/findList",formData).then(res=>{
		pageData.cfInfo=pageData.cfInfo.concat(res.data)
		if (formData.pageSize > res.data.length) {
			loadingData.reload = false
			loadingData.status = 'noMore'
			uni.stopPullDownRefresh();
		} else {
			loadingData.status = 'more'
			loadingData.reload = true
		}
	})
	setTimeout(()=>{
		uni.stopPullDownRefresh();
	},1000)
}

// 上拉加载
onReachBottom(()=>{
	if (loadingData.reload) {
		formData.pageNum++;
		loadingData.status = 'loading'
		getCfData();
	}
})

// 下拉刷新
onPullDownRefresh(()=>{
	formData.pageNum = 1;
	loadingData.reload = true
	pageData.cfInfo = []
	getCfData();
})

onShow(()=>{
	getUserInfo()
	getYljgList()
	pageData.cfbh=""
	formData.yljgdm = pageData.deptCode
	pageData.cfInfo = []
	getCfData()
})

onLoad((type)=>{
	if(type.taskStatus!=null){
		pageData.cfStatus=type.taskStatus
	}
})

const getUserInfo=()=>{
	try{
		const value=uni.getStorageSync("userInfo")
		pageData.deptCode = value.deptCode
		pageData.deptName = value.orgName
	}catch(e){
		//TODO handle the exception
	}
}
</script>

<style lang="scss" scoped>
@import './index.scss'
</style>
