import request from '@/helpers/request'
import { Button, Toast } from 'vant'
import { defineComponent } from 'vue'
import styles from './index.module.less'
import UserAuth from '@/views/order-detail/userAuth/index'
import { postMessage } from '@/helpers/native-message'
import { state } from '@/state'

export default defineComponent({
  name: 'cash-protocol',
  data() {
    return {
      realStatus: true, // 是否已经实名认证
      url: ''
    }
  },
  async mounted() {
    try {
      // 先实名认证
      console.log(state.user)
      const user = state.user.data
      if (!user.realName || !user.idCardNo) {
        this.realStatus = false
      } else {
        this.getDetail()
      }
    } catch {}
  },
  methods: {
    async getDetail() {
      try {
        // 查询提现协议
        const res = await request.get(
          '/api-teacher/sysUserContractRecord/queryLatestContractTemplate',
          {
            params: {
              contractType: 'WITHDRAW'
            }
          }
        )

        this.url =
          window.location.origin +
          window.location.pathname +
          '/pdf/web/viewer.html?file=' +
          // encodeURIComponent(
          //   'http://daya.ks3-cn-beijing.ksyun.com/202207/TAxRhc6.pdf'
          // ) +
          encodeURIComponent(res.data.origanalFileUrl) +
          '&btn=1'
      } catch {}
    },
    async onSubmit() {
      try {
        await request.get('/api-teacher/sysUserContractRecord/sign', {
          params: {
            contractType: 'WITHDRAW'
          }
        })
        setTimeout(() => {
          Toast('签署成功')
        }, 100)
        setTimeout(() => {
          postMessage({
            api: 'openPageSuccess',
            content: {
              page: 'WITHDRAW'
            }
          })
        }, 1000)
      } catch {}
    },
    onSuccess() {
      // 实名成功
      this.realStatus = true
      this.getDetail()
    }
  },
  render() {
    return (
      <div class={styles.cashProtocol}>
        {this.realStatus ? (
          <>
            <iframe
              src={this.url}
              frameborder="0"
              style={{ width: '100%', height: '100vh', border: 'none' }}
            ></iframe>
            <Button
              type="primary"
              size="large"
              block
              round
              class={styles.submit}
              onClick={this.onSubmit}
            >
              我已阅读并同意
            </Button>
          </>
        ) : (
          <UserAuth onSuccess={this.onSuccess} />
        )}
      </div>
    )
  }
})
