<template>
	<view class="query-wrap">
		<uni-easyinput prefixIcon="search" :value="value" placeholder="请输入采购单号" @iconClick="iconClick" @input="input">
		</uni-easyinput>
		<view class="empty-cont" v-if="oubondDelData.length=='0'">
			暂无明细数据
		</view>
		<view class="inner-item" v-for="(item,idex) in oubondDelData" :key="idex" @click.stop="itemDetails(item,idex)">
			<view class="text-title" v-if="idex=='0'">
				<view class="title-circ">

				</view>
				<span>药品清单</span>
			</view>
			<view class="check-title" @tap.stop.prevent>
				<checkbox-group @change="checkChange" :data-id="item.id">
					<label>
						<checkbox :value="idex" />
					</label>
				</checkbox-group>
			</view>
			<view class="inventory-item">
				<view class="shuo-item-name">
					<view class="shuo-label-podnme">
						采购单号:
					</view>
					<view class="shuo-unit">
						{{item.purcOrdId}}
					</view>
				</view>
			</view>
			<view class="inventory-item">
				<view class="shuo-item">
					<view class="shuo-label">
						药品编码:
					</view>
					<view class="shuo-unit">
						{{item.drugListId}}
					</view>
				</view>
			</view>
			<view class="inventory-item" style="margin-top: 40rpx;">
				<view class="shuo-item-name">
					<view class="shuo-label-podnme">
						药品名称:
					</view>
					<view class="shuo-unit">
						<span>{{ item.drugName }}</span>
					</view>
				</view>
			</view>
			<view class="inventory-item">
				<view class="shuo-item">
					<view class="shuo-label">
						采购数量:
					</view>
					<view class="shuo-cont">
						{{ item.purcCnt||"" }}
					</view>
				</view>
				<view class="shuo-item">
					<view class="shuo-label">
						采购金额:
					</view>
					<view class="shuo-cont">
						{{ item.purcAmt||"" }}
					</view>
				</view>
			</view>
			<view class="inventory-item">
				<view class="shuo-item-name">
					<view class="shuo-label">
						药品规格:
					</view>
					<view class="shuo-cont">
						{{
					         "" +
					         item.specName +
					         "*" +
					         item.convrat +
					         item.pac +
					         "/" +
					         item.pacUnt +
					         "】"
					       }}
					</view>
				</view>
			</view>
			<view class="inventory-item">
				<view class="shuo-item">
					<view class="shuo-label">
						药品剂型:
					</view>
					<view class="shuo-cont">
						{{item.dosformName}}
					</view>
				</view>
				<view class="shuo-item">
					<view class="shuo-label">
						待出库数量:
					</view>
					<view class="shuo-cont">
						{{item.toDelvCnt}}
					</view>
				</view>
			</view>

		</view>
		<uni-popup ref="popup">
			<uni-popup-dialog type="warn" :before-close="true" cancelText="取消" confirmText="确定" content="你确定当前操作吗？"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<view class="botm-btn">
			<button type="default" plain="true" style="width:240rpx;line-height: 70rpx;background: #01A992;color:#fff"
				@click.stop="accessData()">确认</button>
		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex';
	import moment from "moment";
	import {
		debounce
	} from 'lodash';
	export default {
		data() {
			return {
				listData: [],
				value: "",
				acceptDetaData: {},
				subMitData: {},
				selcectData: {} //详情选中的数据
			}
		},
		computed: {
			...mapState(['oubondDelData', 'instData', 'batchData', 'userData'])
		},
		onShow() {},
		mounted() {},
		methods: {
			...mapActions(['getoutBondDelData']),
			...mapMutations(['setBatchData', 'setOubondDelData']),
			input: debounce(function(e) {
				this.getoutBondDelData({
					orgId: this.instData.orgId,
					purcOrdId: e
				})
			}, 500),
			forMatTime(row) {
				return moment(row).format("YYYY-MM-DD HH:mm");
			},
			checkChange(e) {
				let newSelect = {
					...this.selcectData
				};
				let chekData = e.detail.value;
				if (chekData.length > 0) {
					this.selcectData = {
						...this.selcectData,
						[e.target.dataset.id]: this.oubondDelData[chekData[0]]
					}
				} else {
					delete newSelect[e.target.dataset.id]
					this.selcectData = newSelect;
				}
			},
			//耗材添加批次跳转
			itemDetails(item, index) {
				if (!this.selcectData[item.id]) {
					uni.showToast({
						title: '请选中再添加批次',
						icon: 'none',
						duration: 1000,
					});
					return;
				}
				uni.setStorage({
					key: 'batchDelsData',
					data: item
				});
				uni.navigateTo({
					url: '/subpkg/pages/outbond-detal/durg-batch',
					success: function(res) {
						console.log(res, "res")
					}
				});
			},
			//确认/退回
			accessData() {
				if (JSON.stringify(this.selcectData) === "{}") {
					uni.showToast({
						title: '请选择出库药品',
						icon: 'none',
						duration: 1000,
					});
					return;
				};;
				let selectKey = Object.keys(this.selcectData);
				let filterEmpty = selectKey.filter((item) => {
					return this.batchData[item] == null || this.batchData[item].length == 0
				})
				if (filterEmpty.length > 0) {
					uni.showToast({
						title: '请为选中的药品添加批次',
						icon: 'none',
						duration: 1000,
					});
					return;
				};
				let newSubData = [];
				selectKey.forEach((item) => {
					newSubData.push(this.batchData[item])
				})
				const merged = newSubData.reduce((acc, current) => [...acc, ...current], []);
				
				this.$http('outbondDetal.submitData', {
					spdId: this.instData.spdId,
					confirm: "1",
					detlList: merged
				}, '加载中').then(res => {
					if (res.success == true) {
						this.setBatchData({});
						this.setOubondDelData([]);
						this.value = "";
					}
				}).catch(e => {

				})
			},
			//属性值进行比较
			//日期格式化
			forMatTime(time) {
				return moment(time).format("YYYY-MM-DD");
			},
			//加时分秒
			forMatTimehs(time) {
				return moment(time).format("YYYY-MM-DD HH:mm");
			},
		},
		watch: {

		}

	}
</script>

<style lang="scss" scoped>
	.query-wrap {
		width: 100%;
		height: 100%;
		padding: 20rpx;
		box-sizing: border-box;
		background-color: #F1F1F1;
		overflow-y: scroll;
		overflow-x: hidden;
		position: relative;
		padding-bottom: 200rpx;

		.botm-btn {
			width: 100%;
			height: 120rpx;
			background-color: #fff;
			position: fixed;
			left: 0;
			bottom: 0;
			padding-top: 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
		}

		.time-view {
			height: 350rpx;
			background-color: #fff;
			padding: 50rpx 50rpx 0 50rpx;
			box-sizing: border-box;

			.btn-view {
				width: 100%;
				height: 100rpx;
				margin-top: 100rpx;
				display: flex;
				justify-content: space-around;
			}
		}

	}

	uni-page-body {
		width: 100%;
		height: 100%;
	}
</style>