<template>
	<view>
		<view class="headbgbox flex2">
			<view class="searchbox">
				<u-icon name="search" bold="true" size="25" color="rgb(246,185,78)"></u-icon>
				<input type="text" v-if="state==0" v-model="keywords" placeholder="请输入供应商名称" placeholder-style="color:#BBBCBE" @blur="changeSeach"/>
				<input type="text" v-if="state==1" v-model="keywords" placeholder="请输入承兑人名称" placeholder-style="color:#BBBCBE" @blur="changeSeach2"/>
			</view>
			<view class="screen" @click="show = true">
				<text>筛选</text>
				<view style="padding-left: 10rpx;">
					<u-icon name="arrow-down" color="#1F242A" size="14" bold="true"></u-icon>
				</view>
			</view>
		</view>
		<view class="acpcardlist">
			<view class="lable " v-for="(item,index) in listall" :key="index">
				<view class="radiobox" v-if="editall">
					<radio :value="index" :checked="item.checked" color="#D8AB5A" @click="checkBox(item.id)" />
				</view>
			
				<view class="acpcard" v-if="state==1" style="height: 330rpx;" >
					<view class="allmsg">
						<view class="acpcont">
							<view class="acpname">
								<text>{{item.acceptorName}}</text>
								<view class="tag" v-if="item.statusText=='待审核'">待审核</view>
								<view class="tagdel" v-if="item.statusText=='失败'">失败</view>
								<view class="tagdel2" v-if="item.statusText=='失败'" @click="replayAcceptor(item.acceptorId)">重新提交</view>
								<view class="tagpass" v-if="item.statusText=='成功'">成功</view>
							</view>
							<view class="acpinfo " v-if="item.statusText=='失败'">
								<!-- <text class="pt">账号：{{item.bankAccount}}</text> -->
								<text class="pt">服务费率：%</text>
								<text class="pt">利率：%</text>
							</view>
							<view class="acpinfo " v-if="item.statusText=='成功'">
								<!-- <text class="pt">账号：{{item.bankAccount}}</text> -->
								<text class="pt">服务费率：{{item.serviceRate}}%</text>
								<text class="pt">利率：{{item.interest}}%</text>
								<text class="pt" @click.stop="addpdf(item.templateServiceContractList)">点击获取服务合同信息</text>
							</view>
							<view class="acpinfo " v-if="item.statusText=='待审核'">
								<!-- <text class="pt">账号：{{item.bankAccount}}</text> -->
								<text class="pt">服务费率：%</text>
								<text class="pt">利率：%</text>
							</view>
						</view>
					</view>
				</view>
				<view class="acpcard" v-if="state==0" @click='acpDetail(item.id)'>
					<view class="allmsg">
						<view class="acpcont">
							<view class="acpname">
								<text>{{item.acceptorName}}</text>
								<view class="tag" v-if="item.statusText=='待审核'">待审核</view>
								<view class="tagdel" v-if="item.statusText=='失败'">失败</view>
								<view class="tagpass" v-if="item.statusText=='成功'">成功</view>
							</view>
							<view class="acpinfo ">
								<text class="pt">供应商名称：{{item.supplierName}}</text>
								<text class="pt">服务费率：{{item.serviceRate}}%</text>
								<text class="pt">利率：{{item.interest}}%</text>
								<text class="pt" @click.stop="addpdf(item.templateServiceContractList)">点击获取服务合同信息</text>
							</view>
						
						</view>
					</view>
				</view>
			</view>

		</view>
		<view class="nomore" v-if="listall.length==0">
			<text>暂无数据</text>
		</view>
		<view class="totalmanage " v-if="listall.length>0">
			<text style="color:#68625B;font-size: 28rpx;">合计：{{total}}份</text>
			<view class=" maboxtitls" @click="editall=true">
				<view class="managebox">
					<image src="/static/img/manage.png" mode="aspectFit"></image>
				</view>
				<text style="padding-left: 15rpx;">管理</text>
			</view>
		</view>
		<view class="safebt"></view>
		
		<view class="plusicon" v-if="state==1"   @click="addAcceptor"></view>
		<!-- 筛选功能 -->
		<u-popup  :show="show" @close="close" @open="open">
			<view class="popscreen" style="height: 340rpx;">
				<view class="checklist" v-for="(item,index) in ctrstatus" :key="index">
					<view class="alllistbtn">
						<text class="btntitle" style="">{{item.title}}</text>
						<view class="yunbtn">
							<view class="btnchoose" v-for="(i,k) in item.list" :key="k" @click="choose(item.title,k)"
								:class="[contract_status == k?'active':'btnchoose']">
								<text>{{i.name}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="aszs flex6">
					<view class="againbtn"  @click="restoreALL"><text>重置</text></view>
					<view class="againbtn" style="color: #D8AB5A;border: 1rpx solid #D8AB5A;" @click="makeSureSelect">
						<text>确认</text></view>
				</view>
			</view>
		</u-popup>
		<!-- 底部删除 -->
		<u-popup :show="editall" @close="closeEidt" @open="openEidt" :overlay="false">
			<view class="editbox">
				<view class="chooseall flex6">
					<view class="leftchoose ">
						<radio :checked="quan" @click="makeSureAll" color="#D8AB5A"></radio>
						<text>全选</text>
					</view>
					<view class="surebtn" @click="selectDown">
						完成
					</view>
				</view>
				<view class="choosebtn delet-btn">
					<view class="delbtn flex1" @click="deleteAcceptor">删除</view>
					<!-- <view class="restorebtn flex1" @click="backAcceptor">复原</view> -->
				</view>
				<view class="choosebtn" v-if="state==0">
					<view class="delbtn flex1" @click="examineBillFail()">审核失败</view>
					<view class="restorebtn flex1" @click="examineBillSuccess()">审核成功</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	let that
	export default {
		
		data() {
			return {
				state:'',//业务员为0,供应商为1
				supplierId:'',//供应商id
				status:'',//承兑人状态
				acceptorName:'',//承兑人名称
				supplierName:'',//供应商名称
				ids:[],//删除承兑人id数组
				keywords:'',
				editall: false,
				quan: false,
				show: false,
				contract_status: 0,
				listall: [],//页面渲染数据
				pageIndex: 1,
				total: '',
				nomore: false,
				statusall: 0,
				ctrstatus: [{
					title: "承兑人状态",
					list: [{
							name: "全部",

						},
						{
							name: "待审核",
						},
						{
							name: "成功",
						},
						{
							name:'失败'
						}

					]
				}],
			}
		},
		onLoad(o) {
			that = this
			this.state = this.$store.state.admin
			console.log('state',this.state);
			console.log('o',o);
			if(o.id){
				this.supplierId = o.id
			}
		},
		onShow() {
			that.pageIndex = 1
			that.nomore = false
			that.listall = []
			this.init()
		},
		// 下拉到底部后加载新数据
		// onReachBottom() {
		// 	//判断下一页是否存在数据，不存在将显示暂无数据等提示语
		// 	if (this.listall.length >= this.pageIndex * 10) {
		// 		this.pageIndex++; //页数加一
		// 		this.init(); //回调接口
		// 	} else {
		// 		this.nomore = true
		// 	}
		// },
		methods: {
			init() {
				uni.showLoading()
				uni.$u.http.get(`/api/SupplierAcceptor?pageSize=-1&supplierName=${this.supplierName}&status=${this.status}&acceptorName=${this.keywords}`, {
					supplierId:this.supplierId||'',
					
				}, {
					custom: {
						auth: true
					}
				}).then((res) => {
					uni.hideLoading()
					this.total = res.total
					this.listall=res.result
					// let list = res.result
					// if (list.length > 0) {
					// 	list.forEach(item => {
					// 		item.checked = false
					// 		this.listall.push(item)
					// 	})
					// 	if (list.length < 10) {
					// 		that.nomore = true
					// 	}
					// }
				}).catch((err) => {
					uni.hideLoading()
					console.log(err)
				})

			},
			
			//关闭筛选弹框
			close() {
				this.show = false
			},
			open() {

			},
			//搜索功能
			changeSeach(){
				this.supplierName=this.keywords
				that.pageIndex = 1
				that.nomore = false
				// that.listall = []
				this.init()
			},
			changeSeach2(){
				this.acceptorName=this.keywords
				that.pageIndex = 1
				that.nomore = false
				// that.listall = []
				this.init()
			},
			
			
			
			//承兑人状态查询
			makeSureSelect() {
				that.pageIndex = 1
				that.nomore = false
				that.listall = []
				this.init()
				this.close()
			},
			//承兑人状态查询
			choose(title, k) {
				if (title == "承兑人状态") {
					this.contract_status = k
					if (k == 1) {
						this.status = 0
					} else if (k == 2) {
						this.status = 1
					} else if (k == 0) {
						this.status = ''
					}else if(k==3){
						this.status=2
					}
				}
			},
			//筛选重置
			restoreALL() {
				this.contract_status = 0
				this.status = ''
				
				
			},
			/*底部管理弹框  */
			selectDown() {
				this.editall = false
				this.quan = false
				let list = this.listall
				list.forEach(item => {
					item.checked = false
				})
			},
			/* 部分选择 */
			checkBox(id) {
				let list = this.listall
				list.forEach(item => {
					if (item.id == id) {
						item.checked = !item.checked
					}
				})
				this.listall = JSON.parse(JSON.stringify(list))

			},
			/* 全选 */
			allSelect() {
				let list = this.listall
				if (this.quan) {
					list.forEach(item => {
						item.checked = true
					})
				} else {
					list.forEach(item => {
						item.checked = false
					})
				}
				this.listall = JSON.parse(JSON.stringify(list))
				console.log(this.listall);
				
			},
			makeSureAll() {
				this.quan = !this.quan
				this.allSelect()
			},
			closeEidt() {
				this.editall = false
			},
			openEidt() {
				this.editall = true
			},
			//承兑人绑定详情
			acpDetail(id) {
				uni.navigateTo({
					url: "/pages/alldeatil/acptsipplierdetail?id=" + id
				})
			},
			addAcceptor() {
				uni.navigateTo({
					url: '/pages/alldeatil/addacceptbind?id='+this.supplierId
				})
			},
			//删除承兑人
			deleteAcceptor() {
				let ids = []
				that.listall.forEach(item => {
					if (item.checked) {
						ids.push(item.id)
					}
				})
				console.log(ids);
				if (ids.length == 0) {
					that.$toast("请选择承兑人")
					return
				}
				uni.showModal({
					title: '提示',
					content: "是否批量删除？",
					success: function(res) {
						if (res.confirm) {
							uni.showLoading()
							uni.$u.http.post('/api/SupplierAcceptor/delete', [...ids], {
								custom: {
									auth: true
								}
							}).then((res) => {
								uni.hideLoading()
								that.editall = false
								that.quan = false
								that.$toast("操作成功")
								setTimeout(() => {
									that.pageIndex = 1
									that.nomore = false
									that.listall = []
									that.init()
								}, 500)

							}).catch((err) => {
								uni.hideLoading()
								that.$toast(err.msg)
							})

						} else if (res.cancel) {

						}
					}
				})
			},
			//审核失败后重新提交
			replayAcceptor(id){
				console.log('id',id);
				let that = this
				this.acceptorIds = [id]
				uni.showLoading()
				uni.$u.http.post('/api/SupplierAcceptor',{
					supplierId:that.supplierId,
					acceptorIds:that.acceptorIds
				}).then((res) => {
					uni.hideLoading()
					that.$toast("提交成功")
					setTimeout(()=>{
						
					that.init()
					},1000)
				})
			},
			
			//批量审核失败
			examineBillFail() {
				let ids = []
				let that = this
				this.listall.forEach(item => {
					if (item.checked) {
						ids.push(item.id)
					}
				})
				console.log(ids);
				if (ids.length == 0) {
					that.$toast("请选择承兑人")
					return
				}
				uni.showModal({
					title: '提示',
					content: '是否审核失败？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading()
							uni.$u.http.post('/api/SupplierAcceptor/update', {
								status: 2,
								ids:[...ids]
							}, {
								custom: {
									auth: true
								}
							}).then((res) => {
								uni.hideLoading()
								that.$toast("操作成功")
								that.init()
							}).catch((err) => {
								uni.hideLoading()
								that.$toast(err.msg)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
				},
			
			
			//批量审核成功
			examineBillSuccess() {
				let ids = []
				let that = this
				this.listall.forEach(item => {
					if (item.checked) {
						ids.push(item.id)
					}
				})
				console.log(ids);
				if (ids.length == 0) {
					that.$toast("请选择承兑人")
					return
				}
				uni.showModal({
					title: '提示',
					content: '是否审核成功？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading()
							uni.$u.http.post('/api/SupplierAcceptor/update', {
								status: 1,
								ids:[...ids]
							}, {
								custom: {
									auth: true
								}
							}).then((res) => {
								uni.hideLoading()
								that.$toast("操作成功")
								that.init()
							}).catch((err) => {
								uni.hideLoading()
								that.$toast(err.msg)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			addpdf(msg) {
				uni.showModal({
					title: '提示',
					content: "是否获取服务合同信息？",
					success: function(res) {
						msg.forEach(item=>{
							if(res.confirm){
								uni.setClipboardData({
									data: item,
									success: function() {
										that.$toast("复制成功，请到浏览器下载")
									}
								});
							}else if(res.cancel){
								console.log('取消');
							}
							
							
						})
					}
				})
			},
			
		}

	}
</script>

<style lang="scss">
	page {
		background-color: #F6F6F6 !important;
	}

	.headbgbox {
		background-color: #fff;
		padding: 23rpx 31rpx;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		z-index: 999;
		width: 100%;

		.screen {
			display: flex;
			justify-content: center;
			align-items: center;
			padding-left: 30rpx;
		}
	}

	.searchbox {
		padding-left: 30rpx;
		box-sizing: border-box;
		background-color: #ECECEC;
		border-radius: 38rpx;
		// width: 688rpx;
		flex: 1;
		height: 75rpx;
		margin: 0 auto;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.acpcardlist {
		margin-top: 140rpx;

		.acpcard {
			flex: 1;
			// width: 690rpx;
			height: 400rpx;
			margin: 0 30rpx;
			// margin-top: 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
			box-shadow: 0px 8rpx 20rpx 1rpx rgba(31, 36, 42, 0.08);

			.allmsg {
				display: flex;
				padding: 40rpx 30rpx;
				box-sizing: border-box;
				width: 100%;

				.cicle {
					width: 28rpx;
					height: 28rpx;
					border-radius: 50%;
					background-color: #D8AB5A;
					margin-top: 8rpx;
				}

				.acpcont {
					margin-left: 20rpx;
					width: 100%;

					.acpname {
						box-sizing: border-box;
						display: flex;
						align-items: center;

						padding-bottom: 30rpx;
						flex-grow: 1;
						width: 100%;
						// width: 572rpx;
						font-size: 34rpx;
						font-weight: 600;
						.tagdel2{
							margin-left: 10rpx;
							width: 100rpx;
							// height: 36rpx;
							border-radius: 10rpx;
							background-color: rgba(8, 102, 214,.3);
							font-size: 24rpx;
							font-weight: 400;
							color: rgb(8, 102, 214);
							line-height: 36rpx;
							text-align: center;
						}
						.tagdel {
							margin-left: 10rpx;
							width: 80rpx;
							// height: 36rpx;
							border-radius: 10rpx;
							background-color: rgb(241, 206, 195);
							font-size: 24rpx;
							font-weight: 400;
							color: rgb(213, 109, 77);
							line-height: 36rpx;
							text-align: center;
						}
						.tagpass{
							margin-left: 10rpx;
							width: 80rpx;
							height: 36rpx;
							border-radius: 10rpx;
							background-color: rgba(30, 190, 130,.5);
							font-size: 24rpx;
							font-weight: 400;
							color: rgb(0, 200, 83);
							line-height: 36rpx;
							text-align: center;
						}

						.tag {
							margin-left: 10rpx;
							width: 80rpx;
							height: 36rpx;
							border-radius: 10rpx;
							background-color: rgb(232, 222, 203);
							font-size: 24rpx;
							font-weight: 400;
							color: #D8AB5A;
							line-height: 36rpx;
							text-align: center;
						}
					}
				}

				.acpinfo {
					border-top: 1rpx solid #E8E8E8;
					display: flex;
					flex: 1;
					flex-direction: column;
					font-size: 28rpx;
					color: #BEBDBB;
					justify-content: space-between;
					box-sizing: border-box;

					.pt {
						padding-top: 20rpx;
					}
				}
			}
		}
	}

	.totalmanage {
		margin-top: 90rpx;
		margin-bottom: 100rpx;
		// position: fixed;
		bottom: 0;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;

		.managebox {
			width: 23.8rpx;
			height: 29.7rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.maboxtitls {
			width: 160rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 30rpx;
			background-color: #D9D9D9;
			// line-height: 60rpx;
			font-size: 28rpx;
			box-sizing: border-box;
			// align-items: center;
		}
	}

	.plusicon {
		position: fixed;
		left: 0;
		right: 0;
		margin: 0 auto;
		bottom: 110rpx;
		width: 150rpx;
		height: 150rpx;
		background: url("@/static/img/plusicon.png") no-repeat;
		background-size: 150rpx 150rpx;
	}
	.editbox{
		position: relative;
	}
	.delet-btn{
		position: absolute;
		top: 6px;
		left: 140px;
		.delbtn{
			width: 181rpx !important;
		}
	}
</style>
