<template>
	<!-- 分配订单 -->
	<view>
		<u-navbar title="分配订单" autoBack placeholder></u-navbar>
		<view class="space_between card_Address">
			<view class="title_waybill">收件人:</view>
			<view class="card_flex_il" @click="addressBook('consignee')">
				<u-icon name="order" size="22"></u-icon>
				<view class="title_sou">收件人簿</view>
			</view>
		</view>
		<x-form ref="recipients" :list="list" :model="recipientsModel" :rules="rules"></x-form>
		<x-form ref="goods" :list="goodsList" :model="goodsModel" :rules="goodsRules" :addloading="addloading"
			:productData="productData" @getSweep="getSweep"></x-form>
		<view class="safeBottom" style="width: 100%;height: 180rpx;"></view>
		<view class="btn_print" @click="printWaybill">
			<u-button type="primary" text="确定"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		allocationRulesil,
		allocationList
	} from "./waybill.js";
	export default {
		data() {
			return {
				list: allocationRulesil(),
				recipientsModel: {
					name: '',
					phone: '',
					address: '',
				},
				rules: {
					name: {
						required: false,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					},
					phone: {
						required: true,
						message: '请输入电话',
						trigger: ['blur', 'change']
					},
					address: {
						required: false,
						message: '请输入地址',
						trigger: ['blur', 'change']
					},
				},
				goodsModel: {
					drugBarCode: '',
					prescription: [],
				},
				productData: [],
				goodsRules: {},
				goodsList: allocationList(),
				addloading: false,
				allocationSuccess: false,
				waybillNo: '',
			}
		},
		onUnload() {
			uni.$off('success')
			// console.log('页面销毁')
			uni.removeStorageSync('selectAddress')
		},
		onLoad(option) {
			if (option.flag == 'true') {
				this.allocationSuccess = true
			}
			this.waybillNo = option.waybillNo
		},
		onShow() {
			let address = uni.getStorageSync('selectAddress')
			if (address) {
				this.$nextTick(() => {
					this.$refs['recipients'].validateForm();
				})
				this.recipientsModel = address
				this.saveConsigneeAddress = false
			}
		},
		methods: {
			// 选择类型
			addressBook(type) {
				this.operationType = type
				uni.navigateTo({
					url: '/pages/order/addressBook?type=' + type
				});
			},
			// 确定分配订单
			async printWaybill() {
				let flag1 = await this.$refs['recipients'].validateForm();
				if (flag1) {
					if (this.productData.length > 0) {
						let params = {
							consigneeAddressName: this.recipientsModel.name,
							consigneeAddressPhone: this.recipientsModel.phone,
							consigneeAddressDetails: this.recipientsModel.address,
							drugs: this.productData,
							waybillNo: this.waybillNo,
							prescription: this.goodsModel.prescription,
						}
						this.$api.post('/api/waybill/assignment', params).then(res => {
							if (res.code == 200) {
								if (this.allocationSuccess) {
									uni.$emit('success', '成功');
									uni.navigateBack({
										delta: 1
									});
								} else {
									uni.$emit('success', '成功');
									uni.navigateBack({
										delta: 2
									});
								}
							}
						})
					} else {
						uni.$u.toast('请扫描分配药品条形码')
					}
				}
			},
			// 获取药品信息码上放心
			getSweep(event) {
				let idExists = this.productData.some(obj => obj.code === event);
				if (!idExists) {
					this.addloading = true
					uni.request({
						url: 'https://public.coldbaozhida.com/Ali_msfx/codedetail', //仅为示例，并非真实接口地址。
						method: 'GET',
						header: {
							'content-type': 'application/x-www-form-urlencoded', // 默认值
						},
						data: {
							code: event,
						},
						success: (res) => {
							if (res.data.status == 200) {
								let arr = JSON.parse(res.data.data)
								if (arr.result.models.length > 0) {
									let drugList = {
										code: '',
										physic_name: '',
										physic_type_desc: '',
										pkg_spec_crit: '',
										ent_name: '',
									}
									drugList.code = arr.result.models[0].code
									drugList.physic_name = arr.result.models[0].drug_ent_base_d_t_o.physic_name
									drugList.physic_type_desc = arr.result.models[0].drug_ent_base_d_t_o
										.physic_type_desc
									drugList.pkg_spec_crit = arr.result.models[0].drug_ent_base_d_t_o
										.pkg_spec_crit
									drugList.ent_name = arr.result.models[0].p_user_ent_d_t_o.ent_name
									this.productData.push(drugList)
									this.productData = this.uniqueArray(this.productData)
								} else {
									uni.$u.toast('该商品不存在')
								}
							}
							this.addloading = false
						}
					});
				} else {
					uni.$u.toast('该商品已存在')
				}
			},
			// 去重
			uniqueArray(arr) {
				return [...new Set(arr)];
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.card_Address {
		position: relative;
	}

	.card_flex_il {
		display: flex;
		align-items: center;
		padding: 20rpx;
	}

	.title_waybill {
		font-size: 32rpx;
		padding: 20rpx;
	}

	.title_sou {
		font-size: 32rpx;
	}

	.card_Address:before {
		content: " ";
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 1px;
		border-top: 1px solid #e7e6e4;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}
</style>