<template>
	<view class="curpage">
		<radio-group @change="radioChange">
			<label v-for="(item, index) in list" :key="index">
				<view class="list">
					<view class="title">
						<view v-show="choose">
							<radio :value="index" :checked="index === current" />
						</view>
						<view style="padding-left: 20rpx;">
							<view class="">
								{{item.name}}
							</view>
							<view class="" style="margin: 10rpx 0;">
								<text style="color: #666666;">类型：</text>
								{{item.type}}
							</view>
							<view class="" v-if="item.type=='企业'">
								<text style="color: #666666;">税号：</text>
								{{item.tax_no}}
							</view>
						</view>

					</view>
					<view class="icon">
						<text @click="goendit(item)">
							编辑
						</text>
						<text class="line"></text>
						<text class="dele" @click="dele(item.id)">
							删除
						</text>
					</view>
				</view>

			</label>
		</radio-group>
		<!-- <view class="list" v-for="item,index in list" :key="index">
			
		</view> -->
		<u-loadmore v-if="list.length==0" :status="nomore" fontSize="30" :line="true" nomoreText="暂无发票" />

		<view class="" style="height: 1rpx;margin-bottom: 144rpx;"></view>
		<view class="navbar" @click="add" v-show="!choose">
			+ 新增常用发票
		</view>
		<view class="navbar" @click="makSure" v-show="choose">
			确认
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,
				choose: false,
				id: 1,
				list: [],
				state: 0
			}
		},
		onLoad(o) {
			console.log(o)
			if (o.obj) {
				let obj = JSON.parse(o.obj)
				// console.log(obj, 888)
				this.choose = obj.choose
				this.state = obj.state
				console.log(obj)
			}
			
			this.init()
		},
		onShow() {
			let token = uni.getStorageSync("token")
			if (!token) {
				//未登录
				uni.showToast({
					title: "请先登录",
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateTo({
						url: "/pages/login"
					})
				}, 1500)

				return false
			}
			// uni.showLoading({
			// 	title: '加载中'
			// });
			// this.init()
		},
		methods: {
			init() {
				uni.$u.http.post('/api/invoice/list', {
					custom: {
						auth: true
					}
				}).then((res) => {
					uni.hideLoading();
					console.log(res)
					res.forEach(item => {
						if (item.type == 1) {
							item.type = "个人"
						} else if (item.type == 2) {
							item.type = "企业"
						}
					})
					if (this.choose) {
						if (this.state == 0) {
							res.forEach(item=>{
								if(item.type=="企业"){
									this.list.push(item)
								}
							})
						} else if (this.state == 1) {
							res.forEach(item=>{
								if(item.type=="个人"){
									this.list.push(item)
								}
							})
						}
					} else {
						this.list = res
					}


				}).catch((err) => {
					console.log(err)
				})
			},
			makSure() {
				let obj = this.list[this.current]
				console.log(obj,88888)
				uni.navigateTo({
					url: '/pages/invoice/applyInvoice?obj=' + JSON.stringify(obj)
				})
			},
			radioChange(e) {
				this.current = e.detail.value
				console.log(e)
			},
			// 编辑发票
			goendit(item) {
				uni.navigateTo({
					url: `/pages/invoice/invoiceEndit?state=0&info=${JSON.stringify(item)}`
				})
			},
			// 增加发票
			add() {
				uni.navigateTo({
					url: "/pages/invoice/invoiceEndit?state=1"
				})
			},
			// 删除发票
			dele(id) {
				uni.showModal({
					title: "提示",
					content: "确认删除吗？",
					success: (res) => {
						if (res.confirm) {
							this.$showLoadding("删除中")
							uni.$u.http.post('/api/invoice/delete', {
								id
							}, {
								custom: {
									auth: true
								}
							}).then((res) => {
								uni.hideLoading()
								this.$toast("删除成功")
								this.init()
							}).catch((err) => {
								console.log(err)
								uni.showToast({
									icon: "error",
									title: err.message,
								})
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F4F4F4;
		font-size: 28rpx;
	}

	.curpage {
		.list {
			background: #FFFFFF;
			box-shadow: 0px 2rpx 4rpx 0px rgba(0, 0, 0, 0.02);
			border-radius: 16rpx 16rpx 16rpx 32rpx;
			margin: 30rpx;

			.title {
				display: flex;
				align-items: center;
				padding: 30rpx;
				border-bottom: 1rpx solid #E3E3E3;
			}

			.dele {
				// border-left: 1rpx solid #1E9F6A;
				// padding-left: 40rpx;
				margin-right: 40rpx;
			}

			.icon {
				text-align: right;
				padding: 40rpx 0rpx;
				color: #1E9F6A;

				.line {
					display: inline-block;
					width: 1px;
					height: 30rpx;
					background-color: #1E9F6A;
					margin: 0 40rpx -4rpx;
				}
			}
		}
	}

	.navbar {
		padding: 30rpx 0;
		text-align: center;
		background: #fff;
		color: #1E9F6A;
		bottom: 0;
		margin: 0 auto;
		left: 0;
		position: fixed;
		width: 100%;
		// margin-top: 144rpx;

	}
</style>
