<template>
	<!-- 确认送达 -->
	<view>
		<u-navbar :title="navTitle" autoBack placeholder @leftClick="leftClick"></u-navbar>
		<view class="card_order_details">
			<view style="display: flex;align-items: center;margin-bottom: 20rpx;" v-if="storeFlag">
				<image class="mine_image" src="../../static/portrait.png" mode=""></image>
				<view class="card_user_title">
					<view class="mine_phone">{{storeData[2]}}
					</view>
					<view class="mine_phone">{{storeData[4]}}
					</view>
				</view>
			</view>
			<view class="details_title">单位内编号 <span class="line_title">*</span></view>
			<view style="display: flex;align-items: center;" v-if="selectiveType != 'inspect'">
				<u-input border="surround" placeholder="点击输入" v-model="frequencyCoding"></u-input>
				<view class="btn_add_card">
					<u-button type="primary" text="添加" @click="addCode"></u-button>
				</view>
			</view>
			<view class="card_search" v-else-if="selectiveType == 'inspect'">
				<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" @blur="inputBlur"></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 class="card_frequency" v-if="selectiveType != 'inspect'">
				<view class="scan_card_code">
					<view style="display: flex;align-items: center;">
						<view style="display: flex;align-items: center;" @click="addLine">
							<u-icon name="scan" size="26"></u-icon>
							<view class="title_subhead">增加一条</view>
						</view>
						<view style="display: flex;align-items: center;margin-left: 30rpx;" @click="continuousAddition">
							<u-icon name="scan" size="26"></u-icon>
							<view class="title_subhead">连续扫码增加</view>
						</view>
					</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 style="display: flex;align-items: center;">
							<view class="item_current">{{index + 1}}</view>
							<view class="title_codingewtw">{{item}}</view>
						</view>
						<view class="item_title1 title_red" @click="removeWaybill(item)">删除</view>
					</view>
				</view>
				<view class="card_empty" v-else>
					<u-empty mode="list" text="请先扫描添加单位内编号"></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[`${item.field}`]" 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 class="center_row" v-if="item.type == 'numinput'">
						<u-input class="input_item" type="digit" border="surround"
							v-model="ruleForm[`${item.field}`]"></u-input>
						<view style="margin-left: 15rpx;width: 90rpx;">{{item.unit}}</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>
		<u-popup :show="show" :round="10" :closeOnClickOverlay="false" mode="center" @close="close">
			<view class="card_coding_scan">
				<view class="title_coding">单位内编号</view>
				<view class="coding_title1">{{scanCoding}}</view>
				<view style="display: flex;align-items: center;">
					<u-icon color="#19be6b" size="30" name="checkmark-circle-fill"></u-icon>
					<view class="success_title">扫描成功</view>
				</view>
				<view class="title_coding">3秒后再次扫描</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		formRules,
	} from "./new_file.js";
	export default {
		data() {
			return {
				frequencyCoding: '',
				navTitle: '',
				selectiveType: '',
				cmpCode: '',
				list: [],
				checkEntry: formRules(),
				ruleForm: {
					deadWeight: '',
					color: '',
					pipePressure: '',
					fillWeight: '',
					repeatWeight: '',
					corrosion: 0,
					crackle: 0,
					deform: 0,
					damage: 0,
					safeAnnex: 0,
					gasPressure: 0,
					bodyDeform: 0,
					fillingLeak: 0,
					bodyTemperature: 0,
					filledLeak: 0,
					warnSign: 0,
					fillLabel: 0,
					seal: 0,
				},
				orderId: '',
				scanType: 1,
				show: false,
				scanCoding: '',
				storeFlag: false,
				storeData: [],
				scanFlag: true,
			}
		},
		onUnload() {
			console.log('页面销毁了！');
			this.$cache.removeCache('storeInformation')
		},
		onLoad(receive) {
			var storeList = this.$cache.getCache('storeInformation')
			if (storeList) {
				this.storeFlag = true
				this.storeData = storeList
			}
			this.navTitle = receive.title
			this.selectiveType = receive.id
			this.cmpCode = receive.cmpCode
			this.orderId = receive.orderId
		},
		methods: {
			leftClick() {
				uni.$emit('refresh');
			},
			submit() {
				// 99订单扫码确定送达、100订单扫码确定取消
				if (this.selectiveType == '99' || this.selectiveType == '100') {
					// 修改订单状态
					if (this.list.length > 0) {
						const param = {
							id: Number(this.orderId),
							state: null,
							chipUid: this.list,
						}
						if (this.selectiveType == '99') {
							param.state = 3
						} else if (this.selectiveType == '100') {
							param.state = 4
						}
						uni.showLoading({
							mask: true,
						});
						this.$api.put('/api/order/state', param).then(res => {
							if (res.code == 200) {
								this.frequencyCoding = ''
								this.list = []
								uni.$u.toast('操作成功')
								setTimeout(() => {
									uni.$emit('refresh');
									uni.navigateBack({
										delta: 1
									});
								}, 1000)
							} else {
								uni.$u.toast(res.data.msg)
							}
							uni.hideLoading();
						}).catch(() => {
							uni.hideLoading();
						})
					} else {
						uni.$u.toast('请先扫单位内编号')
					}
				} else if (this.selectiveType == 'inspect') {
					if (this.frequencyCoding) {
						uni.showLoading({
							mask: true,
						});
						let params = JSON.parse(JSON.stringify(this.ruleForm))
						params.repeatWeight = Number(this.ruleForm.repeatWeight)
						params.fillWeight = Number(this.ruleForm.fillWeight)
						params.deadWeight = Number(this.ruleForm.deadWeight)
						params.pipePressure = Number(this.ruleForm.pipePressure)
						this.$api.post('/api/fill-check', {
							innerCode: this.frequencyCoding,
							...params,
						}).then(res => {
							if (res.code == 200) {
								this.frequencyCoding = ''
								for (const k in this.ruleForm) {
									// 循环赋值
									this.ruleForm[k] = 0
									if (['deadWeight', 'color', 'pipePressure', 'fillWeight', 'repeatWeight']
										.includes(k)) {
										this.ruleForm[k] = ''
									}
								}
								uni.$u.toast('操作成功')
							}
							uni.hideLoading();
						}).catch(() => {
							uni.hideLoading();
						})
					} 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
						}
						uni.showLoading({
							mask: true,
						});
						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)
							}
							uni.hideLoading();
						}).catch(() => {
							uni.hideLoading();
						})
					} else {
						uni.$u.toast('请先扫单位内编号')
					}
				}
			},
			// 输入值
			inputBlur(value) {
				this.list.push(value)
				this.list = Array.from(new Set(this.list));
			},
			// 输入钢瓶编号添加
			addCode() {
				if (this.frequencyCoding) {
					if (['10', '15', '33', '37', '12', '16', '19', '99'].includes(this.selectiveType)) {
						let arrNum = ''
						if (this.selectiveType == '99') {
							arrNum = '26'
						} else {
							arrNum = this.selectiveType
						}
						this.$api.post('/api/operation-log/check', {
							chipUid: this.frequencyCoding,
							optType: arrNum,
						}).then(res => {
							if (res.code == 200) {
								this.list.push(this.frequencyCoding)
								this.list = Array.from(new Set(this.list));
								this.frequencyCoding = ''
							} else {
								uni.$u.toast(res.data.msg)
							}
						})
					} else {
						this.list.push(this.frequencyCoding)
						this.list = Array.from(new Set(this.list));
						this.frequencyCoding = ''
					}
				}
			},
			// 添加一条
			addLine() {
				this.scanType = 1
				if (this.scanFlag) {
					this.scanFlag = false
					this.sweep()
				}
			},
			// 连续添加
			continuousAddition() {
				this.scanType = 2
				if (this.scanFlag) {
					this.scanFlag = false
					this.sweep()
				}
			},
			// 扫一扫
			sweep() {
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ['qrCode'],
					autoZoom: false,
					success: (res) => {
						console.log(res, '--------');
						if (res.result) {
							this.scanFlag = true
							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
							if (this.selectiveType != 'inspect') {
								if (['10', '15', '33', '37', '12', '16', '19', '99'].includes(this
										.selectiveType)) {
									let arrNum = ''
									if (this.selectiveType == '99') {
										arrNum = '26'
									} else {
										arrNum = this.selectiveType
									}
									this.$api.post('/api/operation-log/check', {
										chipUid: arr.inner_code,
										optType: arrNum,
									}).then(res => {
										if (res.code == 200) {
											if (this.scanType == 1) {
												this.list.push(arr.inner_code)
												this.list = Array.from(new Set(this.list));
											} else {
												this.show = true
												this.list.push(arr.inner_code)
												this.list = Array.from(new Set(this.list));
												this.scanCoding = arr.inner_code
												// 扫码间隔两秒
												setTimeout(() => {
													this.sweep()
													this.show = false
												}, 3000)
											}
										} else {
											this.show = false
											uni.$u.toast(res.data.msg)
										}
									})
								} else {
									if (this.scanType == 1) {
										this.list.push(arr.inner_code)
										this.list = Array.from(new Set(this.list));
									} else {
										this.show = true
										this.list.push(arr.inner_code)
										this.list = Array.from(new Set(this.list));
										this.scanCoding = arr.inner_code
										// 扫码间隔两秒
										setTimeout(() => {
											this.sweep()
											this.show = false
										}, 3000)
									}
								}
							} else {
								this.frequencyCoding = arr.inner_code
							}
						} else {
							uni.$u.toast('请扫描单位内编号二维码')
						}
						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);
				}
			},
			// 弹窗关闭
			close() {
				this.show = false
			}
		}
	}
