<template>
	<view style="position: relative;">
		<!-- 胶囊 -->
		<mvBar :mysNavConfig="mysNavConfig"></mvBar>
		<view :style="{marginTop:navH}"></view>
		<view @click="toRecord()">
			<view style="display: flex;justify-content: center;">
				<image style="height: 60rpx;width: 60rpx;" src="../../static/img/icon-clock.png"></image>
			</view>
			<view style="display: flex;justify-content: center;">
				<p style="font-size: 30rpx;">查看修改/反馈记录</p>
			</view>
		</view>
		<view v-if="!loading && !onlyShow">
			<!-- 多行输入框 -->
			<view style="margin: 40rpx;font-size: 32rpx">
				<p style="margin-bottom: 20rpx;">上传链接</p>
				<textarea v-model="link_url" style="height: 50px;width: 100%;"
					placeholder="请删除中文等其他字符，仅输入https://或http://开头的发布链接，样叽会对此进行质检~" />
			</view>

			<view style="margin: 0 40rpx;">
				<p style="margin-bottom: 20rpx;">上传作品收录/发布截图</p>
				<htz-image-upload :max="1" mediaType="image" name="file" :chooseNum="1" v-model="imageData"
					@chooseSuccess="ceshiChooseSuccess1">
				</htz-image-upload>
			</view>

			<view class="signup">
				<button type="default" class="but1" :loading="loading" @click="submit()">
					提交链接</button>
			</view>
		</view>

		<view v-if="!loading && onlyShow">
			<view style="margin: 20rpx;">
				<!-- 图片 -->
				<p>{{link_url}}</p>
				<view>
					<image :src="imageData" mode="scaleToFill"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mvBar from "@/components/mys_navBar/mysNavBar";
	import htzImageUpload from '@/components/htz-image-upload/htz-image-upload.vue';
	import getPolicyEncode from '@/components/obs/getPolicy.js';
	import getSignature from '@/components/obs/GetSignature.js';
	export default {
		components: {
			mvBar,
			htzImageUpload,
		},
		data() {
			return {
				onlyShow: false,
				loading: true,
				navH: getApp().globalData.navHeight,
				taskId: "",
				link_url: "",
				imageData: [],
				linkStatus: "",
				mysNavConfig: {
					/* 开启单页显示首页图标 */
					isHome: true,
					/* 固定导航  */
					navFixed: true,
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "上传链接",
						color: "",
						fontSize: "32rpx", // px upx rpx
						fontWeight: "normal", // 100 - 700
					},
					btnType: "type2",
					onLeftClick: '',
					/* type2 按钮 */
					type2Config: {
						// 左图标
						leftPath: "/static/img/png2.png",
						// 右图标
						rightPath: "/static/img/png4.png",
						// 圆角
						radius: "40rpx",
					},
				},
			}
		},

		onLoad(options) {
			let data = options.textObj.replace(/""/g, "");
			data = JSON.parse(decodeURIComponent(data))
			this.taskId = data.taskId
			this.linkStatus = data.linkStatus
			this.getLink()
		},
		methods: {
			async getLink() {
				this.loading = true
				uni.showLoading({
					title: '加载中'
				});
				await this.$https.get('/youngee/c/t/g/get-unsubmit-task-link' +
						"?" +
						"task_id" +
						"=" +
						this.taskId)
					.then(res => {
						console.log(res)
						if (res.data.data != null) {
							this.onlyShow = true
							this.imageData.push(res.data.data.photo_url)
							this.link_url = res.data.data.link_url
						}
					})
				uni.hideLoading();
				this.loading = false
			},
			toRecord() {
				var data = {
					taskId: this.taskId,
				};
				data = JSON.stringify(data)
				uni.navigateTo({
					url: '/page_workspace/record/linkrecord?textObj=' + encodeURIComponent(data)
				});
			},

			submit() {
				// 校验
				if (this.link_url == "" || (this.link_url.substring(0, 8) != "https://" && this.link_url.substring(0, 7) !=
						"http://")) {
					uni.showToast({
						title: '请输入正确的发布链接',
						icon: 'none'
					})
					return
				}
				if (this.imageData.length == 0) {
					uni.showToast({
						title: '请上传收录/发布截图',
						icon: 'none'
					})
					return
				}
				let that = this
				uni.showModal({
					title: '提示',
					content: '提交后无法修改并进行审核，确认提交？',
					success: async function(res) {
						if (res.confirm) {
							// 调用添加链接接口
							await that.$https.post('/youngee/c/t/p/add-task-link', {
									task_id: that.taskId,
									link_url: that.link_url,
									photo_url: that.imageData[0],
								})
								.then(res => {
									console.log(res)
								})
							await that.$https.get('/youngee/c/t/g/submit-task-link' +
									"?" +
									"task_id" +
									"=" +
									that.taskId)
								.then(res => {
									console.log(res)
								})
							uni.navigateBack()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			ceshiChooseSuccess1(tempFilePaths, e) { //选择图片返回
				console.log('ceshiChooseSuccess', tempFilePaths, e);
				if (e == 0) {
					this.store = 'talent_upload/' + this.guid() + '.png'
				} else if (e == 1) {
					this.store = 'talent_upload/' + this.guid() + '.mp4'
				}
				/****************
				以下代码是自定义上传逻辑，仅供参考
				***************/
				this.imgUpload1(tempFilePaths);
				/*******************************/
				console.log("imgdata" + this.imageData)
			},
			imgUpload1(tempFilePaths) {
				let that = this
				console.log('imgUpload', tempFilePaths)
				let config = {
					AccessKeyId: 'IVW21DTGIIUBBAGXKK0Y', //AK
					SecretKey: 'Y01nEQNcLOATMw7uJwrk3yOdQZ2fqLhSnXcOKVDE', //SK
					EndPoint: 'https://horastar.obs.cn-east-3.myhuaweicloud.com', //上传文件的路径
				};
				let fileName = this.store; //指定上传到OBS桶中的对象名
				// let fileName = "talent_upload/icon-arrow-right.png"
				let OBSPolicy = { //设定policy内容
					"expiration": "2089-12-31T12:00:00.000Z",
					"conditions": [{
							"bucket": "horastar"
						}, //Bucket name
						// {"bucket": "goin"}, 
						{
							'key': fileName
						}
					]
				}

				let policyEncoded = getPolicyEncode(OBSPolicy); //计算policy编码值
				let signature = getSignature(policyEncoded, config.SecretKey); //计算signature 
				uni.uploadFile({
					//url: config.EndPoint,
					url: config.EndPoint,
					filePath: tempFilePaths[0],
					name: 'file',

					formData: {
						'AccessKeyID': config.AccessKeyId,
						'policy': policyEncoded,
						'signature': signature,
						'key': fileName,
					},

					success: function(res) {
						console.log(res.statusCode); //打印响应状态码
						if (res.statusCode == '204') {
							that.imageData.push(config.EndPoint + '/' + fileName);
							console.log('上传图片成功', res)
							let obs_url = config.EndPoint + '/' + fileName; //用你自己的 bucket 名替换星号
							console.log(obs_url)
							// that.formData.home_img = obs_url
							uni.showToast({
								title: '上传成功',
								icon: '成功'
							});
						} else {
							console.log('上传图片失败', res)
							uni.showToast({
								title: '上传失败',
								icon: '失败'
							});
						}
					},
					fail: function(e) {
						console.log(e);
						uni.showToast({
							title: '上传失败22222',
							icon: '失败'
						});
					}
				})
			},
			guid() {
				return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
					var r = Math.random() * 16 | 0,
						v = c == 'x' ? r : (r & 0x3 | 0x8);
					return v.toString(16);
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.signup {
		box-shadow: 0rpx 5rpx 40rpx #ccc;
		width: 100%;
		position: fixed;
		bottom: 0rpx;
		display: flex;
		height: 90rpx;
		padding-top: 2%;
		background-color: #FFFFFF;
	}

	.but1 {
		width: 60%;
		background-color: #f2d22d;
		border-radius: 20rpx;
		font-size: 36rpx;
		line-height: 200%;
		letter-spacing: 10rpx;
		font-weight: 500;
		height: 80%;

		&.on {
			background-color: #C0C0C0;

		}

	}
</style>
