<template>
	<view class="order-pages">

		<view @tap.stop="handlerOnCahnger" class="pd-lg">H5 录音</view>
		<view @tap.stop="handlerOnSave" class="pd-lg">保存录音文件</view>
		<view>{{recorder}}</view>
		---------------------------------------------------------------------------------------------------
		<view v-if="recorder && recorder.data">
			<view>data.data ---- {{recorder.data}}</view>
			<view>data.type---------{{recorder.data.type || '------'}}</view>
		</view>

		<mumu-recorder ref='recorder' @success='handlerSuccess' @error='handlerError'></mumu-recorder>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import MumuRecorder from '@/uni_modules/mumu-recorder/components/mumu-recorder/mumu-recorder.vue'
	export default {
		components: {
			MumuRecorder
		},
		data() {
			return {
				options: {},
				recorder: null,
				recorder_status: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.options = options
			let {
				tab = 0
			} = options
			this.activeIndex = tab
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		methods: {
			...mapActions(['getConfigInfo', 'getCoachInfo', 'toPlayAudio']),
			...mapMutations(['updateUserItem']),
			async initIndex(refresh = false) {
				// #ifdef H5
				if (!refresh && this.$jweixin.isWechat()) {
					await this.$jweixin.initJssdk();
					this.$jweixin.wxReady(() => {
						this.$jweixin.hideOptionMenu()
					})
				}
				// #endif

				let data = {
					"id": 824,
					"uniacid": 666,
					"openid": "oDG4342lxj6wUH73W7pKLzTTvL6w",
					"nickName": "蕭萧",
					"avatarUrl": "https:\/\/lbqnyv3.migugu.com\/image\/666\/23\/04\/ef3f58f161d24baca189988d6a874dff.jpeg",
					"create_time": 1673263802,
					"status": 1,
					"cap_id": 0,
					"city": "",
					"country": "",
					"gender": 0,
					"language": "",
					"province": "",
					"balance": 94820.65,
					"phone": "18284514093",
					"session_key": "1Q5sSHtzYlhUFD1Y9eFDWw==",
					"pid": 824,
					"cash": 265.9,
					"unionid": "o3fbU0SmMbQLD4BKPyxcoD1KhAko",
					"app_openid": "oOxoV6NEt8Zl3P6lA7JWLH7NxMNc",
					"web_openid": "oFcA90U0Dy1YtuX91vppB5_LYgio",
					"wechat_openid": "oDG4342lxj6wUH73W7pKLzTTvL6w",
					"last_login_type": 0,
					"new_cash": "230.65",
					"lock": 153,
					"is_fx": 1,
					"ios_openid": "0",
					"push_id": "",
					"alipay_number": "18284514093",
					"alipay_name": "",
					"ip": "222.211.238.65"
				}
				let autograph = "f99e8e0daa9fb6d3575e1a32a4b27c57"

				this.updateUserItem({
					key: 'isShowLogin',
					val: false
				})
				this.updateUserItem({
					key: 'userInfo',
					val: data
				})
				this.updateUserItem({
					key: 'autograph',
					val: autograph
				})
			},
			handlerOnCahnger() {
				if (this.recorder_status) {
					this.$refs.recorder.stop()
				} else {
					this.$refs.recorder.start()
				}
				this.recorder_status = !this.recorder_status
			},
			async handlerOnSave() {
				console.log("======handlerOnSave")
				let {
					localUrl = ''
				} = this.recorder
				let unix = parseInt(new Date().getTime() / 1000)
				let name = `服务过程录音-${unix}.mp3`

				alert(unix)
				alert(name)
				
				let {
					attachment_path: link
				} = await this.$api.base.uploadFile({
					filePath: localUrl,
					formData: {
						type: 'audio',
						name
					}
				})
				alert(link)
				this.recorder.url = link
			},
			handlerSuccess(res) {
				console.log(res, "======handlerSuccess")
				this.recorder = res
			},
			handlerError(code) {
				switch (code) {
					case '101':
						uni.showModal({
							content: '当前浏览器版本较低，请更换浏览器使用，推荐在微信中打开。'
						})
						break;
					case '201':
						uni.showModal({
							content: '麦克风权限被拒绝，请刷新页面后授权麦克风权限。'
						})
						break
					default:
						uni.showModal({
							content: '未知错误，请刷新页面重试'
						})
						break
				}
			}
		}
	}
</script>


<style lang="scss">
</style>