<template>
	<div class="app-container">
		<el-row type="flex" justify="space-between" style="margin-bottom:20px">
			<div class="grid-content bg-purple">
				<div class="grid-content bg-purple">
					<el-input placeholder="请输入承兑人名称" v-model="search" @change="Search" clearable style="width: 100%;">
						<i slot="prefix" class="el-input__icon el-icon-search"></i>
					</el-input>
				</div>
			</div>
			<!-- <el-col :xs="24" :sm="7" :md="6" :lg="4" :xl="3"> -->
			<div class="grid-content bg-purple">
				<el-dropdown @command="handleCommand">
					<div class="pulldown">
						<span class="el-dropdown-link" v-if="!status">
							承兑人状态
						</span>
						<span class="el-dropdown-link" v-if="status">
							{{status}}
						</span>
						<i class="el-icon-arrow-down el-icon--right"></i>
					</div>
					<el-dropdown-menu slot="dropdown">
						<el-dropdown-item command="承兑人状态">承兑人状态</el-dropdown-item>
						<el-dropdown-item command="正常">正常</el-dropdown-item>
						<el-dropdown-item command="删除">删除</el-dropdown-item>
					</el-dropdown-menu>
				</el-dropdown>
			</div>
			<!-- </el-col> -->

			<!-- <el-col :xs="24" :sm="7" :md="6" :lg="8" :xl="16"> -->
			<div class="grid-content bg-purple" style="display: flex;justify-content: flex-end;">
				<el-button type="primary" @click="addAcceptor">新增承兑人</el-button>
				<el-button type="primary" @click="Exports">批量导出</el-button>

			</div>
			<!-- </el-col> -->

		</el-row>
		<el-table v-loading="listLoading" :data="temporaryList" element-loading-text="Loading" border fit
			highlight-current-row @select="handleSelectionChange" @select-all="handleAll">
			<el-table-column align="center" label="" width="55" type="selection">

			</el-table-column>
			<el-table-column label="承兑人名称" align="center" prop="name">

			</el-table-column>
			<el-table-column label="账号" align="center" prop="bankAccount">

			</el-table-column>
			<el-table-column align="center" label="开户行名称" prop="bankName">

			</el-table-column>
			<el-table-column align="center" label="服务费率">
				<template slot-scope="scope">
					{{scope.row.serviceRate}}%
				</template>
			</el-table-column>
			<el-table-column align="center" label="利率" prop="interest">
				<template slot-scope="scope">
					{{scope.row.interest}}%
				</template>
			</el-table-column>
			<el-table-column align="center" label="状态" prop="statusText">

			</el-table-column>
			<el-table-column align="center" label="时间" prop="createdAt">

			</el-table-column>
			<el-table-column align="center" prop="created_at" label="操作" width="300">
				<template slot-scope="scope">
					<el-tag type="primary" style="cursor: pointer;margin-right: 15px;" @click="toDetail(scope.row.id)">
						详情</el-tag>
					<el-tag type="success" style="cursor: pointer;margin-right: 15px;" @click="recover(scope.row.id)" v-if="scope.row.status==1">恢复
					</el-tag>
					<el-tag type="danger" style="cursor: pointer;margin-right: 15px;"
						@click="deleteStatus(scope.row.id)" v-if="scope.row.status==0">删除</el-tag>

				</template>
			</el-table-column>
		</el-table>
		<div class="pagesip">
			<el-button type="primary" size="small"
				style="background-color:#D8AB5A ;border-color:#D8AB5A ; margin: 0 20px;" @click="jumpFirstPage">首页
			</el-button>
			<el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next"
				:total="list.length" :page-size="10" prev-text="上一页" next-text="下一页" :hide-on-single-page="false"
				ref="pagination">
			</el-pagination>
			<el-button type="primary" size="small"
				style="background-color:#D8AB5A ;border-color:#D8AB5A ;margin: 0 20px;" @click="jumpLastPage">尾页
			</el-button>
		</div>
	</div>
</template>

