<template>
	<!-- 确认送达 -->
	<view>
		<u-navbar :title="navTitle" autoBack placeholder @leftClick="leftClick"></u-navbar>
		<view class="card_order_details">
			<view class="details_title">高频编码 <span class="line_title">*</span></view>
			<view class="card_search">
				<view class="card_sweep" @click="sweep">
					<u-icon name="scan" size="36"></u-icon>
					<view class="scan_title">扫一扫</view>
				</view>
				<view class="card_input">
					<u-input border="surround" v-model="frequencyCoding"></u-input>
				</view>
				<view class="deleteCurrent" @click="removeWaybill(frequencyCoding)">
					<u-icon class="icon_current" name="backspace" height="10" size="20"></u-icon>
					<view class="title_nape">删除</view>
				</view>
			</view>
			<view v-if="selectiveType != '99' && selectiveType != '100'">
				<view class="card_frequency" v-if="selectiveType != 'inspect'">
					<view class="card_high space_between">
						<view class="card_frequency_title">高频编码列表</view>
						<view class="card_bottle">已扫<span>{{list.length}}</span>瓶</view>
					</view>
					<view style="width: 100%;" v-if="list.length > 0">
						<view class="item_coding" v-for="(item,index) in list" :key="index">
							<view class="title_coding">{{item}}</view>
							<u-icon name="close-circle-fill" color="#c0c4cc" size="20"
								@click="removeWaybill(item)"></u-icon>
						</view>
					</view>
					<view class="card_empty" v-else>
						<u-empty mode="list"></u-empty>
					</view>
				</view>
				<view class="card_examine" v-else>
					<view class="card_examine_item space_between" v-for="(item,index) in checkEntry" :key="index">
						<view class="title_entry">{{item.label}}</view>
						<view style="height: 40rpx;" v-if="item.type == 'radio'">
							<u-radio-group v-model="ruleForm.state" placement="row">
								<u-radio shape="circle" :customStyle="{width:'140rpx'}"
									v-for="(item, index) in item.options" :key="index" :label="item.label"
									:name="item.value"></u-radio>
							</u-radio-group>
						</view>
						<view class="center_row" v-if="item.type == 'input'">
							<u-input class="input_item" border="surround" v-model="ruleForm[`${item.field}`]"></u-input>
							<view style="margin-left: 15rpx;width: 90rpx;">{{item.unit}}</view>
						</view>
					</view>
				</view>
			</view>
			<view style="width: 100%;height: 120rpx;"></view>
			<view class="card_btn">
				<u-button style="margin-bottom: 20rpx;" type="primary" @click="submit">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		formRules,
	} from "./new_file.js";
	export default {
		data() {
			return {
				frequencyCoding: '',
				navTitle: '',
				selectiveType: '',
				cmpCode: '',
				list: [],
				checkEntry: formRules(),
				ruleForm: {},
				orderId: '',
			}
		},
		onLoad(receive) {
			this.navTitle = receive.title
			this.selectiveType = receive.id
			this.cmpCode = receive.cmpCode
			this.orderId = receive.orderId
		},
		methods: {
			leftClick() {
				uni.$emit('refresh');
			},
			submit() {
				if (this.selectiveType == '99' || this.selectiveType == '100') {
					// 修改订单状态
					if (this.frequencyCoding) {
						const param = {
							id: Number(this.orderId),
							state: null,
							chipUid: this.frequencyCoding,
						}
						if (this.selectiveType == '99') {
							param.state = 3
						} else if (this.selectiveType == '100') {
							param.state = 4
						}
						this.$api.put('/api/order/state', param).then(res => {
							if (res.code == 200) {
								this.frequencyCoding = ''
								uni.$u.toast('操作成功')
							} else {
								uni.$u.toast(res.data.msg)
							}
						})
					} else {
						uni.$u.toast('请先扫高频编码')
					}
				} else {
					if (this.list.length > 0) {
						// 操作记录
						const param = {
							chipUidList: this.list,
							currentEnterprise: this.cmpCode,
							optType: this.selectiveType,
						}
						if (!param.currentEnterprise) {
							delete param.currentEnterprise
						}
						this.$api.post('/api/operation-log', param).then(res => {
							if (res.code == 200) {
								this.list = []
								this.frequencyCoding = ''
								uni.$u.toast('操作成功')
							} else {
								uni.$u.toast(res.data.msg)
							}
						})
					} else {
						uni.$u.toast('请先扫高频编码')
					}
				}
			},
			// 扫一扫
			sweep() {
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ['qrCode'],
					autoZoom: false,
					success: (res) => {
						console.log(res, '--------');
						if (res.result) {
							let url = res.result;
							const arrf = url.split('=')
							this.unitCoding(arrf[1])
						} else {
							console.log('请重新扫描');
							return false;
						}
					},
					fail: (res) => {
						console.log('未识别到二维码1');
					}
				})
			},
			// 单位内编码获取
			unitCoding(qrid) {
				uni.showLoading({
					title: '加载中'
				});
				uni.request({
					url: 'http://qr.uinshine.com:9000/CommonAPI/product/getDetails', //仅为示例，并非真实接口地址。
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded', // 默认值
					},
					data: {
						qr_id: qrid,
					},
					success: (res) => {
						if (res.data.code == 0) {
							var arr = res.data.data
							this.frequencyCoding = arr.inner_code
							if (this.selectiveType != '99' || this.selectiveType != '100') {
								this.list.push(arr.inner_code)

								function methods1(arr) {
									return Array.from(new Set(arr));
								}
								this.list = methods1(this.list)
							}
							uni.hideLoading();
						}
					}
				});
			},
			// 移除错误运单号
			removeWaybill(value) {
				if (this.frequencyCoding == value) {
					this.frequencyCoding = ''
				}
				const arr = deleteElementById(this.list, value)
				this.list = arr

				function deleteElementById(arr, key) {
					return arr.filter((item) => item !== key);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.card_order_details {
		margin: 25rpx;
	}

	.details_title {
		color: #333;
		font-size: 34rpx;
		font-weight: 500;
		margin-bottom: 10rpx;
	}

	.card_search {
		display: flex;
		align-items: center;
		margin: 20rpx 0rpx;
	}

	.scan_title {
		font-size: 20rpx;
	}

	.card_sweep {
		display: flex;
		flex-direction: column;
		align-items: center;
		flex: none;
	}

	.card_input {
		width: 100%;
		margin-left: 30rpx;
	}

	.line_title {
		color: red;
	}

	.card_high {
		margin-top: 30rpx;
	}

	.card_frequency {
		display: flex;
		flex-wrap: wrap;
	}

	.card_frequency_title {
		font-size: 32rpx;
	}

	.card_bottle {
		font-size: 30rpx;

		span {
			color: red;
		}
	}

	.item_coding {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		margin-top: 10px;
		padding: 25rpx 0rpx;
		border-bottom: 2rpx solid #dfdfdf;
	}

	.title_coding {
		font-size: 30rpx;
	}

	.card_empty {
		width: 100%;
		margin-top: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.card_btn {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding-left: 30rpx;
		padding-right: 30rpx;
		padding-top: 20rpx;
		background-color: #fff;
		padding-bottom: constant(safe-area-inset-bottom); //兼容 IOS<11.2
		padding-bottom: env(safe-area-inset-bottom); //兼容 IOS>11.2
	}

	.card_examine {
		margin-top: 50rpx;
	}

	.card_examine_item {
		display: flex;
		margin-bottom: 20rpx;
	}

	.title_entry {
		font-size: 26rpx;
	}

	.center_row {
		display: flex;
		height: 70rpx;
		align-items: center;
	}

	.input_item {
		width: 240rpx;
	}

	.deleteCurrent {
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: center;
		border: 1px solid #e5e5e5;
		padding: 5rpx 0rpx 5rpx 5rpx;
		margin-left: 10rpx;
		border-radius: 8rpx;
		height: 66rpx;

		.icon_current {
			height: 15px;
		}

		.title_nape {
			margin: 0rpx 10rpx;
			display: flex;
			font-size: 20rpx;
			width: 40rpx;
		}
	}
</style>