<template>
	<view class="Box">
		<view class="Box_box_O">
			<view class="Box_box flex-between" @click="showpopupclick">
				<view>*项目名称</view>
				<view class=""
					style="width: 60%;  overflow:hidden; font-size:32rpx;text-overflow: ellipsis;white-space: nowrap; "
					v-model="form.article">{{ form.article}} <text v-if="!form.article"
						style="color: #86909c;">选择项目名称</text>
				</view>
			</view>
			<view class="Box_box flex-between">
				<view>*金额</view>
				<view><input type="digit" v-model="form.price" @input="onInput" placeholder="请填写金额" /></view>
			</view>
			<view class="Box_box flex-between">
				<view>*数量</view>
				<view><input type="number" v-model="form.number" @input="handleInputChange" placeholder="请填写数量"
						data-key="mobile" /></view>
			</view>
			<view class="Box_box flex-between" @click="showpicker = true">
				<view class="Box_boxlang Box_box_bouttme_item_O">*发票类型</view>
				<view class="Box_box_bouttme_item_T">
					<text type="text" v-model="form.type">{{ form.type || '请输入发票类型' }}</text>
				</view>
			</view>
			<view class="Box_box flex-between">
				<view>*税额</view>
				<view><input type="text" disabled='false' v-model="form.tax_amount" placeholder="请填写税额"
						data-key="mobile" /></view>
			</view>
			<view class="Box_box flex-between">
				<view>*单价</view>
				<view><input type="text" disabled='false' v-model="form.d_price" placeholder="请填写单价"
						data-key="mobile" /></view>
			</view>
			<view class="Box_box flex-between" @click="Issuedbyshowpicker = true">
				<view class="Box_boxlang Box_box_bouttme_item_O">*开票人</view>
				<view class="Box_box_bouttme_item_T">
					<text type="text" v-model="form.dlzhid">{{ form.dlzhid || '请选择开票人' }}</text>
				</view>
			</view>
			<view class="Box_box flex-between">
				<view>备注：</view>
				<u--textarea v-model="form.notes" placeholder="请输入内容"></u--textarea>
			</view>
		</view>
		<view class="preservation flex-colum">
			<view class="preservation_box" @tap="$u.throttle(saveMerchantBaseInfoV2, 1000)">提交</view>
		</view>
		<u-popup :show="showpopup" @close="showpopup = false" @open="openpopup">
			<view class="popupshow">
				<view class="popupshowone">请选择项目名称</view>
				<view class="popupshowone_item" style="	border-bottom: 1rpx solid #E5E5E5;" v-if="keyword">
					<view class="popupshowone_itemone" @click="popupshowone_itemone">
						{{keyword}}
					</view>
				</view>
				<view class="popupshowonebotton" @click="businesscategory">搜索其他项目名称</view>
			</view>
		</u-popup>
		<u-picker :show="showpicker" @confirm="showpickerconfirm" :columns="columns" @cancel="showpicker = false"
			:defaultIndex='[0,0]'></u-picker>
		<u-picker :show="Issuedbyshowpicker" @confirm="Issuedbyconfirm" :columns="Issuedbycolumns"
			@cancel="Issuedbyshowpicker = false" keyName='name'></u-picker>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				keyword: uni.getStorageSync('keyword').name,
				showpopup: false,
				showpicker: false,
				Issuedbyshowpicker: false,
				columns: [],
				Issuedbycolumns: [],
				Uploadurlnumber: '',
				namelang: '',
				merchantAuditStatus: '',
				form: {
					store_id: uni.getStorageSync('userId'),
					article: uni.cache.get('keyword').name,
					type: '',
					price: '',
					se_amount: '',
					number: '',
					tax_amount: '',
					d_price: '',
					notes: '',
					mcc: uni.cache.get('keyword').code,
					dlzh: '',
					dlzhid: "",
					is_new: 1
				}
			};
		},
		onNavigationBarButtonTap() {
			uni.pro.navigateTo('index/order');
		},
		async onLoad(e) {
			// #ifdef APP-PLUS
			let webView = this.$mp.page.$getAppWebview();
			webView.setTitleNViewButtonStyle(0, {
				width: '70px'
			});
			// #endif
			let res = await this.api.storedigitalinvoice()
			this.columns = res.data
			let ress = await this.api.szzpyissuedby({
				store_id: uni.getStorageSync('userId'),
			})
			this.Issuedbycolumns = [ress.data]
			console.log(this.Issuedbycolumns)
		},
		onShow() {
			uni.$on('businesscategorys', res => {
				console.log(res);
				this.form.mcc = uni.cache.get('keyword').code
				this.form.article = uni.cache.get('keyword').name
				this.keyword = uni.cache.get('keyword').keyword
				// this.form.contactLine = res.unionpayCode
				// this.form.branchName = res.branchName
			});
		},
		methods: {
			showpopupclick() {
				this.keyword = uni.getStorageSync('keyword').name
				this.showpopup = true
			},
			popupshowone_itemone() {
				this.showpopup = false
				this.form.article = this.keyword
			},
			handleInputChange(e) {
				if (this.form.price == null || this.form.price == '') {
					return false;
				}
				if (this.form.number == null || this.form.number == '') {
					return false;
				}
				if (this.form.type == null || this.form.type == '') {
					return false;
				}
				uni.$u.debounce(this.showpickerconfirm(), 500)

			},
			onInput(e) {
				if (this.form.price == null || this.form.price == '') {
					return false;
				}
				if (this.form.number == null || this.form.number == '') {
					return false;
				}
				if (this.form.type == null || this.form.type == '') {
					return false;
				}
				if (e.detail.value) {
					uni.$u.debounce(this.showpickerconfirm(), 500)
				} else {
					this.subCategoryList = []
				}
			},
			showpickerconfirm(e) {
				if (this.form.price == null || this.form.price == '') {
					uni.showToast({
						title: '请输入开票金额',
						icon: 'none'
					});
					return false;
				}
				if (this.form.number == null || this.form.number == '') {
					uni.showToast({
						title: '请输入数量',
						icon: 'none'
					});
					return false;
				}
				try {
					if (e.value) {
						if (e.value[1] == "1%") {
							this.form.se_amount = 1
						} else {
							this.form.se_amount = 3
						}
						this.form.type = e.value.toString();
					}
				} catch (e) {
					//TODO handle the exception
				}
				uni.request({
					url: uni.conf.baseUrl + 'store/se',
					data: {
						//参数
						amount: this.form.price,
						se_amount: this.form.se_amount
					},
					method: 'POST', //请求方式，必须为大写
					success: res => {
						this.form.tax_amount = res.data.data.tx
						this.form.d_price = res.data.data.d_amount
						this.showpicker = false;
					}
				});
			},
			Issuedbyconfirm(e) {
				this.Issuedbyshowpicker = false
				console.log(e)
				if (e.value) {
					this.form.dlzh = e.value[0].id
					this.form.dlzhid = e.value[0].name
					// this.form.dlzh = e.value.toString();
				}
			},
			businesscategory() {
				this.showpopup = false
				uni.pro.navigateTo('index/businesscategory');
			},
			async saveMerchantBaseInfoV2() {
				if (this.form.article == null || this.form.article == '') {
					uni.showToast({
						title: '选择项目名称',
						icon: 'none'
					});
					return false;
				}
				if (this.form.type == null || this.form.type == '') {
					uni.showToast({
						title: '请选择类型',
						icon: 'none'
					});
					return false;
				}
				if (this.form.price == null || this.form.price == '') {
					uni.showToast({
						title: '请输入开票金额',
						icon: 'none'
					});
					return false;
				}
				if (this.form.number == null || this.form.number == '') {
					uni.showToast({
						title: '请输入数量',
						icon: 'none'
					});
					return false;
				}
				if (this.form.tax_amount == null || this.form.tax_amount == '') {
					uni.showToast({
						title: '请输入税额',
						icon: 'none'
					});
					return false;
				}
				if (this.form.d_price == null || this.form.d_price == '') {
					uni.showToast({
						title: '请输入单价',
						icon: 'none'
					});
					return false;
				}
				if (this.form.dlzh == null || this.form.dlzh == '') {
					uni.showToast({
						title: '请选择开票人',
						icon: 'none'
					});
					return false;
				}
				uni.request({
					url: uni.conf.baseUrl + 'store/subinvoicing',
					data: this.form,
					method: 'POST', //请求方式，必须为大写
					success: res => {
						console.log(res)
						this.form = {
							store_id: uni.getStorageSync('userId'),
							article: uni.cache.get('keyword').name,
							type: '',
							price: '',
							se_amount: '',
							number: '',
							tax_amount: '',
							d_price: '',
							notes: '',
							mcc: uni.cache.get('keyword').code,
							dlzh: '',
							is_new:1
						}
						uni.showToast({
							title: res.data.message || res.data.msg,
							icon: "none",
						})
						setTimeout(datalange => {
							uni.pro.navigateTo('index/wxinvoicing', res.data.data.invoice_records)
						}, 2000)

					}

				});
			}
		}
	};
