<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>

		<view class="button-wrap">
			<button class="submit" @click="del()">删除</button>
			<button class="submit" @click="submit()">保存</button>
		</view>
	</view>

</template>

<script setup lang="ts">
	import {
		ref,
		reactive
	} from "vue";
	import { billRaiseUpdate, billRaiseDelete } from "@/utils/network/api.js";
	import { requestNew } from "@/utils/network/request.js";
	import { stringToJson } from "@/utils/network/encryption";
	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: "",//客户手机号
		id: "",
	})
	// 单选数据列表
	const radiolist1 = reactive([{
		name: '单位',
		disabled: false
	},
	{
		name: '个人',
		disabled: false
	},
	])
	// 单选默认数据
	const radiovalue1 = ref('单位')
	onLoad((options) => {
		let data = JSON.parse(decodeURIComponent(options.params))
		form.userMobile = options.userMobile
		console.log("data", data, data.isDefault)
		form.buyerName = data.buyerName;
		form.isDefault = data.isDefault;
		form.buyerTaxNo = data.buyerTaxNo;
		form.companyAddress = data.companyAddress;
		form.companyTel = data.companyTel;
		form.bankName = data.bankName;
		form.bankAccount = data.bankAccount;
		form.titleType = data.titleType;
		form.id = data.id;
		if (data.titleType == 1) {
			radiovalue1.value = '单位'
		} else {
			radiovalue1.value = '个人'
		}
		if (data.isDefault > 0) {
			checked.value = true
		} else {
			console.log("data.isDefault", data.isDefault)
			checked.value = false
		}
	})

	const checked = ref(false)
	const change = (status) => {
		if (status) {
			form.isDefault = 1
		} else {
			form.isDefault = -1
		}
		console.log(status, form.isDefault);
	}

	// 单选
	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,
			id: form.id
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};

		requestNew(billRaiseUpdate, options).then((res) => {
			const data = res;
			console.log("data", data)
			uni.navigateBack({
				delta: 1
			})
		});
	}
	const del = () => {
		var data = {
			id: form.id,
			userMobile: form.userMobile,
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};

		requestNew(billRaiseDelete, 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: 200rpx;
			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;
		}
	}

	::v-deep .u-icon__icon {
		top: -4px !important;
	}

	.button-wrap {
		display: flex;
		justify-content: space-evenly;
		margin-top: 100rpx;
	}
</style>