<template>
  <view class="Mall4j page-distribution-center">
    <view class="up-box">
      <!-- 个人信息 -->
      <view class="user-msg">
        <view class="name-level">
          <view class="name">
            {{ distInfo.nickName }}
          </view>
          <block v-if="disNotice.length > 0">
            <swiper
              circular
              vertical
              autoplay
              duration="1000"
              class="swiper-cont"
            >
              <block
                v-for="(item, index) in disNotice"
                :key="index"
              >
                <swiper-item
                  class="items"
                  @tap="onToNotice"
                >
                  <span class="items-text">{{ item.msgTitle }} </span><span class="items-icon">></span>
                </swiper-item>
              </block>
            </swiper>
          </block>
        </view>
        <view class="head-sculpture">
          <image
            v-if="distInfo.pic"
            :src="util.addDomain(distInfo.pic)"
            @error="distInfo.pic = ''"
          />
          <image
            v-else
            src="/static/images/head-sculpture.png"
          />
        </view>
      </view>
      <!-- 资产 -->
      <view class="assets">
        <view
          class="item"
          @tap="onToWithdrawal"
        >
          <view class="number">
            {{ wxs.parsePrice(settledAmount)[0] }}.{{ wxs.parsePrice(settledAmount)[1] }}
          </view>
          <view class="text">
            可提现余额(元)
          </view>
        </view>
        <view
          class="item"
          @tap="onToIncomeDetails"
        >
          <view class="number">
            {{ wxs.parsePrice(accumulateAmount)[0] }}.{{ wxs.parsePrice(accumulateAmount)[1] }}
          </view>
          <view class="text">
            历史总收益(元)
          </view>
        </view>
        <view
          class="item"
          @tap="onToIncomeDetails"
        >
          <view class="number">
            {{ wxs.parsePrice(unsettledAmount)[0] }}.{{ wxs.parsePrice(unsettledAmount)[1] }}
          </view>
          <view class="text">
            待结算收益(元)
          </view>
        </view>
      </view>
    </view>
    <!-- 推广 -->
    <view class="extension-invitation">
      <view
        class="item"
        @tap="onToPromoteGoods"
      >
        <view class="bg">
          <image src="@/package-activities/static/images/extension.png" />
        </view>
        <view class="text-arrow">
          推广商品
        </view>
        <view class="des">
          分销共赢
        </view>
      </view>
      <view
        class="item"
        @tap="onToInvitationCard"
      >
        <view class="bg">
          <image src="@/package-activities/static/images/invitation.png" />
        </view>
        <view class="text-arrow">
          邀请好友
        </view>
        <view class="des">
          携手参与
        </view>
      </view>
    </view>
    <!-- 其他操作 -->
    <view class="actions">
      <view
        class="item"
        @tap="onToMyFriends"
      >
        <view class="text">
          我的好友
        </view>
        <view class="text-arrow" />
      </view>
      <view
        class="item"
        @tap="onToMyUsers"
      >
        <view class="text">
          我的用户
        </view>
        <view class="text-arrow" />
      </view>
      <view
        class="item"
        @tap="onToMyDistribution"
      >
        <view class="text">
          我的推广
        </view>
        <view class="text-arrow" />
      </view>
      <view
        class="item"
        @tap="onToIncomeDetails"
      >
        <view class="text">
          收益明细
        </view>
        <view class="text-arrow" />
      </view>
      <view
        class="item"
        @tap="onToWithdrawalRecord"
      >
        <view class="text">
          提现记录
        </view>
        <view class="text-arrow" />
      </view>
    </view>
    <!-- 确定弹窗 -->
    <confirm-pop
      :show-pop="showPop"
      :pop-content="popContent"
      @handle-pop-cancel="onPopCancel"
      @handle-pop-confirm="onPopConfirm"
    />
  </view>
</template>

<script setup>
import util from '@/utils/util.js'

const wxs = number()
// 弹窗组件参数
const showPop = ref(false) // 是否显示弹窗
const popContent = ref({}) // 弹窗内容
let confirmType = -1 // 弹窗提示类型 0：未实名认证 1：未绑定手机号

const paySettlementType = uni.getStorageSync('cloudPaySettlementType') // 1:通联支付

onLoad(() => {
  // 分销员信息
  onGetDisInfo()

  // 获取分销公告
  onGetDistributionMsg()
  if (paySettlementType === 1) {
    onGetMemberInfo()
  }
})
onShow(() => {
  // 分销钱包数据
  onGetDisInfoData()
})
/**
 * 获取分销员信息
 */
const distInfo = ref({})
const onGetDisInfo = () => {
  http.request({
    url: '/mall4cloud_marketing/distribution_user/distribution_user_info',
    method: 'GET'
  }).then(res => {
    if (res.state === -1) {
      uni.showModal({
        title: '',
        showCancel: false,
        content: $t('distributorBanned'),
        confirmColor: '#eb2444',
        success (res) {
          if (res.confirm) {
            uni.switchTab({
              url: '/pages/user/user'
            })
          }
        }
      })
    }

    if (res.state === 2) {
      uni.showModal({
        title: '',
        content: $t('distributorCleared'),
        confirmText: $t('confirm'),
        confirmColor: '#eb2444',
        showCancel: false
      })
    }
    distInfo.value = res
  })
}

