<template>
	<view style="position: relative;">
		<!-- 胶囊 -->
		<mvBar :mysNavConfig="mysNavConfig"></mvBar>
		<view :style="{marginTop:navH}"></view>
		<view>
			<view style="margin: 40rpx;font-size: 32rpx">
				<p style="margin-bottom: 20rpx;">上传链接</p>
				<textarea v-model="link_url" style="height: 50px;width: 100%;"
					placeholder="请上传正确链接~" />
			</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>
				<p style="height: 50px;font-size: 32rpx;width: 100%;color: #808090;">上传作业数据截图（直播任务请上传销量截图）
				</p>
			</view>

			<view class="signup">
				<button type="default" class="but1" @click="submit()">
					提交作业</button>
			</view>
		</view>

		<!-- 		<view v-if="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,
				navH: getApp().globalData.navHeight,
				taskId: "",
				link_url: "",
				imageData: [],
				assignmentStatus: "",
				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.assignmentStatus = data.assignmentStatus
		},
		methods: {
			submit() {
				// 校验
				if (this.link_url === "") {
					uni.showToast({
						title: '请输入正确的发布链接',
						icon: 'none'
					})
					return
				}
				// 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.addAssignmentApi()
							uni.navigateBack()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			addAssignmentApi() {
				return this.$https.post('/youngee/c/api/t/assignment/add', {
						task_id: this.taskId,
						link_url: this.link_url,
						photo_url: this.imageData[0],
					})
					.then(res => {})
			},

			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: '上传失败',
							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>