<template>
	<view class="content">
		<u-toast ref="uToast" />
		<u-popup v-model="show" class="popup" @close="close" mode="bottom" border-radius="30" :closeable="true">
			<view class="top">
				<view class="top-center-1">收到</view>
			</view>
			<view class="top-content-2">
				<view>来自</view>
				<view class="top-content-green">{{user.title}}</view>
				<view>的邀请。</view>
			</view>
			<view class="top-content-3">
				接受邀请后对方将可以查看您的个人粮商信息(姓名、手机号、账户信息和身份证附件等)。
			</view>
			<div class="content-bottom">
				<view v-if="grainMerchantList.length>0" class="title">请选择粮商身份</view>
				<view v-if="grainMerchantList.length>0">
					<view v-for="(item,index) in grainMerchantList" :key="index">
						<view v-if="item.cover!=1" :class="item.check?'item-select':'item'" @click="selectItem(item)">
							<image v-if="item.check" class="img" :src="img1" alt=""></image>
							<view class="name">{{item.customerName}}</view>
							<view class="phone">
								<view class="number">{{item.customerPhone}}</view>
								<view>（{{item.authenticationStatus}}）</view>
							</view>
						</view>
					</view>
				</view>
				<view class="nolist" v-if="grainMerchantList.length==0">
					<image class="img" :src="img2" alt=""></image>
					<view class="text">您还未认证个人粮商身份</view>
				</view>
				<button v-if="grainMerchantList.length>0" :class="flag?'':'custom-style'"
					@click="submit">{{btnVal}}</button>
				<button v-if="grainMerchantList.length==0" class="custom-style"
					@click="toGrainMerchantCertification()">接受并去认证</button>
			</div>
		</u-popup>
		<u-toast ref="warning" />
		<u-popup v-model="show1" class="popup" @close="close" mode="bottom" border-radius="30" :closeable="true">
			<view class="buns_item">
				<view class="but_css" @click="skip(1)" v-if="getRoles('acquisitionQuality.initial') && status == '已初检'">
					去初检
				</view>
				<view class="but_css" @click="skip(2)"
					v-if="getRoles('acquisitionQuality.again') && !payStatus && confirm != '1' && (status == '已初检'|| status == '已复检')">去复检</view>
				<view class="but_css" @click="skip(3)"
					v-if="getRoles('acquisitionWeight.mao') && weightStatus != '已称皮重' && (weightStatus == '已质检'|| weightStatus == '已称毛重')">
					去称毛重</view>
				<view class="but_css" @click="skip(4)"
					v-if="getRoles('acquisitionWeight.weight') && !payStatus && (weightStatus == '已称毛重' || weightStatus == '已称皮重')">
					去称皮重
				</view>
				<view class="but_css" @click="skip(5)"
					v-if="getRoles('acquisitionQuality.con') && confirm != '1' && status == '已复检'">
					去确认质检
				</view>
				<view class="but_css" @click="close">返回</view>
			</view>
		</u-popup>
	</view>

