<template>
	<view class="curpage">
		<view class="list" v-for="item,index in list" :key="index">
			<view class="title">
				<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 class="icon">
				<text @click="goendit(item)">
					编辑
				</text>
				<text class="line"></text>
				<text class="dele" @click="dele(item.id)">
					删除
				</text>
			</view>
		</view>


		<view class="" style="height: 1rpx;margin-bottom: 144rpx;"></view>
		<view class="navbar" @click="add">
			+ 新增常用发票
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,

				id: 1,
				list: []
			}
		},
		onLoad() {
			this.init()
		},
		onShow() {
			let token = uni.getStorageSync("token")
			if (!token) {
				//未登录
				uni.showToast({
					title: "请先登录",
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					})
				}, 1500)

				return false
			}
			this.init()
		},
		methods: {
			init() {
				uni.$u.http.post('/api/invoice/list', {
					custom: {
					 auth: true
					}
				}).then((res) => {
					console.log(res)
					res.forEach(item => {
						if (item.type == 1) {
							item.type = "个人"
						} else if (item.type == 2) {
							item.type = "企业"
						}
					})
					this.list = res
				}).catch((err) => {
					console.log(err)
				})
			},
			// 编辑发票
			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 {
				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>
