<template>
  <s-layout :title="$t('pay.withdraw.title')" class="withdraw-wrap" navbar="inner">
    <!-- <view class="page-bg"></view> -->
    <view
      class="wallet-num-box ss-flex ss-col-center ss-row-between"
      :style="[
        {
          marginTop: '-' + Number(statusBarHeight + 88) + 'rpx',
          paddingTop: Number(statusBarHeight + 108) + 'rpx',
        },
      ]"
    >
      <view class="">
        <view class="num-title">{{$t('pay.withdraw.balance')}}</view>
        <view class="wallet-num">{{ userInfo.commission || '0.00' }}</view>
      </view>
      <button class="ss-reset-button log-btn" @tap="sheep.$router.go('/pages/pay/withdraw-log')"
        >{{$t('pay.withdraw.log')}}</button
      >
    </view>
    <!-- 提现输入卡片-->
    <view class="draw-card">
      <view class="card-title">{{$t('pay.withdraw.amount')}}</view>
      <view class="input-box ss-flex ss-col-center border-bottom">
        <view class="unit">￥</view>
        <uni-easyinput
          :inputBorder="false"
          class="ss-flex-1 ss-p-l-10"
          v-model="state.amount"
          type="number"
          :placeholder="$t('pay.withdraw.enterAmount')"
        />
      </view>
      <view class="bank-box ss-flex ss-col-center ss-row-between ss-m-b-30">
        <view class="name">{{$t('pay.withdraw.getto')}}</view>
        <view class="bank-list ss-flex ss-col-center" @tap="onAccountSelect(true)">
          <view v-if="!state.accountInfo.type" class="empty-text">{{$t('pay.withdraw.type')}}</view>
          <view v-if="state.accountInfo.type === 'wechat'" class="empty-text">{{$t('pay.withdraw.weixin')}}</view>
          <view v-if="state.accountInfo.type === 'alipay'" class="empty-text">{{$t('pay.withdraw.alipay')}}</view>
          <view v-if="state.accountInfo.type === 'bank'" class="empty-text">{{$t('pay.withdraw.bank')}}</view>
          <text class="cicon-forward"></text>
        </view>
      </view>
      <view class="bind-box ss-flex ss-col-center ss-row-between" v-if="state.accountInfo.type">
        <view class="placeholder-text" v-if="state.accountInfo.account_name">
          {{ state.accountInfo.account_header }}|{{ state.accountInfo.account_name }}
        </view>
        <view class="placeholder-text" v-else>{{$t('pay.withdraw.noAccount')}}</view>
        <button class="add-btn ss-reset-button" @tap="onAccountEdit(true)">
          {{ state.accountInfo.account_name ? $t('pay.withdraw.edit') : $t('pay.withdraw.add') }}
        </button>
      </view>
      <button class="ss-reset-button save-btn ui-BG-Main-Gradient ui-Shadow-Main" @tap="onConfirm">
        {{$t('pay.withdraw.confirm')}}
      </button>
    </view>

    <!-- 提现说明 -->
    <view class="draw-notice">
      <view class="title ss-m-b-30">{{$t('pay.withdraw.intro')}}</view>
      <view class="draw-list" v-for="(rule, index) in state.rulesList" :key="index">
        {{ index + 1 }}.{{ rule }}
      </view>
    </view>

    <!-- 选择提现账户 -->
    <account-type-select
      :show="state.accountSelect"
      @close="onAccountSelect(false)"
      round="10"
      v-model="state.accountInfo"
      :methods="state.rules.methods"
    />
    <!-- 编辑账户信息 -->
    <account-info-modal
      v-if="state.accountInfo.type"
      v-model="state.accountInfo"
      :show="state.accountEdit"
      @close="onAccountEdit(false)"
      round="10"
    />
  </s-layout>
</template>