<script>
	import {
		getList
	} from '@/api/table'
	import {
		dataConversionUtil
	} from '../../utils/Excel.js'
	export default {
		filters: {
			statusFilter(status) {
				const statusMap = {
					published: 'success',
					draft: 'gray',
					deleted: 'danger'
				}
				return statusMap[status]
			}
		},
		data() {
			return {
				srcList: [],
				list: [],
				listLoading: false,
				status: '',
				search: '',
				temporaryList: [],
				currentPage: 1,
				pagesize: 11,
				multipleSelection: [],
				hpstatus: '',
				checked: false,
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
				value1: '',
				value2: ''
			}
		},
		created() {},
		mounted() {
			this.getAllList()
		},
		methods: {
			getAllList(){
				this.$request({
					url: '/api/Acceptor/search',
					method: 'post',
					data: {
						pageIndex: 1,
						pageSize: 100,
					}
				}).then((res) => {
					console.log(res.data.result);
					this.list = res.data.result;
					this.getPageData();
				})
			},
			handleCommand(command) {
				this.status = command
				if (this.status == "正常" || this.status == "删除") {
					this.temporaryList = this.srcList.filter(item => item.statusText == this.status)
				} else {
					this.temporaryList = this.srcList
				}
				console.log(this.srcList, "command")
			},
			handlehpCommand(command) {
				this.hpstatus = command
			},
			handleSelectionChange(data) {
				this.multipleSelection = data
				console.log(this.multipleSelection)
			},
			handleAll(data){
				this.multipleSelection=data
				console.log(this.multipleSelection)
			},
			Search() {
				if (this.search == "") {
					this.temporaryList = this.srcList
				} else {
					this.temporaryList = this.list.filter(item => item.name == this.search)
				}
			},
			addAcceptor() {
				this.$router.push({
						path: '/table/addAcceptor'
					})
			},
			toDetail(id) {
				this.$router.push({
					path: 'table/detail',
					query: {
						id: id
					}
				})
			},
			recover(id) {
				this.$request({
					url: '/api/Acceptor/manager',
					method: 'post',
					data: {
						ids:[id],
						operateType: 2
					}
				}).then((res) => {
					if(res.code==200){
						this.$message({
							type:'success',
							message:'恢复成功'
						})
						this.getAllList()
					}else{
						this.$message({
							type:'danger',
							message:'恢复失败'
						})
						this.getAllList()
					}
				})
			},
			deleteStatus(id) {
				this.$request({
					url: '/api/Acceptor/manager',
					method: 'post',
					data: {
						ids:[id],
						operateType: 1
					}
				}).then((res) => {
					if(res.code==200){
						this.$message({
							type:'success',
							message:'删除成功'
						})
						this.getAllList()
					}else{
						this.$message({
							type:'danger',
							message:'删除失败'
						})
						this.getAllList()
					}
				})
			},

			handleCurrentChange(val) {
				this.currentPage = val;
				this.getPageData();
			},
			jumpFirstPage() {
				this.$refs.pagination.handleCurrentChange(1);
				this.$emit('handleCurrentChange', 1);
			},
			jumpLastPage() {
				let font = this.$refs.pagination
				let cpage = Math.ceil(font.total / font.pageSize);
				font.handleCurrentChange(cpage);
			},
			getPageData() {
				let start = (this.currentPage - 1) * this.pagesize;
				let end = start + this.pagesize;
				this.srcList = this.list.slice(start, end);
				this.temporaryList = this.srcList
			},
			Exports() {
				if(this.multipleSelection==""){
					this.$message({
						type:'warning',
						message:'请选择数据!'
					})
				}else{
					var tableHeader = [
						['序号','承兑人名称', '账号', '开户行名称', '服务费率(%)', '利率(%)', '状态', '时间'],
					]
					var dataList = []
					this.multipleSelection.forEach((item, index) => {
						dataList.push([index + 1, item.name, item.bankAccount, item.bankName, item.serviceRate, item
							.interest, item.statusText, item.createdAt
						]);
					})
					dataConversionUtil.dataToExcel('承兑人列表', tableHeader, dataList)
					this.$message.success('导出成功!');
				}	
			}
		},
	}
</script>
<style lang="scss">
	.pulldown {
		width: 185px;
		height: 40px;
		border: 1px solid #E8E8E8;
		border-radius: 10px;
		display: flex;
		justify-content: center;
		align-items: center;
		justify-content: space-between;
		padding: 0 20px;
		color: #999999;
	}

	.pagesip {
		width: 100%;
		display: flex;
		justify-content: flex-end;
	}

	.el-pagination.is-background .el-pager li:not(.disabled).active {
		background-color: #D8AB5A;
	}

	.el-col {
		margin-bottom: 20px;
	}
</style>
