<template>
	<view class="content">
		<navseat :opacity='false' :title='"开票审核"' :titleshow='true' :iconshow='false' :heightshow='true'
			:namecolor='"#fff"' iconcolor='#fff'></navseat>
		<view class="content_top">
			<view :class="form.user_type == 1 ? 'content_topview':''" @click="clickakex(1)">
				个人
			</view>
			<view :class="form.user_type == 2 ? 'content_topview':''" @click="clickakex(2)">
				企业
			</view>
		</view>
		<view class="content_item">
			<view class="Box_box flex-between" v-if="form.user_type == 1">
				<view>*姓名</view>
				<view><input type="text" v-model="form.user_name" placeholder="请填写名称" /></view>
			</view>
			<view class="Box_box flex-between" v-if="form.user_type == 2">
				<view>*单位名称</view>
				<view style="position: relative; width:70%;">
					<input type="text" v-if="subCategoryList.length == 0" v-model="form.user_name" placeholder="请填写公司名称"
						@input="onInput" />
					<view class="position" v-else>
						<view class="positionitem" v-for="(item,index) in subCategoryList" :key="index"
							@click="categorySubClick(item)">
							{{item.companyName}}
						</view>
					</view>
				</view>
			</view>
			<view class="Box_box flex-between" v-if="form.user_type == 2">
				<view>*税号</view>
				<view><input type="text" v-model="form.user_duty" placeholder="请填写公司税号" /></view>
			</view>
			<view class="Box_box flex-between">
				<view>*邮箱</view>
				<view><input type="text" v-model="form.email" placeholder="请填写邮箱" /></view>
			</view>
			<view class="Box_box flex-between">
				<view>*手机号</view>
				<view><input type="number" v-model="form.mobile" placeholder="请填写联系电话" /></view>
			</view>
			<view class="Box_box flex-between">
				<view>*开票价格</view>
				<view><input type="text" v-model="form.price" placeholder="请填写价格" /></view>
			</view>
			<view class="Box_box flex-between">
				<view>备注：</view>
				<u--textarea v-model="form.notes" placeholder="请输入内容"></u--textarea>
			</view>
			<view class="Box_boxbox" @click="upload">
				<view class="Box_boxone">*上传账单</view>
				<view class="Box_boxtow" v-if="form.img">
					<image :src="form.img" style="width: 100%; height: 100%;" mode=""></image>
				</view>
				<view class="Box_boxtow" v-else>
					<image src="https://czg-invoicing.oss-cn-beijing.aliyuncs.com/static/xj.png"
						style="width: 60rpx;height: 58rpx;" mode=""></image>
				</view>
			</view>
			<view class="preservation flex-colum">
				<view v-if="form.status == 6"
					style="width: 100%; color: #FF2B2B; padding: 28rpx 0;word-wrap: break-word;">
					拒绝理由:{{form.no|| '无'}}</view>
			</view>
		</view>
		<view class="content_bottom" @click="requestSubscribeMessage">
			确定
		</view>
	</view>
</template>

