<template>
	<view class="content">
		<view class="topbar" v-show="isIphone">
		</view>
		<view class="status_bar">
			<view class="left" @click="goBack"><u-icon name="arrow-left"></u-icon>返回</view>
			<span style="font-size: 36rpx;">医废打包</span>
			<view class="right"></view>
		</view>
		<view class="mainCont">
			<u-cell-group style="margin-bottom: 10px">
				<u-cell-item :arrow="false">
					<view slot="title"><span class="red">*</span>科&emsp;&emsp;室：</view>
					{{ ks }}
					<u-icon @click="scanCode()" slot="right-icon" name="scan" color="#2979ff" size="40rpx"></u-icon>
				</u-cell-item>
			</u-cell-group>
			<u-cell-group style="margin-bottom: 10px">
				<u-cell-item>
					<view slot="title"><span class="red">*</span>医废类型：</view>
					<u-input input-align="right" disabled placeholder="请选择类型" v-model="yfType" type="text"
						@click="typeSelect = true" />
				</u-cell-item>
				<u-cell-item :arrow="false">
					<view slot="title"><span class="red">*</span>重&emsp;&emsp;量：</view>
					<view style="display: flex;justify-content: space-between;">
						<u-input input-align="right" placeholder="重量" v-model="weight" type="number"
							style="width: calc(100% - 30px);" />
						<span style='width: 30px;line-height: 35px;'>kg</span>
					</view>

				</u-cell-item>
				<u-cell-item :arrow="false">
					<view slot="title"><span class="red"></span>打&ensp;包&ensp;人：</view>
					{{ userName }}
				</u-cell-item>
				<u-cell-item :arrow="false">
					<view slot="title"><span class="red"></span>暂&ensp;存&ensp;点：</view>
					{{ positionName }}
				</u-cell-item>
			</u-cell-group>
			<view class="imgupload">
				<u-input placeholder="请输入备注内容" v-model="remark" auto-height type="textarea" />
				<u-upload :show-progress="false" ref="upload" @on-remove="uploadRemove" @on-success="uploadSuccess"
					:header="uploadHeader" max-count="3" width="80px" height="80px" :custom-btn="true" :action="action"
					:file-list="fileList">
					<view slot="addBtn" class="slot-btn">
						<image style="width: 80px;height: 80px" src="/static/tjzp.png" alt="">
					</view>
				</u-upload>
			</view>
		</view>
		<view class="bottomButton" @click="confirmShow()">
			<view>提交</view>
		</view>
		<u-select :default-value="selectType" value-name='code' @confirm="confirm" v-model="typeSelect"
			:list="typeList"></u-select>
		<u-toast ref="uToast" />
		<u-modal v-model="qrshow" @cancel="scanCode" @confirm="sumbitPack" show-cancel-button cancel-text="重新扫码"
			:content="ks" title="确认科室"></u-modal>
		<u-modal v-model="cgshow" @cancel="goBackWithClear" @confirm="continuePack" show-cancel-button
			content="提交成功,是否继续打包该科室的垃圾" title="提交成功"></u-modal>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		onLoad() {
			uni.getSystemInfo({
				success: (res) => {
					console.log(res)
					if (res.model == 'iPhone') {
						this.isIphone = true;
					}
				},
				fail: (err) => {
					console.log(err)
				}
			})
			let postData = {
				dictCode: 'rubbishCategoryHosp'
			}
			NET.request(API.getLabelByCode, postData).then(res => {
				console.log(res.data)
				this.typeList = res.data
			}).catch(error => {
				this.$refs.uToast.show({
					title: error.message,
					type: 'warning',
				})
			})
			NET.request(API.getUserInfoById, {}).then(res => {
				console.log(res.data)
				this.positionName = res.data.positionName;
			}).catch(error => {
				this.$refs.uToast.show({
					title: error.msg,
					type: 'warning',
				})
			})
			// alert(uni.getStorageSync('ksId'))
			this.ks = uni.getStorageSync('ksName');
			this.positionId = uni.getStorageSync('ksId')
		},
		onReady() {
			if (this.isIphone) {
				document.getElementsByClassName('mainCont')[0].style.height = document.getElementsByClassName('mainCont')[
					0].clientHeight - 25 + 'px'
			}
		},
		onShow() {

		},
		data() {
			return {
				qrshow: false,
				cgshow: false,
				yfType: "",
				yfValue: "",
				selectType: [0],
				typeSelect: false,
				weight: 0,
				positionName: "",
				positionId: "",
				ks: "",
				ksId: "",
				typeList: [],
				remark: "",
				userName: uni.getStorageSync('userName'),
				isIphone: false,
				uploadHeader: {
					Authorization: 'Bearer ' + uni.getStorageSync('token')
				},
				action: API.uploadFile,
				fileList: [],
				imgList: [],
			}
		},
		methods: {
			uploadSuccess(data, index) {
				console.log(data, index)
				if (data.code == 200) {
					data.data[0].fileType = 2
					data.data[0].fileCategory = 1
					this.imgList.push(data.data[0])
					this.$refs.uToast.show({
						title: '上传成功',
						type: 'success',
					})
				} else {
					this.$refs.upload.remove(index)
				}
			},
			uploadRemove(index) {
				console.log(index);
				this.imgList.splice(index, 1)
			},
			goBack() {
				try {
					EbeiPlugins.onBackKeyDown();
				} catch (error) {
					uni.navigateBack({
						delta: 1
					});
				}
				// uni.navigateBack({
				// 	delta: 1
				// });
			},
			goBackWithClear() {
				uni.removeStorageSync('ksId')
				uni.removeStorageSync('ksName')
				uni.navigateBack({
					delta: 1
				});
			},
			confirm(val) {
				this.yfType = val[0].label
				this.yfValue = val[0].value
				this.selectType[0] = this.typeList.findIndex((a) => a.code == val[0].value)
			},
			getKs() {
				NET.request(API.queryPositionDetail + '/' + this.positionId, {}).then(res => {
					if (res.data.positionType == '1') {
						uni.setStorage({
							key: 'ksId',
							data: this.positionId
						})
						uni.setStorage({
							key: 'ksName',
							data: res.data.positionName
						})
						this.ks = res.data.positionName
					} else {
						this.$refs.uToast.show({
							title: '此点位不是科室，请重新扫码',
							type: 'warning',
						})
					}
				}).catch(error => {
					this.$refs.uToast.show({
						title: error.msg,
						type: 'warning',
					})
				})
			},
			scanCode() {
				console.log('asdasdasd')
				let that = this
				EbeiPlugins.scanQRCode((string) => {
					that.positionId = string;
					that.getKs()
				}, () => {
					that.showTips('扫码失败')
				})
			},
			confirmShow() {
				if (!this.positionId) {
					this.$refs.uToast.show({
						title: '请扫码获取科室',
						type: 'warning',
					})
					return false
				}
				if (!this.yfValue) {
					this.$refs.uToast.show({
						title: '请选择医废类型',
						type: 'warning',
					})
					return false
				}
				if (!this.weight) {
					this.$refs.uToast.show({
						title: '请输入重量',
						type: 'warning',
					})
					return false
				}
				this.qrshow = true;
			},
			continuePack() {
				this.weight = 0;
				this.remark = '';
				this.$refs.upload.clear()
				this.imgList = [];
				this.yfType = "";
				this.yfValue = "";
			},
			sumbitPack() {
				let postData = {
					packRemake: this.remark,
					fileList: this.imgList,
					createUser: uni.getStorageSync('userId'),
					positionId: this.positionId, //科室ID
					rubbishCategory: this.yfValue, //医废类型
					packWeight: this.weight,
				}
				NET.request(API.SaveOrUpdatePackage, postData, 'POST').then(res => {
					console.log(res.data)
					this.cgshow = true;
				}).catch(error => {
					this.$refs.uToast.show({
						title: error.msg,
						type: 'warning',
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.mainCont {
		height: calc(100% - 80rpx);
		padding-bottom: 60px;
		overflow-y: scroll;
	}

	.red {
		display: inline-block;
		width: 10px;
		height: 10px;
		color: red;
	}

	.imgupload {
		padding: 10px;
		background: #fff;
	}

	/deep/ .u-cell__value {
		color: #1c1c1c;
	}
</style>
