<template>
	<u-popup :show="show" @close="cancel" :closeable="false">
		<view class="mainBox" style="width: 750rpx;">
			<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="文档列表" @clickLeft="cancel">

			</uni-nav-bar>
			<view class="topBtn">
				<u-button v-if="type!=='view'" type="primary" @click="add(1)" text="本地上传"></u-button>
				<u-button v-if="type!=='view'" @click="add" text="关联文档库"></u-button>
			</view>

			<u-cell-group style="min-height:500rpx">
				<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' @click="preview(item)"></u-button>
						<u-button v-if="type!=='view'" 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="cancel" text="返回"></u-button>
				<u-button v-if="type!=='view'" @click="save" text="保存"></u-button>
			</view>
			<fileEdit ref="fileEditRef" @success="done"></fileEdit>
			<selectDoc ref="selectDocRef" @success="done"></selectDoc>
		</view>
	</u-popup>
</template>

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

		},

		data() {
			return {
				tableList: [],
				fileId: [],
				type: '',
				show: false,

			}
		},

		methods: {

			open(fileId, type) {
				this.type = type
				this.fileId = fileId
				this.show = true
				this.init()
			},
			cancel() {
				this.fileId = []
				this.show = false;
				this.$emit('close')
			},
			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);
			},

			save() {
				this.$emit('getFiles', this.fileId)
				this.cancel()
			},

			add(type) {
				if (type == 1) {
					this.$refs.fileEditRef.open()
				} else {
					this.$refs.selectDocRef.open(1,this.fileId)
				}
			},
			done(id) {
				this.fileId.push(...id);
				this.init()
			},
			preview(row) {
				const apiInfo = uni.getStorageSync('apiInfo')

				const orgin =
					apiInfo.hostname &&
					`${apiInfo.protocal || ''}${apiInfo.hostname || ''}:${apiInfo.port || ''}`


				let file = row.storagePath[0];
				let fileNames = file.storePath.split('/')
				let url = orgin + '/api/main/file/getFile?objectName=' + file.storePath +
					'&fullfilename=' + fileNames[fileNames.length - 1]
				let previewUrl = orgin + '/kkfile/onlinePreview?url=' + btoa(url);



				uni.navigateTo({
					url: `/pages/doc/browse?url=${previewUrl}`
				})
			}

		}
	}
</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>