<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="文档列表" @clickLeft="back">

		</uni-nav-bar>
		<view class="topBtn">
			<u-button type="primary" @click="add" text="本地上传"></u-button>
			<u-button @click="selectDoc" text="关联文档库"></u-button>
		</view>

		<u-cell-group>
			<u-cell v-for="(item, index) in tableList" :key="index">
				<view class="listTitle" slot="title">
					{{item.name}}
				</view>

				<view slot="value" class="listBtn">

					<u-button text="浏览" size='mini'></u-button>
					<u-button text="删除" size='mini' @click="del(index)"></u-button>
				</view>
			</u-cell>
		</u-cell-group>

		<view style="height: 84rpx;"></view>
		<view class="footerButton">
			<u-button type="primary" @click="back" text="返回"></u-button>
			<u-button @click="save" text="保存"></u-button>
		</view>
		<fileEdit  ref="fileEditRef" @success="done"></fileEdit>
	</view>
</template>

<script>
	import {
		queryIds
	} from './api';
	import fileEdit from './file-edit';
	export default {
		components: {
			fileEdit
		},

		data() {
			return {
				tableList: [],
				fileId: []

			}
		},
		onLoad({
			fileId
		}) {
			if (fileId) {
				this.fileId = JSON.parse(fileId)
			}
			this.init()
			uni.$off('setDocList')
			uni.$on('setDocList', (id) => {
				this.fileId.push(...id);
				this.init()
			})
		},
		methods: {
			async init() {
				if (this.fileId.length > 0) {
					this.tableList = await queryIds({
						ids: "'" + this.fileId + "'"
					});
				} else {
					this.tableList = [];
				}
			},
			del(index) {
				this.tableList.splice(index, 1);
				this.fileId = this.tableList.map((item) => item.id);
			},
			selectDoc() {
				uni.navigateTo({
					url: '/pages/doc/selectDoc?isAll=' + 1 + '&fileId=' + JSON.stringify(this.fileId)
				})
			},
			save() {
				uni.$emit('getFiles', this.fileId)
				this.back()
			},
			onunload() {
				uni.$off('setDocList')
			},
			add() {
				this.$refs.fileEditRef.open()
			},
			done(id){
				this.fileId.push(...id);
				this.init()
			}

		}
	}
</script>

<style lang="scss" scoped>
	.listTitle {
		width: 420rpx;
		white-space: nowrap;
		/* 防止文本换行 */
		overflow: hidden;
		/* 隐藏溢出的内容 */
		text-overflow: ellipsis;
		/* 显示省略符号来代表被修剪的文本 */
	}

	.topBtn {
		display: flex;
		margin-bottom: 20rpx;
		margin-top: 10rpx;

		uni-button {
			width: 180rpx;
			margin-left: 5px;
			margin-right: 5px;
		}
	}

	.listBtn {
		display: flex;

		uni-button {
			width: 100rpx;
			// height: 50rpx;
			margin-left: 10rpx;
			margin-right: 0;
			color: #fff !important;
			border: none;
			background: #157a2c !important;

		}
	}

	.footerButton {
		width: 100%;
		height: 84rpx;
		display: flex;
		position: fixed;
		bottom: 0;
		z-index: 10;

		/deep/.u-button {
			height: 100%;
		}

		>view {
			flex: 1;

		}
	}

	.item {
		display: flex;
		flex-direction: column;
		padding: 16rpx;

		>view {
			padding-top: 14rpx;
		}

		.item_title {
			font-size: 32rpx;
			font-weight: 800;
		}

		.item_list {
			display: flex;
			align-items: center;

			text {
				font-size: 28rpx;
			}

			.lable {
				color: #626060;
			}

			.value {
				color: #333;
				margin-left: 12rpx;
				flex: 1
			}
		}
	}

	/deep/.u-swipe-action-item__right__button__wrapper {
		background-color: #f56c6c !important;
	}

	/deep/.u-input {
		padding: 0 !important;
	}

	/deep/uni-textarea {
		padding: 0 !important;
	}
</style>