<script>
	import _ from 'lodash'
	import navseat from '@/components/navseat.vue'
	import uploadImage from "@/js_sdk/yushijie-ossutil/ossutil/uploadFile.js";
	export default {
		components: {
			navseat
		},
		data() {
			return {
				showtext: 0,
				subCategoryList: [],
				open_id: '',
				form: {
					id: '',
					user_type: 2,
					img: '',
					user_name: "",
					user_duty: "",
					open_id: '',
					email: "",
					mobile: '',
					status_note: '', //原因备注
					price: '',

				},
				queryParams: {

				}
			}
		},
		async onLoad(e) {
			await this.$onLaunched;
			// e.id是从列表进的id
			if (e.q) {
				try {
					if (e.q) {
						const q = decodeURIComponent(e.q) // 获取到二维码原始链接内容
						// this.form.id = e.id
						let queryParams = {};
						let queryString = q.split('?')[1];
						if (queryString) {
							// 将查询字符串分割成单个参数
							let params = queryString.split('&');
							params.forEach((param) => {
								this.form.body = param.split('=')[1] ? param.split('=')[1] : '';
							});
						}

					}
				} catch (e) {}
			} else {
				this.form.body = e.id
			}
			// 获取开票抬头
			// 这是H5
			// #ifdef H5
			this.getInvoiceList()
			this.form.body = e.id
			this.form.open_id = e.open_id
			// #endif


			// 这是微信
			// #ifdef MP-WEIXIN
			this.showtitle = true
			this.form.open_id = uni.cache.get('open_id')
			// #endif


			try {
				let res = await this.api.szzpyfailccho({
					id: this.form.body,
					view_type: this.form.view_type
				})
				if (res.code == 1) {
					this.form = res.data
				}
			} catch (e) {
				//TODO handle the exception
			}
		},
		methods: {
			onInput(e) {
				if (this.form.user_type == 2) {
					if ([...e.detail.value].length >= 6) {
						this.handleClick()
					} else {
						this.subCategoryList = []
					}
				}

			},
			handleClick: _.debounce(function() {
				this.getOrder()
				// 函数执行内容

			}, 500),
			categorySubClick(category) {
				console.log(category, 86)
				this.form.user_name = category.companyName
				this.form.user_duty = category.creditCode
				this.subCategoryList = []
			},
			upload() {
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择，和摄像头功能，默认二者都有
					success: res => {
						var thisimgUrlcaca = res.tempFilePaths[0];
						uploadImage(thisimgUrlcaca, 'merchantinformation/',
							result => {
								uni.showToast({
									title: '上传成功',
									icon: 'none',
								});
								this.form.img = result
								console.log(this.form)
								uni.hideLoading()
							}, result => {
								uni.hideLoading()
								uni.showToast({
									title: result.message || result.msg,
									icon: 'none',
								});
							})
					}
				});
			},
			requestSubscribeMessage() {
				var this_ = this
				var tmplIds = 'LQ4X2dgvpxC6iuZhbxv8qtqVWT9_DFEBPxu-PmggjfY'
				uni.getSetting({
					withSubscriptions: true,
					success(res) {
						// 调起授权界面弹窗
						if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
							if (res.subscriptionsSetting.itemSettings !=
								null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
								let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
								if (moIdState === 'accept') {
									uni.requestSubscribeMessage({ // 调起消息订阅界面
										tmplIds: [tmplIds],
										success(res) {
											if (res[tmplIds] == 'accept') {
												this_.overlayshowclick(1)
											} else if (res[tmplIds] == 'reject') {
												this_.overlayshowclick(0)
											} else if (res[tmplIds] == 'ban') {
												this_.overlayshowclick(0)
											} else if (res[tmplIds] == 'accept') {
												this_.overlayshowclick(0)
											} else {
												this_.overlayshowclick(0)
											}
										},
										fail(er) {
											this_.overlayshowclick(0)
											console.log(er);
										}
									})

								} else if (moIdState === 'reject') {
									console.log("拒绝消息推送");
									this_.overlayshowclick(0)

								} else if (moIdState === 'ban') {
									console.log("已被后台封禁");
									this_.overlayshowclick(0)
								}
							} else {
								// 当用户没有点击 ’总是保持以上选择，不再询问‘  按钮。那每次执到这都会拉起授权弹窗
								uni.showModal({
									title: '提示',
									content: '请授权开通服务通知',
									showCancel: true,
									success: function(ress) {
										if (ress.confirm) {
											// console.log('用户点击确定')
											uni.requestSubscribeMessage({ // 调起消息订阅界面
												tmplIds: [tmplIds],
												success(res) {
													if (res[tmplIds] == 'accept') {
														this_.overlayshowclick(1)
													} else if (res[tmplIds] == 'reject') {
														this_.overlayshowclick(0)
													} else if (res[tmplIds] == 'ban') {
														this_.overlayshowclick(0)
													} else if (res[tmplIds] == 'accept') {
														this_.overlayshowclick(0)
													} else {
														this_.overlayshowclick(0)
													}
												},
												fail(er) {
													this_.overlayshowclick(0)
													console.log(er);
												}
											})

										} else if (ress.cancel) {
											this_.overlayshowclick(0)
										}
									}
								})
							}
						} else {
							this_.overlayshowclick(0)
						}
					}
				})
			},
			async overlayshowclick(e) {
				try {
					if (e == 1) {
						await this.api.userminidymessage({
							open_id: this.form.open_id,
						})
					}
				} catch (e) {
					//TODO handle the exception
				}
				if (this.form.user_name == null || this.form.user_name == '') {
					uni.showToast({
						title: '请输入名称',
						icon: 'none'
					});
					return false;
				}
				if (this.form.user_type == 2) {
					if (this.form.user_duty == null || this.form.user_duty == '') {
						uni.showToast({
							title: '请输入税号',
							icon: 'none'
						});
						return false;
					}
				}
				if (this.form.email == null || this.form.email == '') {
					uni.showToast({
						title: '请输入邮箱',
						icon: 'none'
					});
					return false;
				}
				if (this.form.mobile == null || this.form.mobile == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return false;
				}
				if (this.form.img == null || this.form.img == '') {
					uni.showToast({
						title: '请上传图片',
						icon: 'none'
					});
					return false;
				}
				// http://localhost:8080/pages/hindex/index?id=12&open_id=102132
				let res = await this.api.usersubopeninvoicefixed(this.form)
				if (res.code == 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
					// #ifdef MP-WEIXIN
					uni.navigateTo({
						url: '/pages/hindex/involist?typeshow=' + 1
					})
					// #endif
					// #ifndef MP-WEIXIN
					setTimeout(() => {
						if (res.data.url) {
							window.location.href = res.data.url
						}
					}, 1000);
					// #endif

				}

			},
			clickakex(e) {
				this.form.user_type = e
				this.form.user_name = ''
				this.form.img = ''
				this.form.user_duty = ''
				this.form.email = ''
				this.form.mobile = ''
			},
			async getOrder() { //搜索值
				console.log(this.form.open_id)
				let res = await this.api.enterprisestore({
					like: this.form.user_name,
					open_id: this.form.open_id,
					sign: uni.utils.md5(this.form.user_name + 'd2e862a1-02b5-4405-8c69-c74ca871bcfe')
				})
				if (res.code == 1) {
					setTimeout(() => {
						if (this.page == 1) {
							this.subCategoryList = res.data;
						} else {
							this.subCategoryList.push(...res.data);
						}
					}, 500)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #F9F9F9;
	}

	.content {
		padding: 0 28rpx;

		.content_top {
			padding: 32rpx 110rpx;
			width: 100%;
			display: flex;
			justify-content: space-between;

			.content_topview {
				background: #288EFB;
				color: #fff;
			}

			view {
				width: 142rpx;
				height: 56rpx;
				background: #fff;
				color: #288EFB;
				border-radius: 6rpx 6rpx 6rpx 6rpx;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 400;
				font-size: 28rpx;
				line-height: 56rpx;
				text-align: center;
			}
		}

		.content_item {
			width: 100%;
			background: #FFFFFF;
			border-radius: 18rpx 18rpx 18rpx 18rpx;

			.Box_box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				flex-wrap: wrap;
				text-align: right;
				padding: 24rpx;
				border-bottom: 1px solid #e5e5e5;
				font-size: 28rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #333333;

				.position {
					position: absolute;
					right: 0;
					top: 0;
					background: #fff;
					width: 100%;
					max-height: 400rpx;
					overflow: auto;
					z-index: 99;
					box-shadow: 1rpx 3rpx 6rpx #ccc;

					.positionitem {
						padding: 16rpx 10px;
						border-bottom: 1rpx solid #ccc;
					}
				}

			}

			.preservation {
				width: 100%;
				text-align: center;

				.preservation_box {
					width: 100%;
					height: 72rpx;
					background: #288efb;
					text-align: center;
					line-height: 72rpx;
					border-radius: 50rpx;
					font-size: 36rpx;
					font-family: Source Han Sans CN-Regular, Source Han Sans CN;
					font-weight: 400;
					color: #ffffff;
				}

				.preservation_boxs {
					width: 70%;
					height: 72rpx;
					background: #999999;
					text-align: center;
					line-height: 72rpx;
					border-radius: 50rpx;
					font-size: 36rpx;
					font-family: Source Han Sans CN-Regular, Source Han Sans CN;
					font-weight: 400;
					color: #ffffff;
				}
			}

			.Box_boxbox {
				width: 100%;
				padding: 16rpx;

				.Box_boxone {
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 400;
					font-size: 28rpx;
					color: #333333;
				}

				.Box_boxtow {
					display: flex;
					justify-content: center;
					align-items: center;
					margin: 0 auto;
					width: 254rpx;
					height: 156rpx;
					background: #FFFFFF;
					border: 1rpx dashed #707070;
				}
			}
		}

		.content_bottom {
			margin: 20rpx auto 100rpx;
			width: 80%;
			height: 84rpx;
			background: #288EFB;
			border-radius: 50rpx 50rpx 50rpx 50rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 400;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 84rpx;
			text-align: center;
		}

	}
</style>