<template>
	<view class="content">
		<u-sticky>
			<view class="tabs">
				<u-tabs :scrollable="false" :list="tabs" :current="current" lineColor="#FF5C03" @change="tabChange">
				</u-tabs>
			</view>
		</u-sticky>
		<mescroll-body :bottom="bottom+'px'" ref="mescrollRef" :height="height+'px'" @init="mescrollInit"
			:down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
			<view class="list">
				<view class="list-item" v-for="item in dataList" :key="item.id">
					<view class="list-top ellipsis2" @click="navTo('./voiceItem?id='+item.id)">{{item.voiceTxt}}</view>
					<view class="list-cont x-bc">
						<text class="duration">{{secondsToHoursAndMinutes(item.duration || 0)}}</text>
						<view class="btns" v-show="current==0" @click="navTo('./voiceItem?type=2&id='+item.id)">录制</view>
						<view class="btns" v-show="current==1" @click="navTo('./voiceItem?id='+item.id)">录制</view>
						<view class="btns" v-show="current==2" @click="navTo('./voiceItem?id='+item.id)">详情</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<view class="footer" id="footer-btn" v-show="current==1&&dataList&&dataList.length>0">
			<button class="footer-btn" @click="handleAllVoice">一键生成所有录制</button>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {
		querySopVoiceList,
		createUserAllVoice
	} from '@/api/manageCompany'
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				current: 0,
				tabs: [
					{
						id: 2,
						name: '声音录制'
					}, {
						id: 0,
						name: '未完成'
					},
					{
						id: 1,
						name: '已完成'
					}
				],
				mescroll: null,
				downOption: { //下拉刷新
					use: true,
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll: false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore: "已经到底了",
					empty: {
						icon: 'https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png',
						tip: '暂无数据'
					}
				},
				dataList: [],
				height: 0,
				bottom: 0
			}
		},
		onLoad() {
			uni.$on('refreshVoiceList', () => {
				this.mescroll.resetUpScroll()
			})
		},
		onUnload() {
			uni.$off('refreshVoiceList')
		},
		methods: {
			getHeight() {
				this.$nextTick(() => {
					const query = uni.createSelectorQuery().in(this);
					query
						.select("#footer-btn")
						.boundingClientRect((data) => {
							if (data) {
								this.height = uni.getSystemInfoSync().windowHeight - data.height - 44
								this.bottom = data.height
							}
						})
						.exec();
				})
			},
			secondsToHoursAndMinutes(totalSeconds) {
				let hours = Math.floor(totalSeconds / 3600);
				let minutes = Math.floor((totalSeconds % 3600) / 60);
				let seconds = totalSeconds % 60;
				hours = hours.toString().padStart(2, '0');
				minutes = minutes.toString().padStart(2, '0');
				seconds = seconds.toString().padStart(2, '0');
				return hours + ':' + minutes + ':' + seconds;
			},
			tabChange(item) {
				if (this.current == item.index) return
				this.current = item.index
				this.mescroll.resetUpScroll()
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调*/
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					recordType: this.tabs[this.current].id,
					pageNum: page.num,
					pageSize: page.size
				};
				querySopVoiceList(data).then(res => {
					if (res.code == 200) {
						if (page.num == 1) {
							that.dataList = res.rows;
							that.getHeight()
						} else {
							that.dataList = that.dataList.concat(res.rows);

						}
						that.mescroll.endBySize(res.rows.length, res.total);

					} else {
						uni.showToast({
							icon: 'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			},
			handleAllVoice() {
				createUserAllVoice().then(res => {
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 2000
					});
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@mixin u-flex($flexD, $alignI, $justifyC) {
		display: flex;
		flex-direction: $flexD;
		align-items: $alignI;
		justify-content: $justifyC;
	}

	.content {
		.tabs {
			background-color: #fff;
		}

		.list {
			width:100%;
			padding: 15rpx;
			@include u-flex(column, flex-start, flex-start);

			.list-item {
				margin-bottom: 15rpx;
				width:100%;
				padding: 15rpx;
				box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.05);
				background-color: #fff;
				border-radius: 15rpx;
				@include u-flex(column, flex-start, flex-start);
			}

			.list-cont {
				margin-top: 24rpx;
				width: 100%;
			}
		}

		.duration {
			box-sizing: border-box;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #757575;
		}

		.btns {
			padding: 10rpx 30rpx;
			border: 1rpx solid #FF5C03;
			color: #FF5C03;
			font-size: 28rpx;
			border-radius: 30rpx;
		}
	}

	.footer {
		width: 100%;
		height: 152rpx;
		padding: 20rpx 24rpx;
		box-sizing: border-box;
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;

		&-btn {
			height: 112rpx;
			background: #FF5C03;
			border-radius: 112rpx;
			font-weight: 600;
			font-size: 34rpx;
			color: #FFFFFF;
			line-height: 112rpx;
			text-align: center;

			&::after {
				border: none;
			}
		}
	}
</style>