<template>
	<view>
		<view style="padding:5rpx 20rpx 45rpx 20rpx;">
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							申请人
						</view>
					</uni-col>
					<uni-col :span="19">
						<view>
							<uni-easyinput primaryColor="#37babd" v-model="form.applyUserName" disabled>
							</uni-easyinput>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							申请部门
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="deptBindPickerChange" :value="deptConfig.deptIndex"
								:range="deptConfig.deptDatas" range-key="deptName">
								<view class="pickerText"
									v-if="deptConfig.deptDatas && deptConfig.deptDatas.length>0 && deptConfig.deptIndex !== undefined">
									{{ deptConfig.deptDatas[deptConfig.deptIndex].deptName }}
								</view>
								<view class="pickerText" v-else>
									{{defaultDept}}
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc" v-if="pageType === 'dj'">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							登记类型
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="accessBindPickerChange" :value="accessStorageType.index"
								:range="accessStorageType.data" range-key="dictLabel">
								<view class="pickerText"
									v-if="accessStorageType.data && accessStorageType.data.length>0 && accessStorageType.index !== undefined">
									{{ accessStorageType.data[accessStorageType.index].dictLabel }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc" v-if="pageType === 'ly'">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							领用类型
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="dictBindPickerChange" :value="outStorageType.index"
								:range="outStorageType.data" range-key="dictLabel">
								<view class="pickerText"
									v-if="outStorageType.data && outStorageType.data.length>0 && outStorageType.index !== undefined">
									{{ outStorageType.data[outStorageType.index].dictLabel }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc" v-if="pageType === 'gh'">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							物品状态
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<radio-group @change="radioChange">
								<uni-col :span="12" v-for="(item, index) in radiolist1">
									<label :key="item.value">
										<radio color="rgb(55,186,189)" :value="item.value" :checked="index === current">
											{{item.value}}
										</radio>
									</label>
								</uni-col>
							</radio-group>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							物资类型
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="oneBindPickerChange" :value="typeData.oneTypeIndex"
								:range="typeData.oneType" range-key="goodsTypeName">
								<view class="pickerText"
									v-if="typeData.oneType && typeData.oneType.length>0 && typeData.oneTypeIndex !== undefined">
									{{ typeData.oneType[typeData.oneTypeIndex].goodsTypeName }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							具体分类
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="twoBindPickerChange" :value="typeData.twoTypeIndex"
								:range="typeData.twoType" range-key="goodsTypeName"
								:disabled="typeData.twoTypeDisabled">
								<view class="pickerText"
									v-if="typeData.twoType && typeData.twoType.length>0 && typeData.oneTypeIndex !== undefined">
									{{ typeData.twoType[typeData.twoTypeIndex].goodsTypeName }}
								</view>
								<view class="pickerText" v-else @click="isTwoTypeShowModal()">
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							物资名称
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView pickerText">
							<view @click="openGoodsPopup" class="pickerText" v-if="form.goodsName">
								{{form.goodsName}}
							</view>
							<view @click="openGoodsPopup" class="pickerText" v-else>
								待 选 择
							</view>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							单位
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<view class="pickerText" v-if="form.unit">
								{{form.unit}}
							</view>
							<view class="pickerText" v-else>
								暂无
							</view>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;" v-if="pageType === 'dj'">
							登记数量
						</view>
						<view class="desc" style="font-size: 25rpx;" v-if="pageType === 'ly'">
							申请数量
						</view>
						<view class="desc" style="font-size: 25rpx;" v-if="pageType === 'gh'">
							归还数量
						</view>
					</uni-col>
					<uni-col :span="19">
						<view>
							<u-number-box v-model="form.amount"></u-number-box>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc" v-if="pageType === 'ly'">
				<view class="desc" style="font-size: 25rpx;">
					备注：
				</view>
				<view>
					<uni-easyinput type="textarea" primaryColor="#37babd" v-model="form.remark">
					</uni-easyinput>
				</view>
			</view>
		</view>
		<u-gap height="70" bgColor="#ffffff"></u-gap>
		<view class="bottim_view">
			<view>
				<u-row>
					<u-col span="11">
						<u-button :disabled="isDisabled" class="btnDoPay" shape="circle" @click="submit"
							color="rgb(55,186,189)" text="提交"></u-button>
					</u-col>
				</u-row>
			</view>
		</view>
		<!-- 物资选择弹窗 -->
		<uni-popup ref="goodsPopup" background-color="#fff">
			<view style="height: 800rpx;">
				<view style="padding: 30rpx;">
					<u-search v-model="searchGoodsName" :show-action="false" placeholder="快捷搜索"
						@change="getAppGoodsData(typeData.twoType[typeData.twoTypeIndex].id)"></u-search>
				</view>
				<view style="padding: 5rpx;">
					<uni-list v-for="item in goodsData">
						<uni-list-item :clickable="true" @click="goodsToSubmiut(item)" :title="item.name" />
					</uni-list>
				</view>
			</view>
		</uni-popup>
		<!-- 下一步选择人员弹窗 -->
		<uni-popup ref="popup" background-color="#fff">
			<view style="height: 800rpx;">
				<view class="desc">
					<uni-row>
						<uni-col :span="10">
							<view style="font-size: 40rpx;color: rgb(145, 145, 145);">
							</view>
						</uni-col>
						<uni-col :span="11">
							<view style="font-size: 40rpx;color: rgb(98, 98, 98);">
								下一步
							</view>
						</uni-col>
						<uni-col :span="3">
							<view @click="submitPass" style="font-size: 40rpx;color: rgb(55,186,189);">
								确认
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view>
					<!--  -->
					<uni-row>
						<uni-col>
							<view style="padding: 15rpx 20rpx;">
								<uni-data-picker ref="picker" placeholder="请选择" :popup-title="selectTitle"
									:localdata="dataTree" v-model="hxForm.candidate" @change="nextApprovalChange">
								</uni-data-picker>
							</view>
						</uni-col>
					</uni-row>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isDisabled: false,
				selectTitle: '',
				form: {
					goodsId: '',
					goodsName: '',
					amount: 1,
					unit: '',
					remark: ''
				},
				org_dept_parent_id: '',
				searchGoodsName: '',
				hxForm: {},
				dataTree: [],
				index: 0,
				defaultDept: '',
				reserve_type: [],
				goodsData: [],
				deptConfig: {
					deptDatas: [],
					deptIndex: undefined
				},
				current: 0,
				radiolist1: [{
					value: '正常'
				}, {
					value: '异常'
				}, {
					value: '损坏'
				}],
				typeData: {
					oneTypeIndex: undefined,
					oneType: [],
					twoTypeIndex: undefined,
					twoType: [],
					twoTypeDisabled: true
				},
				outStorageType: {
					index: undefined,
					data: []
				},
				accessStorageType: {
					index: undefined,
					data: []
				},
				pageType: ''
			}
		},
		onLoad() {
			this.pageType = this.$root.$mp.query.pageType
			let title = ''
			switch (this.pageType) {
				case "dj":
					title = '物资登记'
					this.accessStorageType.data = uni.getStorageSync('access_stroage_type')
					this.form.recordType = 1
					break
				case "ly":
					title = '物资领用'
					this.outStorageType.data = uni.getStorageSync('out_storage_type')
					this.form.recordType = 2
					break
				case "gh":
					title = '物资归还'
					this.form.recordType = 3
					this.form.applyReason = 5
					this.form.remark = '正常'
					break
			}
			uni.setNavigationBarTitle({
				title: title
			})
			//物资一级分类初始化
			this.getAppGoodsTypeData('')
			//机构信息初始化
			this.deptConfig.deptDatas = uni.getStorageSync('deptData')
			//申请人初始化
			this.form.applyUserName = uni.getStorageSync("userInfo").nickName
			//翻译默认部门
			let department = uni.getStorageSync('files').department
			this.defaultDept = this.$dictTranslation(department, 'deptData', 'dept')
			this.form.applyDepartment = department
		},
		methods: {
			openGoodsPopup() {
				if (!this.typeData.twoTypeDisabled) {
					this.$refs.goodsPopup.open('bottom')
				} else {
					this.$showModal('请先选择具体分类')
				}
			},
			isTwoTypeShowModal() {
				if (this.typeData.twoTypeDisabled) {
					this.$showModal('请先选择物资类型')
				}
			},
			change(e) {
				console.log(e);
			},
			radioChange: function(evt) {
				this.form.remark = evt.detail.value
			},
			async submit() {
				this.isDisabled = true
				setTimeout(() => {
					this.isDisabled = false
				}, 1000)
				if (!this.form.applyDepartment) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择所属部门',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				if (!this.form.goodsName) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择物资',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				if (!this.form.amount) {
					this.isDisabled = false
					uni.showModal({
						content: '请填写数量',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				if (!this.form.applyReason) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择领用类型',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/submitApply/rcr',
					method: 'post',
					data: this.form
				});
				if (res.code === 200) {
					this.dataTree = res.data.tree
					this.hxForm.taskId = res.data.taskId
					this.$refs.popup.open('bottom')
					this.selectTitle = "请选择" + this.dataTree[0].text
					this.isDisabled = false
				} else {
					this.$showModal(res.msg)
				}
				this.isDisabled = false
			},
			async submitPass() {
				if (!this.hxForm.candidate) {
					uni.showModal({
						content: '请选择' + this.dataTree[0].text,
						title: '提交失败',
						showCancel: false,
					})
				}
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/task/submit/candidate?taskId=' + this.hxForm.taskId +
						'&candidate=' + this.hxForm.candidate + '&applyUserName=' + this.hxForm.applyUserName,
					method: 'get',
				});
				if (res.code === 200) {
					uni.showModal({
						content: '已提交给审批人员',
						title: '提交成功',
						showCancel: false,
						success() {
							uni.navigateBack()
						}
					})
				} else {
					uni.showModal({
						content: res.msg,
						title: '提交失败',
						showCancel: false,
						success() {
							uni.navigateBack()
						}
					})
				}
			},
			nextApprovalChange(e) {
				// console.log('onchange:', e.detail.value[1].text);
				this.hxForm.applyUserName = e.detail.value[1].text
				this.hxForm.candidate = e.detail.value[1].value
			},
			//获取物资一级/二级类型
			async getAppGoodsTypeData(type) {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/goodsType?type=' + type,
					urlType: this.$getUrlType()
				});
				if (res.code === 200) {
					if (type) {
						this.typeData.twoType = res.data
						this.getAppGoodsData(this.typeData.twoType[0].id)
					} else {
						this.typeData.oneType = res.data
						this.getAppGoodsTypeData(this.typeData.oneType[0].id)
					}
				}
			},
			//获取物资信息/查询物资信息
			async getAppGoodsData(type) {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/goodses?type=' + type + '&name=' + this.searchGoodsName,
					urlType: this.$getUrlType()
				});
				if (res.code === 200) {
					this.goodsData = res.data
				}
			},
			//选择物资form赋值
			goodsToSubmiut(item) {
				this.form.goodsName = item.name
				this.form.goodsId = item.id
				this.form.unit = item.unit
				this.$refs.goodsPopup.close()
			},
			maskClick(e) {},

			//选择框赋值区
			oneBindPickerChange: function(e) {
				this.typeData.oneTypeIndex = e.detail.value
				this.getAppGoodsTypeData(this.typeData.oneType[this.typeData.oneTypeIndex].id)
				this.typeData.twoTypeIndex = 0
				this.typeData.twoTypeDisabled = false
				this.form.goodsName = ''
			},
			twoBindPickerChange: function(e) {
				this.typeData.twoTypeIndex = e.detail.value
				this.form.goodsName = ''
				this.goodsData = []
				this.getAppGoodsData(this.typeData.twoType[this.typeData.twoTypeIndex].id)
			},
			deptBindPickerChange: function(e) {
				this.deptConfig.deptIndex = e.detail.value
				this.form.applyDepartment = this.deptConfig.deptDatas[this.deptConfig.deptIndex].deptId
			},
			dictBindPickerChange: function(e) {
				this.outStorageType.index = e.detail.value
				this.form.applyReason = this.outStorageType.data[this.outStorageType.index].dictValue
			},
			accessBindPickerChange: function(e) {
				this.accessStorageType.index = e.detail.value
				this.form.applyReason = this.accessStorageType.data[this.accessStorageType.index].dictValue
			}
		}
	}
</script>

<style lang="scss" scoped>
	.desc {
		padding: 10rpx 5rpx;
		color: #818181;
		font-size: 20rpx;
	}

	.pickerView {
		// align-items: flex-end;
		padding: 10rpx 20rpx;
		border: 1rpx #eeeeee solid;
	}

	.pickerText {
		font-size: 26rpx;
		color: #b1b1b1;
	}

	.bottim_view {
		padding: 5px 20px 30px;
		position: fixed;
		left: 0px;
		bottom: 0px;
		width: 100%;
		height: 40px;
		background-color: #ffffff;
	}
</style>