</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				img1: require("../../../static/img/select.png"),
				img2: require("../../../static/img/nolist.png"),
				show: false,
				show1: true,
				identityAuthenticationInfo: {},
				grainMerchantList: [],
				selectID: "",
				btnVal: "接受邀请",
				user: {
					title: '',
					companyId: ''
				},
				isAllselect: false,
				flag: true,
				cangid: "", //仓库id
				managementType: 1,
				warehouseName: "", //仓库名
				status: "", //质检状态
				weightStatus: "", //检斤状态
				qualityNo: "",
				confirm: "", //确认初检确认状态
				payStatus:"",//结算状态
				detailData: {},
				userName: "", //仓库负责人
				companyId: "",
				decide: false
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onLoad(options) {
			let _list = [];
			if(options.data){
				for (var p1 in options) {
				          if (options.hasOwnProperty(p1)){
							   _list.push(p1);
						  }              
				      }
			}
			//#ifdef APP-PLUS
			this.user.title = options.data.split(',')[2]
			this.user.companyId = options.data.split(',')[1]
			this.compId = options.data
			this.qualityNo = _list[1]
			this.cangid = _list[2]
			//#endif
		},
		onShow() {
			// this.compId = "2710b21efc1e4393930c5dc800010dc4"
			// this.qualityNo = "SGRK202201170060001"
			// this.cangid = "374a8a02ecfe40dc9abdcd902dd564f5"
			console.log("查看",this.qualityNo,this.compId,this.cangid)
			if (this.cangid) { //判断是在哪扫码进入的
				if(this.compId.indexOf(",") != -1){
					this.compId = this.compId.split(",")[1]
				}
				this.show = false
				this.show1 = true
				this.companyId = uni.getStorageSync('pcUserInfo').compId
				this.userName = uni.getStorageSync('userInfo').userName.split("-")[1]
				if (this.companyId == this.compId) {
					this.$api.doRequest('get', '/warehouseBaseInfo/getWarehouse', {
						id: this.cangid
					}).then(res => {
						let cangList = res.data.data
						let person = cangList.otherPersonCharge.split(",")
						person.push(cangList.personCharge)
						for (let i = 0; i < person.length; i++) {
							if (person[i].indexOf(uni.getStorageSync('userInfo').userName.split("-")[1]) != -1) {
								this.decide = true
								this.getShow()
								return
							}
						}
						if (!this.decide) {
							this.$refs.warning.show({
								title: '当前身份不可操作',
								type: "error",
								icon: false
							})
						}
					}).catch(res => {});
				}else{
				    this.$api.msg('当前身份不可操作！')
				}
			} else if(this.user.title && this.user.companyId){//邀请客户
				this.show = true
				this.show1 = false
				this.getList()
			}
			// this.getShow()
			// debugger
			// this.getRoles("acquisitionWeight.mao")
		},
		methods: {
			//查看权限
			getRoles: function(role) {
				// debugger
				let _roles = uni.getStorageSync('rolesList')
				for (let i = 0; i < _roles.length; i++) {
					if (_roles[i] == role) {
						return true
					}
				}
				return false
			},
			getShow() {
				let tmp = {
					compId: this.compId,
					number: this.qualityNo,
					warehouseId: this.cangid
				}
				this.$api.doRequest('get', '/weighingManagement/getQRcode', tmp).then(res => {
						this.detailData = res.data.data
						this.status = this.detailData.qualityInspectionManagement.status
						this.confirm = this.detailData.qualityInspectionManagement.confirm
						this.weightStatus = this.detailData.status
						if(this.detailData.paymentManagement){
							this.payStatus = this.detailData.paymentManagement.approveStatus
						}	
					})
					.catch(res => {
						if (res.message) {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000
							})
						} else {
							uni.showToast({
								title: "请稍后再试!",
								icon: 'none',
								duration: 2000
							})
						}
					});
			},
			//跳转
			skip(val) {
				if (val == 1) {
					uni.navigateTo({
						url: '/pages/erpbusiness/edit_quality_testing?id=' + this.detailData
							.qualityInspectionManagement.id + '&managementType=' + this
							.managementType + '&cangid=' + this.cangid + '&flag=' + '0'
					})
				} else if (val == 2) {
					uni.navigateTo({
						url: '/pages/erpbusiness/edit_quality_testing?id=' + this.detailData
							.qualityInspectionManagement.id + '&managementType=' + this
							.managementType + '&cangid=' + this.cangid + '&flag=' + '1'
					})
				} else if (val == 3) {
					this.detailData.warehouseId = this.cangid
					uni.navigateTo({
						url: '/pages/erpbusiness/acquisitionInspection/grossWeightDetail?detailData=' +
							JSON.stringify(this.detailData)
					})
				} else if (val == 4) {
					this.detailData.warehouseId = this.cangid
					uni.navigateTo({
						url: '/pages/erpbusiness/acquisitionInspection/tareDetail??detailData=' + JSON
							.stringify(this.detailData)
					})
				} else if (val == 5) {
					uni.navigateTo({
						url: '/pages/erpbusiness/edit_quality_testing?id=' + this.detailData
							.qualityInspectionManagement.id + '&managementType=' + this
							.managementType + '&cangid=' + this.cangid + '&flag=' + '2'
					})
				}
			},
			selectItem(item) {
				if (item.authenticationStatus == "已接受") return
				for (let i = 0; i < this.grainMerchantList.length; i++) {
					if (this.grainMerchantList[i].id == item.id) {
						this.grainMerchantList[i].check = !this.grainMerchantList[i].check
						if (this.grainMerchantList[i].check) {
							this.selectID = this.grainMerchantList[i].id
						}
					}
				}
			},
			//查询粮商身份
			selectGrainMerchantList() {
				//
			},
			//粮商认证
			toGrainMerchantCertification() {
				var that = this
				// url: `/pageA/product/list?keywords=${key}&TabCur=${TabCur}`
				uni.navigateTo({
					url: `/pageD/identity/companyIdentity?status=1&compId=${that.user.companyId}&ids=${that.user.title}`,
				})
			},

			getList() {
				this.identityAuthenticationInfo.commonId = this.userInfo.id
				this.grainMerchantList = []
				this.$api.doRequest('get', '/identityAuthenticationInfo/selectIdentityAuthenticationInfo', {
						pageSize: 100,
						currentPage: 1,
						commonId: this.userInfo.id,
						compId: this.user.companyId,
						flag: 3
					}, 'application/json;charset=UTF-8').then(res => {
						for (var i = 0; i < res.data.data.records.length; i++) {
							res.data.data.records[i].basis = "false"
							if (res.data.data.records[i].defaultFlag == 1) {
								res.data.data.records[i].basis = "true"
							}
						}
						var data = res.data.data.records
						for (var i = 0; i < data.length; i++) {
							if (data[i].authenticationStatus == "未通过") {
								continue
							}
							if (data[i].customerTypeFlag == 2) {
								if (data[i].compName && data[i].compName.length > 10) {
									data[i].compName = data[i].compName.substring(0, 10) + "..."
								}
							}
							if (data[i].authenticationStatus == "已接受") {
								data[i].check = true
								this.isAllselect = true
							} else {
								data[i].check = false
								this.isAllselect = false
								this.flag = false
							}
							this.grainMerchantList.push(data[i])
						}
						if (this.flag) {
							this.btnVal = "已接受"
						}
					})
					.catch(res => {
						if (res.message) {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000
							})
						} else {
							uni.showToast({
								title: "系统异常，请联系管理员",
								icon: 'none',
								duration: 2000
							})
						}
					});
			},
			submit() {
				if (this.flag) return
				let _isAllCheck = false
				for (let i = 0; i < this.grainMerchantList.length; i++) {
					if (this.grainMerchantList[i].check) {
						_isAllCheck = true
					}
				}
				if (!_isAllCheck) {
					this.$refs.uToast.show({
						title: '至少选择一个身份',
						type: "error",
						icon: false
					})
				}
				this.$api.doRequest('get', '/identityAuthenticationInfo/saveCompanyIdentity', {
						id: this.selectID,
						compId: this.user.companyId,
					}, 'application/json;charset=UTF-8').then(res => {
						console.log(res);
						if (res.data.code == 200) {
							// this.getList()
							uni.navigateBack(-1)
						}
					})
					.catch(res => {
						if (res.message) {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000
							})
						} else {
							uni.showToast({
								title: "接受邀请失败，请稍后再试。",
								icon: 'none',
								duration: 2000
							})
						}
					});

			},
			close() {
				uni.navigateBack(-1)
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background: white;
	}

	.top {
		background: url(../../../static/img/bg@2x.png);
		background-size: 100% 100%;
		padding: 0 80rpx 0 80rpx;
		text-align: center;
		height: 310rpx;
	}

	.content-bottom {
		padding: 50rpx 50rpx 50rpx 50rpx;

		.title {
			font-size: 32rpx;
			font-weight: 600;
			color: #333333;
			text-align: center;
			margin-bottom: 30rpx;
		}

		.active {}

		.item-select {
			border-radius: 1px;
			border: 1px solid #C5CAD5;
			padding: 15rpx 40rpx;
			background: #F4FAF4;
			border: 1px solid #22C572;
			margin-bottom: 20rpx;
			position: relative;

			.img {
				position: absolute;
				width: 42rpx;
				height: 40rpx;
				top: 0;
				right: 0;
			}

			.name {
				font-size: 32rpx;
				font-weight: 600;
				color: #22C572;
			}

			.phone {
				display: flex;
				align-items: center;
				color: #8E93A3;

				.number {
					margin-right: 20rpx;
				}
			}
		}

		.item {
			border-radius: 1px;
			border: 1px solid #C5CAD5;
			padding: 15rpx 40rpx;
			margin-bottom: 20rpx;
			position: relative;

			.img {
				position: absolute;
				width: 42rpx;
				height: 40rpx;
				top: 0;
				right: 0;
			}

			.name {
				font-size: 32rpx;
				font-weight: 600;
			}

			.phone {
				display: flex;
				align-items: center;
				color: #8E93A3;

				.number {
					margin-right: 20rpx;
				}
			}
		}
	}

	.top-center-1 {
		font-size: 42rpx;
		font-weight: 600;
		color: #333333;
		position: relative;
		top: 88rpx;
	}

	.top-content-2 {
		display: flex;
		justify-content: center;
		align-items: flex-end;
		position: relative;
		top: -50px;
	}

	.top-content-3 {
		position: relative;
		top: -25px;
		padding: 0 81rpx;
	}

	.top-content-green {
		color: #22C572;
		font-size: 32rpx;
	}

	.custom-style {
		background: #22C572;
		border-radius: 46px;
		margin: 50rpx;
		color: white;
	}

	.nolist {
		text-align: center;
		margin: 50rpx 50rpx 100rpx 50rpx;

		.img {
			width: 240rpx;
			height: 240rpx;
		}

		.text {
			margin: 20rpx;
			color: #8E93A3;
		}
	}

	.but_css {
		width: 200rpx;
		height: 80rpx;
		background-color: #22C572;
		margin: 20rpx auto;
		color: #FFFFFF;
		line-height: 80rpx;
		border-radius: 20rpx;
		text-align: center;
	}
	.buns_item{
		margin: 100rpx;
	}
</style>