</script>

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

	.Box {
		.Box_box_O {
			padding: 0 28rpx;

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

			.Box_box_bouttme_item_T {
				padding-left: 30rpx;
				flex: 1;
				text-align: right;

				text {
					color: #808080;
				}
			}
		}

		.Box_box_T {
			padding: 0 28rpx;
			border-top: 16rpx solid #e5e5e5;

			.Box_box_T_o {
				margin-top: 16rpx;
			}

			.flex-colum {
				image {
					width: 396rpx;
					height: 240rpx;
				}

				padding-bottom: 16rpx;
				border-bottom: 1px solid #e5e5e5;

				.Box_box_Ts {
					width: 396rpx;
					height: 240rpx;
					background: #ffffff;
					border-radius: 8rpx;
					border: 2rpx dashed #707070;
					background: url(@/static/my2.png) no-repeat;
					background-position: 50% 50%;
					background-size: 50%;
				}
			}
		}

		.preservation {
			margin-top: 50rpx;
			width: 100%;

			.preservation_box {
				width: 70%;
				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;
			}
		}

		.popupshow {
			width: 100%;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 0rpx 0rpx;
			padding-bottom: 50rpx;

			.popupshowone {
				margin-top: 22rpx;
				text-align: center;
				width: 100%;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 400;
				font-size: 32rpx;
				color: #333333;
			}

			.popupshowone_item {
				padding: 26rpx 52rpx;

				.popupshowone_itemone {
					text-align: center;
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 500;
					font-size: 36rpx;
					color: #333333;
				}

			}

			.popupshowonebotton {
				margin: 100rpx auto 0 auto;
				width: 558rpx;
				height: 84rpx;
				background: #288EFB;
				border-radius: 50rpx 50rpxmy2 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;
				padding-bottom: 32rpx;
			}
		}
	}
</style>