</script>

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

	.mine_image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
	}

	.card_user_title {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
	}

	.mine_phone {
		margin-left: 20rpx;
		font-size: 30rpx;
	}

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

	.btn_add_card {
		margin-left: 20rpx;
	}

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

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

	.line_title {
		color: red;
	}

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

	.scan_card_code {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 30rpx;
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #e4e7ed;
	}

	.card_bottle {
		font-size: 30rpx;

		span {
			margin: 0rpx 5rpx;
			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;
	}

	.item_current {
		width: 40rpx;
		color: #82848a;
	}

	.title_codingewtw {
		margin-left: 10rpx;
		font-size: 30rpx;
	}

	.item_title1 {
		margin-left: 10rpx;
	}

	.title_red {
		color: #fa3534;
	}

	.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;
		height: 72rpx;
	}

	.title_entry {
		font-size: 26rpx;
	}

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

	.input_item {
		width: 240rpx;
	}

	.card_coding_scan {
		display: flex;
		align-items: center;
		flex-direction: column;
		width: 400rpx;
		padding: 30rpx;
	}

	.title_coding {
		font-size: 32rpx;
		margin: 20rpx;
	}

	.success_title {
		font-size: 40rpx;
		margin-left: 10rpx;
	}

	.coding_title1 {
		font-size: 60rpx;
		margin-bottom: 20rpx;
		font-weight: 600;
	}
</style>