<template>
  <view style="height: 100vh">
    <zj-page-container>
      <zj-fiex-column>
        <view class="page-content" v-if="!loading">
          <view class="module-view">
            <view class="module-view-single">
              <text>被保险人</text>
              <text>{{ info.workerName }}</text>
            </view>
            <view class="module-view-single">
              <text>被保险人身份证号</text>
              <text>{{ info.workerIdcard }}</text>
            </view>
            <view class="module-view-single">
              <text>保单名称</text>
              <text>{{ info.policyName }}</text>
            </view>
            <view class="module-view-single">
              <text>保单类型</text>
              <text>{{ { AC: '意外险', EM: '雇主险', IN: '工伤险', DS: '第三者责任险' }[info.type] }}</text>
            </view>
            <view class="module-view-single" v-if="info.startTime">
              <text>生效日期</text>
              <text>{{ info.startTime }}</text>
            </view>
            <view class="module-view-single" v-if="info.endTime">
              <text>保障截止时间</text>
              <text>{{ info.endTime }}</text>
            </view>
          </view>
          <view class="module-view">
            <view class="module-view-single">
              <text>付款方式</text>
              <text>全额支付</text>
            </view>
            <view class="module-view-single">
              <text>保费</text>
              <text style="color: #e95505">{{ info.payAmount }}</text>
            </view>
          </view>
        </view>
        <view slot="after" class="bottom-reservation">
          <view class="gouxuan">
            <label :disabled="loading" class="radio" @click="checked = !checked">
              <radio :checked="checked" />
              <text>请阅读</text>
            </label>
            <text v-for="(item, index) in insureAgreeList" :key="index" @click="goDetailsAgreement(item)"
              >{{ item.agreementName }}<text v-if="index < insureAgreeList.length - 1">/</text>
            </text>
          </view>
          <view class="btns">
            <u-button :disabled="loading" type="primary" @click="goback">暂不同意</u-button>
            <u-button :disabled="loading || !checked" type="primary" @click="pay">同意并继续</u-button>
          </view>
        </view>
      </zj-fiex-column>
    </zj-page-container>
  </view>
</template>
<script>
import ZjFiexColumn from '@/components/zj-container/zj-fiex-column.vue'
import ZjPageContainer from '@/components/zj-container/zj-page-container.vue'
import { payMoney } from '@/utils/utils.js'
import { countdown } from '@/utils/tool.js'
import { weixinPay, mini_env } from '@/common/utils/util.js'
export default {
  components: {
    ZjFiexColumn,
    ZjPageContainer
  },
  data() {
    return {
      loading: true,
      checked: false,
      info: {},
      insureAgreeList: [],
      websitId: '',
      id: ''
    }
  },
  onLoad({ websitId, id }) {
    this.id = id
    this.websitId = websitId
    this.$api.post('/worker/buy', { policyId: this.id, websitId }).then(res => {
      this.info = res.data
      this.$nextTick(() => {
        this.loading = false
        uni.hideLoading()
      })
    })
    this.$api.post('/worker/detail', { id }).then(res => {
      var { agreements, policyName, text, policyRanges } = res.data
      this.$api.post('/worker/agreementList', { policyId: this.id }).then(res2 => {
        this.insureAgreeList = res2.data.filter(item => item.agreementType === '投保须知')
      })
    })
  },
  methods: {
    async pay() {
      mini_env(bool => {
        if (!bool) {
          uni.showToast({
            title: '请转移到小程序进行支付操作',
            icon: 'success',
            duration: 2000
          })
          return
        }
        this.$api.post('/worker/pay', { policyOrderId: this.info.id }).then(res => {
          // 小程序环境
          if (bool) {
            uniWebview.navigateTo({
              url: `/pages/pay/pay?${Object.entries({
                ...res.data,
                payPackage: res.data.payPackage.split('=')[0] || '',
                payPackageVal: res.data.payPackage.split('=')[1] || ''
              })
                .map(item => item.join('='))
                .join('&')}`
            })
          }
          // h5环境
          else {
            weixinPay(res.data, function (res) {
              setTimeout(() => {
                that.$navToPage(
                  {
                    url: '/packageEnter/pages/insurance/myInsurance/index'
                  },
                  'reLaunch'
                )
              }, 1000)
            })
          }
        })
      })
    },
    goback() {
      uni.navigateBack({
        delta: 1
      })
    },
    goDetailsAgreement(item) {
      if (item.id) {
        this.$navToPage({
          url: `/packageEnter/pages/insurance/detailsAgreement/index?id=${item.id}`
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
@import './style.scss';
</style>
