<template>
	<view>
		<view class="headerGetbg"></view>
		<view class="home-content">
			<form @submit="save">
				<view class="home-content-top">

					<view class="br box-pack-between" style="padding: 32rpx 0 25rpx 0;">
						<text>语音信息</text>
						<view class="flex-y-center" v-if="id">
							<view @click="del(info.id)" class="flex-y-center ft28" style="color: #999999;">
								<view class="iconfont iconshanchu list-icon"></view>
								<view class="ft30">删除</view>
							</view>
						</view>
					</view>

					<view>
						<view class="box-pack-between ft28 item">
							<text>语音名称<text class="red">*</text></text>
							<view class="flex-center ft24">
								<input type="text" :value="info.name" placeholder="请填写语音名称" name="name" />
							</view>
						</view>
					</view>
					<view class="box-pack-between ft28 item">
						<view class="flex-y-center">首页播报<text class="red">*</text></view>
						<view class="flex-y-center">
							<view class="desc-content" v-for="(item,index) of zt_list" :key='index'>
								<view class="checklist-box" @click="choicezt(index)" :class="is_zt==index?'actt':''">
									<view class="radio__inner" :class="is_zt==index?'act_border':''">
										<view :class="is_zt==index?'is-default-checked':''"
											class="radio__inner-icon radio__inner-icon ">
										</view>
									</view>
									<text class="checklist-text">{{item}}</text>
								</view>
							</view>
						</view>
					</view>
					<view>
						<view class="box-pack-between ft28 item">
							<text>排序</text>
							<view class="flex-center ft24">
								<input type="number" :value="info.num" placeholder="请填写排序" name="num" />
							</view>
						</view>
					</view>
					<view class="" style="padding-bottom: 20rpx;">
						<view class="box-pack-between ft28 item">
							<text>添加语音<text class="red">*</text> </text>
							<view class="add-home flex-center ft24" @click="Uploads">
								上传
							</view>
						</view>
						<view class="item-bg" v-if="arr.length>0">
							<view @click="bindfile(index)" class="box-pack-between mar64 flex-y-center" v-for="(item,index) of arr" :key='index'>
								<view class="flex-y-center flex-grow-1 pdr-20">
									{{item.name}}
								</view>
								<view class="iconfont iconshanchu1 item-del flex-center" @click.stop="delScene"
									:data-index='index'></view>
							</view>
						</view>
					</view>

				</view>
				<view class="hg200rpx"></view>
				<view class="bottom-bar">
					<button form-type="submit" data-status="2" class="btn1 flex-center">
						提交
					</button>
				</view>
			</form>
		</view>

		<uni-popup ref="popup" type="center">
			<view class="home">
				<view class="home-top box-pack-between">
					<view class="flex-y-center">
						<view class="rund flex-center" @click="choiceAll(index)">
							<icon type="success_no_circle" size="12" v-if="checkedAll" />
						</view>
						<text class="ft28">全选</text>
					</view>

					<view class="flex-y-center">
						<view class="btn flex-center" @click="Uploads">
							上传素材
						</view>
						<view class="btn flex-center" style="background: #007AFF;" @click="config">
							确定选中素材
						</view>
					</view>
				</view>

				<scroll-view scroll-y="true" style="height: 630rpx;">
					<view class="item-li flex-y-center" v-for="(item,index) of material_list" :key='index'>
						<view class="rund flex-center" @click="choice(index)">
							<icon type="success_no_circle" size="12" v-if="item.checked" />
						</view>
						<text @click="bindfile(index)">{{item.file_name}}</text>
					</view>
				</scroll-view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkedAll: false,
				arr: [],
				material_list: [],
				id: "",
				info: {},
				is_video: 1,
				URL: getApp().globalData.URL,
				zt_list: ["关闭", "开启"],
				is_zt: 0,
			}
		},
		onLoad(e) {
			if (e.id) {
				this.id = e.id;
				this.detail(e.id);
				uni.setNavigationBarTitle({
					title: "编辑语音"
				})
			}
			// this.material();
		},


		onShow() {
			var pages = getCurrentPages();
			var currPage = pages[pages.length - 1],
				is_video = currPage.data.is_video;
			console.log(is_video)
			if (is_video == 2) {
				currPage.setData({
					is_video: 1,
				})
				// this.material();
			}
		},


		methods: {
			
			choicezt(e) {
				this.is_zt = e;
			},
			
			bindfile(index) {
				uni.setStorageSync("audio-info", this.arr[index])
				uni.navigateTo({
					url: "./detail"
				})
			},

			choiceAll() {
				if (this.checkedAll) {
					this.material_list.forEach((v, i) => {
						v.checked = false;
					})
					this.checkedAll = false
				} else {
					this.material_list.forEach((v, i) => {
						v.checked = true;
					})
					this.checkedAll = true
				}
			},

			choice(index) {
				this.material_list[index].checked = !this.material_list[index].checked;
				let allArr = getData(this.material_list);
				this.checkedAll = this.material_list.length === allArr.length ? true : false;

				function getData(col) {
					return col
						.filter(item => item.checked)
						.map(item => item.id);
				}
			},

			Uploads() {
				let that = this;
				var host = this.URL + '/index.php?s=/api/';
				uni.chooseMessageFile({
					count: 5,
					type: 'file',
					success: (res) => {
						console.log(res);
						for (let i of res.tempFiles) {
							filePath(i.path)
						}

						function filePath(url) {
							uni.showLoading();
							uni.uploadFile({
								url: host + "Smdcshop/voice_upload",
								filePath: url,
								name: 'file',
								formData: {},
								success: (r) => {
									console.log(r)
									let res = JSON.parse(r.data),
										arr = [];

									if (res.code === '200') {
										that.arr.push({
											src: res.data.src,
											name: res.data.name
										})
									}
								},
								fail: err => {
						 		console.log(err)
								},
								complete: res => {
									uni.hideLoading()
						 	}
							})
						}
					}
				});
			},

			config(e) {
				let list = getData(this.material_list);
				this.arr = [...this.arr, ...list];
				if (!this.arr.length) {
					uni.showToast({
						title: "请选择素材",
						icon: "none"
					})
					return
				}

				function getData(col) {
					return col
						.filter(item => item.checked)
						.map(item => item.file_path);
				}
				this.material_list.forEach((v, i) => {
					v.checked = false;
				})
				this.checkedAll = false
				this.$refs.popup.close();
			},
			detail(id) {
				this.request({
					url: "Smdcshop/voice_info",
					data: {
						admin_id: uni.getStorageSync("admin_id"),
						id,
					},
				}).then(res => {
					console.log(res, "res")
					if (res.code === '200') {
						this.arr = res.data.content1;
						this.info = res.data;
						this.is_zt = res.data.status;
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},

			delScene(e) {
				var index = e.currentTarget.dataset.index;
				this.arr.splice(index, 1)
			},

			del(id) {
				uni.showModal({
					title: '提示',
					content: '是否要删除？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.request({
								url: 'Smdcshop/voice_delect',
								data: {
									id: id,
								},
							}).then(res => {
								console.log(res, "res")
								if (res.code === 200 || res.code === "200") {
									uni.showToast({
										title: "删除成功",
										icon: "none",
									})
									setTimeout(() => {
										uni.navigateBack()
									}, 2e3)
								}
							}).catch(() => {
								uni.showToast({
									title: res.message,
									icon: "none",
								})
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},

			open() {
				this.$refs.popup.open();
			},

			save(e) {
				let arr1 = [],
					arr2 = [],
					url = "Smdcshop/voice_add";
				for (let i of this.arr) {
					arr1.push(i.name)
					arr2.push(i.src)
				}

				[{
					"src": "/uploads/cert/admin_thumb/20211109/22592add3565022b1aefa8f2f523505a.mp3",
					"name": "HRq5OiFu8OhSf49d165a0aa9c5dba64bea57813888b5.mp3"
				}, {
					"src": "/uploads/cert/admin_thumb/20211109/018e73356356ea52a6167dd0807e6350.mp3",
					"name": "BUxNW12Mv1waea0b9007242f36525dd1db964e83aec3.mp3"
				}]
				
				let data = {
					name: e.detail.value.name,
					content: JSON.stringify(this.arr),
					// 'content[src]':arr2.join(","),
					// 'content[name]':arr1.join(","),
					status: this.is_zt,
					num: e.detail.value.num,
					admin_id: uni.getStorageSync("admin_id"),
				};
				if (this.id) {
					data.id = this.id;
					url = "Smdcshop/voice_update"
				};
				if (!data.name || this.arr.length <= 0) {
					uni.showToast({
						title: "必填项不能为空",
						icon: 'none'
					})
					return false
				}
				this.request({
					url: url,
					data,
				}).then(res => {
					console.log(res, "res")
					if (res.code === '200') {
						console.log(res)
						uni.showToast({
							title: "提交成功",
							icon: 'none',
							duration: 1000
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
					}
				}).catch((err) => {
					console.log(err)
					uni.showToast({
						title: err.message,
						icon: 'none',
						duration: 2000
					})
				})
			},


		}
	}
</script>

<style lang="scss">
	page {
		background: #F1F1F1;
	}

	.red {
		color: red;
	}

	.item-del {
		color: red;
		font-size: 35rpx;
	}

	.hg200rpx {
		height: 200rpx;
	}

	.list-icon {
		width: 29rpx;
		height: 29rpx;
		margin-right: 6rpx;
	}

	.home-content {
		width: 690rpx;
		opacity: 1;
		border-radius: 14rpx;
		box-shadow: 0rpx 8rpx 16rpx 0rpx rgba(204, 215, 230, 0.08);
		margin: 0 auto;
		position: relative;

		.home-content-top {
			width: 100%;
			opacity: 1;
			background: #ffffff;
			border-radius: 14rpx;
			box-shadow: 0rpx 8rpx 16rpx 0rpx rgba(204, 215, 230, 0.08);
			padding: 0 24rpx;
			margin-top: 24rpx;
		}
	}

	.col-num {
		color: #007AFF;
	}

	.mar64 {
		margin-bottom: 64rpx;
	}

	.item-bg .mar64:last-child {
		margin-bottom: 0rpx;
	}

	.item_name {
		-webkit-box-flex: 0;
		-webkit-flex: none;
		flex: none;
		width: 176rpx;
	}

	.content {
		word-wrap: break-word;
		word-break: break-all;
	}

	.pdr-20 {
		padding-right: 20rpx;
		word-break: break-all;
	}

	.item {
		padding: 32rpx 0rpx;
	}

	.item input {
		text-align: right;
	}

	.upimg {
		width: 126rpx;
		height: 126rpx;
	}

	.add-home {
		color: #FFFFFF;
		width: 126rpx;
		height: 54rpx;
		background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
		border-radius: 8rpx;
		box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
	}

	.item-bg {
		// width: 670rpx;
		background: #f6f8fe;
		border-radius: 12rpx;
		margin: 0 auto;
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #333333;
		padding: 36rpx 32rpx;
	}

	.item-del {
		width: 40rpx;
		height: 40rpx;
		min-width: 40rpx;
	}

	.item-bg-32 {
		margin-right: 32rpx;
	}

	.bottom-bar {
		width: 100%;
		background: #fff;
		margin-bottom: 0rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		background: none;
	}

	.btn1 {
		width: 670rpx;
		height: 98rpx;
		background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
		border-radius: 8rpx;
		box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
		opacity: 1;
		font-size: 32rpx;
		color: #FFFFFF;
		margin: 0 auto;
		margin-bottom: 32rpx;
	}

	.home {
		width: 690rpx;
		height: 800rpx;
		background: #FFFFFF;
		padding: 20rpx;
		border-radius: 6rpx;

		.home-top {
			padding-bottom: 20rpx;

			.btn {
				height: 60rpx;
				padding: 0 20rpx;
				font-size: 24rpx;
				background: #009688;
				color: #FFFFFF;
				margin-right: 20rpx;
				border-radius: 6rpx;
			}
		}

		.item-li {
			padding: 10rpx 0;
			word-break: break-all;
			font-size: 28rpx;
			border-bottom: 1rpx solid #F1F1F1;

		}

		.rund {
			height: 40rpx;
			width: 40rpx;
			margin-right: 10rpx;
			border: 1rpx solid #ccc;
		}
	}

	.checklist-box {

		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-webkit-flex-direction: row;
		flex-direction: row;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		margin: 5px 0;
		margin-right: 22px;
	}

	.radio__inner {
		-webkit-flex-shrink: 0;
		flex-shrink: 0;

		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-pack: center;
		-webkit-justify-content: center;
		justify-content: center;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		position: relative;
		border: 1px solid #DCDFE6;
		border-radius: 2px;
		box-sizing: border-box;
		width: 16px;
		height: 16px;
		border-radius: 16px;
		background-color: #fff;
		z-index: 1;
		-webkit-transition: border-color .3s;
		transition: border-color .3s;
	}

	.radio__inner-icon {
		width: 8px;
		height: 8px;
		border-radius: 10px;
		opacity: 0;
		-webkit-transition: -webkit-transform .3s;
		transition: -webkit-transform .3s;
		transition: transform .3s;
		transition: transform .3s, -webkit-transform .3s;
	}

	.is-default-checked {
		opacity: 1;
		background-color: #007aff;
		-webkit-transform: rotate(45deg) scaleY(1);
		transform: rotate(45deg) scaleY(1);
	}

	.checklist-text {
		font-size: 28rpx;
		margin-left: 5px;
		-webkit-transition: color 0.2s;
		transition: color 0.2s;
		color: #000;
	}

	page .act_border {
		border: 1px solid #007AFF;
	}
</style>