/**
  * 获取分销用户钱包数据
  */
const settledAmount = ref(null)
const accumulateAmount = ref(null)
const unsettledAmount = ref(null)
const onGetDisInfoData = () => {
  http.request({
    url: '/mall4cloud_marketing/distribution_user_wallet',
    method: 'GET',
    hasCatch: true
  }).then(res => {
    unsettledAmount.value = res.unsettledAmount
    if (paySettlementType === 1) {
      // 通联支付下，可提现余额和历史总收益需要单独获取
      // 获取可提现余额（通联）
      http.request({
        url: '/mall4cloud_payment/allinpay/order/query_balance',
        method: 'GET'
      }).then(res => {
        settledAmount.value = res.allAmount
      })
      // 获取历史总收益（通联）
      http.request({
        url: '/mall4cloud_user/user_withdraw/get_total_withdraw_amount',
        method: 'GET'
      }).then(res => {
        accumulateAmount.value = res
      })
      return
    }
    settledAmount.value = res.settledAmount
    accumulateAmount.value = res.accumulateAmount
  }).catch(err => {
    if (err.code === 'A00001') {
      uni.showToast({
        title: err.msg,
        icon: 'none',
        mask: true,
        complete: () => {
          setTimeout(() => {
            uni.redirectTo({
              url: '/pages/my/my'
            })
          }, 1500)
        }
      })
    }
  })
}

// 获取分销公告
const disNotice = ref([])
const onGetDistributionMsg = () => {
  http.request({
    url: '/mall4cloud_marketing/distribution_msg/page',
    method: 'GET',
    data: {
      pageNum: 1,
      pageSize: 20,
      isTop: ''
    }
  }).then(res => {
    disNotice.value = res.list
  })
}

// 获取用户扩展信息（通联支付）
const onGetMemberInfo = () => {
  uni.showLoading({
    title: '',
    mask: true
  })
  http.request({
    url: '/mall4cloud_payment/allinpay/member/get_user_info',
    method: 'GET',
    hasCatch: true
  }).then(res => {
    uni.hideLoading()
    let msg = ''
    if (!res.isIdentityChecked) {
      // 未实名认证
      msg += '进行实名认证'
      confirmType = 0
    }
    if (!res.isPhoneChecked) {
      if (msg) {
        msg += '及绑定安全手机'
      } else {
        msg += '绑定安全手机'
        confirmType = 1
      }
    }
    if (msg) {
      showPop.value = true
      popContent.value.title = '平台已开启通联支付，用户使用分销模块需先' + msg + '，以保障用户账户资金安全'
    }
  }).catch(err => {
    uni.hideLoading()
    uni.showToast({
      title: err.msg,
      icon: 'none',
      duration: 1000
    })
  })
}

// 跳转到分销公告页面
const onToNotice = () => {
  if (disNotice.value.length > 0) {
    uni.navigateTo({
      url: '/package-activities/pages/distribution-notice/distribution-notice'
    })
  }
}

// 去提现
const onToWithdrawal = () => {
  if (paySettlementType === 1) {
  // 开启通联支付下，跳转到通联支付提现页面
    uni.navigateTo({
      url: '/package-activities/pages/allinpay-withdrawal/allinpay-withdrawal'
    })
    return
  }
  uni.navigateTo({
    url: '/package-activities/pages/withdrawal/withdrawal'
  })
}

// 去提现记录
const onToWithdrawalRecord = () => {
  uni.navigateTo({
    url: '/package-activities/pages/withdrawal-record/withdrawal-record'
  })
}

// 去收益明细
const onToIncomeDetails = () => {
  uni.navigateTo({
    url: '/package-activities/pages/income-details/income-details'
  })
}

// 去推广商品
const onToPromoteGoods = () => {
  uni.navigateTo({
    url: '/package-activities/pages/promote-goods/promote-goods'
  })
}

// 去邀请好友
const onToInvitationCard = () => {
  uni.navigateTo({
    url: '/package-activities/pages/invitation-card/invitation-card'
  })
}

// 去我的好友
const onToMyFriends = () => {
  uni.navigateTo({
    url: '/package-activities/pages/my-friends/my-friends'
  })
}

// 去我的用户
const onToMyUsers = () => {
  uni.navigateTo({
    url: '/package-activities/pages/my-users/my-users'
  })
}

// 去我的推广
const onToMyDistribution = () => {
  uni.navigateTo({
    url: '/package-activities/pages/my-distribution/my-distribution'
  })
}

// 弹窗取消
const onPopCancel = () => {
  showPop.value = false
  uni.navigateBack()
}

// 弹窗确定
const onPopConfirm = () => {
  if (confirmType === 0) { // 未实名认证
    uni.navigateTo({
      url: '/package-user/pages/realname-verification/realname-verification'
    })
  } else if (confirmType === 1) { // 未绑定手机号
    uni.navigateTo({
      url: '/package-user/pages/bind-mobile/bind-mobile'
    })
  }
  showPop.value = false
  popContent.value = {}
  confirmType = -1
}

</script>

<style lang="scss" scoped>
@use "./distribution-center.scss";
</style>
