<template>
	<view class="box">
		<view class="from">
			<u-form :model="form" ref="uForm">
				<u-form-item>
					<view class="from_item">
						<text>抬头类型：</text>
						<u-radio-group v-model="radiovalue1">
							<u-radio :customStyle="{marginBottom: '8px'}" activeColor="#2CE242"
								v-for="(item, index) in radiolist1" :key="index" :label="item.disabled"
								:name="item.name" @change="radioChange">
								{{item.name}}
							</u-radio>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item>
					<view class="from_item">
						<text><text style="color: red;">*</text>抬头名称：</text>
						<view style="display: flex;">
							<u-input v-model="form.buyerName" class="input" placeholder="请输入抬头名称" />
						</view>
					</view>
				</u-form-item>
				<u-form-item>
					<view class="from_item">
						<text><text style="color: red;">*</text>纳税人识别号：</text>
						<view style="display: flex;">
							<u-input v-model="form.buyerTaxNo" class="input" placeholder="请输入纳税人识别号" />
						</view>
					</view>
				</u-form-item>
				<u-form-item v-if="form.titleType==1">
					<view class="from_item">
						<text>单位地址</text>
						<view style="display: flex;">
							<u-input v-model="form.companyAddress" class="input" placeholder="请输入单位地址" />
						</view>
					</view>
				</u-form-item>
				<u-form-item v-if="form.titleType==1">
					<view class="from_item">
						<text>公司电话号码</text>
						<view style="display: flex;">
							<u-input v-model="form.companyTel" class="input" placeholder="请输入公司电话号码" type="number"
								maxlength="11" />
						</view>
					</view>
				</u-form-item>
				<u-form-item v-if="form.titleType==1">
					<view class="from_item">
						<text>开户银行</text>
						<view style="display: flex;">
							<u-input v-model="form.bankName" class="input" placeholder="请输入开户银行" />
						</view>
					</view>
				</u-form-item>
				<u-form-item v-if="form.titleType==1">
					<view class="from_item">
						<text>银行账户</text>
						<view style="display: flex;">
							<u-input v-model="form.bankAccount" class="input" placeholder="请输入银行账户" type="number" />
						</view>
					</view>
				</u-form-item>
				<view class="from_item">
					<text>设置默认抬头</text>
					<u-switch v-model="checked" active-color="#00B38B" inactive-color="#DCDCDC"
						@change='change'></u-switch>
				</view>
			</u-form>
		</view>

		<button class="submit" @click="submit()">保存</button>
	</view>

</template>

<script setup lang="ts">
	import {
		ref,
		reactive
	} from "vue";
	import { billRaiseInsert } from "@/utils/network/api.js";
	import {requestNew } from "@/utils/network/request.js";
	import { msg } from "@/utils/utils";
	import { onLoad } from "@dcloudio/uni-app";
	const form = reactive({
		titleType: 1, //抬头类型 1-单位抬头 2个人抬头
		buyerName: '',
		isDefault: -1, //1：默认抬头；-1：非默认抬头
		buyerTaxNo: "",//纳税人识别号
		companyAddress: "", //单位地址
		companyTel: "",//公司电话号码
		bankName: "",//开户银行
		bankAccount: "",//银行账号
		userMobile: "",//客户手机号
	})
	// 单选数据列表
	const radiolist1 = reactive([{
		name: '单位',
		disabled: false
	},
	{
		name: '个人',
		disabled: false
	},
	])
	onLoad((options) => {
		form.userMobile = options.userMobile
	})
	const checked = ref(false)
	const change = (status) => {
		if (status) {
			form.isDefault = 1
		} else {
			form.isDefault = -1
		}
		console.log(status, form.isDefault);
	}
	// 单选默认数据
	const radiovalue1 = ref('单位')
	// 单选
	const radioChange = (n) => {
		console.log('radioChange', n);
		if (n == '单位') {
			form.titleType = 1
			console.log(form.titleType);
		} else {
			form.titleType = 2
			console.log(form.titleType);
		}
		empty()
	}
	const empty = () => {
		form.buyerName = "";
		form.isDefault = -1;
		form.buyerTaxNo = "";
		form.companyAddress = "";
		form.companyTel = "";
		form.bankName = "";
		form.bankAccount = "";
		checked.value = false
	}
	const submit = () => {
		if (!form.buyerName) {
			msg("请输入抬头名称！");
			return;
		}
		// 抬头类型为单位时必填 纳税人识别号
		if (!form.buyerTaxNo) {
			msg("请输入纳税人识别号！");
			return;
		}
		var data = {
			titleType: form.titleType,
			buyerName: form.buyerName,
			isDefault: form.isDefault,
			buyerTaxNo: form.buyerTaxNo,
			companyAddress: form.companyAddress,
			companyTel: form.companyTel,
			bankName: form.bankName,
			bankAccount: form.bankAccount,
			userMobile: form.userMobile,
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};

		requestNew(billRaiseInsert, options).then((res) => {
			const data =res;
			console.log("data", data)
			uni.navigateBack({
				delta: 1
			})
		});
	}
</script>


<style>
	page {
		width: 100%;
		height: 100%;
	}
</style>

<style lang="scss" scoped>
	.box {
		width: 100%;
		height: 100%;
		background-color: #EEF7F7;
		border-top: 1rpx solid #DFDFDF;

		.from {
			background-color: #fff;
			margin-top: 30rpx;
			padding: 0 30rpx;
			box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);

			::v-deep .u-form-item {
				padding: 0;
				line-height: normal;

				.u-form-item__message {
					margin-bottom: 12rpx
				}
			}

			.from_item {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-between;
				border-bottom: #DCDCDC 1px solid;
				align-items: center;
				height: 80rpx;

				text {
					font-size: 28rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #999999;
				}

				::v-deep .input {
					text-align: right;
					flex: 1;

					input {
						text-align: right;
					}
				}
			}

			.from_item1 {
				display: flex;
				flex-wrap: nowrap;
				flex-direction: column;
				justify-content: space-between;
				padding: 30rpx;
				border-bottom: #DCDCDC 1px solid;

				input {
					text-align: right;
				}

				.textarea {
					background-color: #F1F1F1;
					width: 100%;
					border-radius: 20rpx;
					margin-top: 10rpx;
					text-indent: 1rem;
					height: 180rpx;
					padding: 20rpx;
					box-sizing: border-box;
				}
			}
		}

		.submit {
			width: 670rpx;
			height: 80rpx;
			background: linear-gradient(-90deg, #43A1E0 0%, #13E7C1 100%);
			border-radius: 40rpx;
			font-size: 32rpx;
			font-family: Noto Sans S Chinese;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 80rpx;
			position: fixed;
			left: 50%;
			transform: translate(-50%);
			bottom: 100rpx;
		}
	}

	::v-deep .u-icon__icon {
		top: -4px !important;
	}
</style>