<script setup>
  import { computed, reactive, onBeforeMount } from 'vue';
  import sheep from '@/sheep';
  import accountTypeSelect from './components/account-type-select.vue';
  import accountInfoModal from './components/account-info-modal.vue';
  import { onPageScroll } from '@dcloudio/uni-app';
  const headerBg = sheep.$url.css('/assets/addons/shopro/uniapp/user/withdraw_bg.png');
  onPageScroll(() => {});
  const statusBarHeight = sheep.$platform.device.statusBarHeight * 2;
  function filterRules(rules) {
    let list = [];
    let str1 = '';
    if (rules.min_amount > 0) {
      str1 += $t('pay.withdraw.min')+` ${rules.min_amount} `+$t('pay.withdraw.cny');
    }
    if (rules.max_amount > 0) {
      str1 += $t('pay.withdraw.max')+` ${rules.max_amount}`+$t('pay.withdraw.cny');
    }
    if (str1 !== '') {
      list.push($t('pay.withdraw.onetime') + str1);
    }

    if (rules.max_num > 0) {
      list.push($t('pay.withdraw.per')+ rules.num_unit === 'day' ? $t('pay.withdraw.day') : $t('pay.withdraw.month')+$t('pay.withdraw.maxWIthdraw')+rules.max_num+$t('pay.withdraw.times'));
    }

    if (rules.charge_rate_format > 0) {
      list.push($t('pay.withdraw.msg1')+` ${rules.charge_rate_format}%;`);
    }
    list.push(
      $t('pay.withdraw.msg2')+ rules.auto_arrival ? $t('pay.withdraw.msg3') : $t('pay.withdraw.msg4') +$t('pay.withdraw.msg5')
    );
    list.push($t('pay.withdraw.msg6'));

    return list;
  }

  const userStore = sheep.$store('user');
  const userInfo = computed(() => userStore.userInfo);
  const state = reactive({
    amount: '',
    type: '',
    accountInfo: {},
    accountSelect: false,
    accountEdit: false,
    rules: {
      min_amount: 0,
      max_amount: 0,
      max_num: 0,
      num_unit: 0,
      charge_rate_format: 0,
      charge_rate: 0,
      methods: [],
    },
    rulesList: [],
  });

  const onAccountEdit = (e) => {
    state.accountEdit = e;
  };

  const onAccountSelect = (e) => {
    state.accountSelect = e;
  };

  const onConfirm = async () => {
    let payload = {
      money: state.amount,
      ...state.accountInfo,
    };

    if (payload.money > userInfo.commission || payload.money <= 0) {
      sheep.$helper.toast($t('pay.withdraw.msg7'));
      return;
    }

    if (!payload.type) {
      sheep.$helper.toast($t('pay.withdraw.msg8'));
      return;
    }

    if (!payload.account_name || !payload.account_header || !payload.account_no) {
      sheep.$helper.toast($t('pay.withdraw.msg9'));
      return;
    }

    if (sheep.$platform.name === 'H5' && payload.type === 'wechat') {
      sheep.$helper.toast($t('pay.withdraw.msg10'));
      return;
    }

    let { code, msg, data } = await sheep.$api.pay.withdraw.apply(payload);
    if (code === -1) {
      sheep.$platform.useProvider('wechat').bind();
    }
    if (code === 1) {
      userStore.getInfo();
      uni.showModal({
        title: $t('pay.withdraw.msg11'),
        content: $t('pay.withdraw.msg12'),
        cancelText: $t('pay.withdraw.msg13'),
        confirmText: $t('pay.withdraw.msg14'),
        success: function (res) {
          res.confirm && sheep.$router.go('/pages/pay/withdraw-log');
        },
      });
    }
  };

  async function getWithdrawRules() {
    let { code, data } = await sheep.$api.pay.withdraw.rules();
    if (code === 1) {
      state.rules = data;
      state.rulesList = filterRules(state.rules);
    }
  }

  onBeforeMount(() => {
    getWithdrawRules();
  });
</script>

<style lang="scss" scoped>
  :deep() {
    .uni-input-input {
      font-family: OPPOSANS !important;
    }
  }

  .wallet-num-box {
    padding: 0 40rpx 80rpx;
    background: var(--ui-BG-Main) v-bind(headerBg) center/750rpx 100% no-repeat;
    border-radius: 0 0 5% 5%;

    .num-title {
      font-size: 26rpx;
      font-weight: 500;
      color: $white;
      margin-bottom: 20rpx;
    }

    .wallet-num {
      font-size: 60rpx;
      font-weight: 500;
      color: $white;
      font-family: OPPOSANS;
    }

    .log-btn {
      width: 170rpx;
      height: 60rpx;
      line-height: 60rpx;
      border: 1rpx solid $white;
      border-radius: 30rpx;
      padding: 0;
      font-size: 26rpx;
      font-weight: 500;
      color: $white;
    }
  }

  // 提现输入卡片
  .draw-card {
    background-color: $white;
    border-radius: 20rpx;
    width: 690rpx;
    min-height: 560rpx;
    margin: -60rpx 30rpx 30rpx 30rpx;
    padding: 30rpx;
    position: relative;
    z-index: 3;
    box-sizing: border-box;

    .card-title {
      font-size: 30rpx;
      font-weight: 500;
      margin-bottom: 30rpx;
    }

    .bank-box {
      .name {
        font-size: 28rpx;
        font-weight: 500;
      }

      .bank-list {
        .empty-text {
          font-size: 28rpx;
          font-weight: 400;
          color: $dark-9;
        }

        .cicon-forward {
          color: $dark-9;
        }
      }

      .input-box {
        width: 624rpx;
        height: 100rpx;
        margin-bottom: 40rpx;

        .unit {
          font-size: 48rpx;
          color: #333;
          font-weight: 500;
        }

        .uni-easyinput__placeholder-class {
          font-size: 30rpx;
          height: 36rpx;
        }

        :deep(.uni-easyinput__content-input) {
          font-size: 48rpx;
        }
      }

      .save-btn {
        width: 616rpx;
        height: 86rpx;
        line-height: 86rpx;
        border-radius: 40rpx;
        margin-top: 80rpx;
      }
    }

    .bind-box {
      .placeholder-text {
        font-size: 26rpx;
        color: $dark-9;
      }

      .add-btn {
        width: 100rpx;
        height: 50rpx;
        border-radius: 25rpx;
        line-height: 50rpx;
        font-size: 22rpx;
        color: var(--ui-BG-Main);
        background-color: var(--ui-BG-Main-light);
      }
    }

    .input-box {
      width: 624rpx;
      height: 100rpx;
      margin-bottom: 40rpx;

      .unit {
        font-size: 48rpx;
        color: #333;
        font-weight: 500;
      }

      .uni-easyinput__placeholder-class {
        font-size: 30rpx;
      }

      :deep(.uni-easyinput__content-input) {
        font-size: 48rpx;
      }
    }

    .save-btn {
      width: 616rpx;
      height: 86rpx;
      line-height: 86rpx;
      border-radius: 40rpx;
      margin-top: 80rpx;
    }
  }

  // 提现说明
  .draw-notice {
    width: 684rpx;
    background: #ffffff;
    border: 2rpx solid #fffaee;
    border-radius: 20rpx;
    margin: 20rpx 32rpx 0 32rpx;
    padding: 30rpx;
    box-sizing: border-box;

    .title {
      font-weight: 500;
      color: #333333;
      font-size: 30rpx;
    }

    .draw-list {
      font-size: 24rpx;
      color: #999999;
      line-height: 46rpx;
    }
  }
</style>
