<template>
	<view class="uni-padding-wrap">
		<page-head :title="title"></page-head>
    <view class="item-box">
      <text>当前应用隐私授权状态:</text>
      <text>{{ appPrivacy }}</text>
    </view>
    <!-- #ifdef MP-WEIXIN -->
    <view class="item-box">
      <text>隐私授权协议的名称:</text>
      <text>{{ privacyContractName }}</text>
    </view>
    <!-- #endif -->
    <view>
      <button class="privacy-button" type="primary" @tap="getPrivacySetting">
        获取隐私协议授权状态
      </button>
      <button class="privacy-button" type="primary" open-type="agreePrivacyAuthorization">
        同意隐私协议专用按钮
      </button>
      <!-- #ifdef APP -->
      <button class="privacy-button" type="primary" @tap="resetPrivacyAuthorization">
        重置隐私协议授权状态
      </button>
      <button class="privacy-button" @tap="openPrivacyDialog">
        显示隐私政策弹框
      </button>
      <!-- #endif -->
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			  title: '隐私信息授权',
			  appPrivacy: '未获取',
        privacyContractName: "",
        listenId: 0
			}
		},
    onReady() {
      // #ifdef APP
      //添加 隐私协议监听
      const id = uni.onPrivacyAuthorizationChange((res) => {
         this.appPrivacy = res.needAuthorization ? "未同意" : "已同意"
         const privacyState = "监听到隐私协议状态已变更为 " + this.appPrivacy;
         uni.showToast({
           "position": "bottom",
           "title": privacyState
         })
      })
      this.listenId = id;
      uni.showToast({
        "position": "bottom",
        "title": "开启监听隐私协议状态"
      })
      // #endif
    },
    onUnload() {
      // #ifdef APP
      //注销监听
      uni.offPrivacyAuthorizationChange(this.listenId)
      this.listenId = 0;
      uni.showToast({
        "position": "bottom",
        "title": "已停止监听隐私协议状态"
      })
      // #endif
    },
		methods: {
			getPrivacySetting() {
			  uni.getPrivacySetting({
			    success: (res) => {
			      this.appPrivacy = res.needAuthorization ? "未同意" : "已同意"
            // #ifdef MP-WEIXIN
            this.privacyContractName = res.privacyContractName
            // #endif
			    }
			  })
			},
      resetPrivacyAuthorization(){
        uni.resetPrivacyAuthorization()
      },
      openPrivacyDialog(){
        uni.openDialogPage({
          url: '/pages/component/button/privacy',
        })
      }
		}
	}
</script>

<style>
.item-box {
    margin-bottom: 10px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
.privacy-button{
  margin-top: 5px;
  margin-bottom: 5px;
}
</style>
