<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>
			<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">
							{{item}}
						</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:'130rpx'}"
									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) {
			console.log(receive, 666)
			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('请先扫高频编码')
					}
				}
			},
			// 扫一扫
			sweep() {
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ["qrCode"],
					success: (res) => {
						// console.log(res);
						if (res.result) {
							let url = res.result;
							this.frequencyCoding = url
							if (this.selectiveType != '99' || this.selectiveType != '100') {
								this.list.push(url)

								function methods1(arr) {
									return Array.from(new Set(arr));
								}
								this.list = methods1(this.list)
							}
						} else {
							console.log('请重新扫描');
							return false;
						}
					},
					fail: (res) => {
						console.log('未识别到二维码');
					}
				})
			}
		}
	}
</script>

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

	.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 {
		margin-top: 26rpx;
		width: 100%;
		padding-bottom: 15rpx;
		border-bottom: 2rpx solid #dfdfdf;
	}

	.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;
	}
</style>