<template>
  <view class="">
    <view class="info">
      <view class="bg"></view>
      <view class="card">
        <view class="title">提现信息</view>
        <view class="item">
          <text>提现账户</text>
          <text v-if="$store.state.app.userInfo.admin_id">{{$store.state.app.userInfo.company.company_name||''}}</text>
          <text v-else>{{$store.state.app.userInfo.account||''}}</text>
        </view>
        <view class="item">
          <text>可提现金额</text>
          <text>{{payMoney.money}}元</text>
        </view>
      </view>
    </view>
    <lsjUpload ref="lsjUpload" childId="upload1" :size="10" :option="fileOption" height="200rpx" style="margin-top: 28rpx;" :debug="false" :formats="'pdf'"
      :multiple="false" :count="1" :instantly="true" @change="changeFile">
      <view class="change-file">请选择PDF类型的发票</view>
    </lsjUpload>
    <view class="fileList" v-for="(item,index) in files.values()" :key="index">
      <view class="name">
        <image src="../../static/img/contract/pdf.png" style="width: 40rpx;height: 40rpx;margin-right: 12rpx;"></image>
        {{item.name}}
      </view>
      <view class="btn">
        <text @click="resetUpload(item.name)" v-if="item.type=='fail'">重新上传</text>
        <text @click="clearShow(item.name)">删除</text>
      </view>
    </view>
    <view class="tip">
      <view class="title">提示</view>
      <view class="text">
        <text>{{payMoney.end_cycle}}</text>后可提现本周期金额;<br />
        可提现金额是上一个周期的收益金额,并不是当前公司收益金额,提现后将会由后台审核,审核通过后会直接往您所上传的银行卡汇款,请耐心等待;<br/>
        <text style="color: #ff7c32;">提现时请注意,需要先上传PDF类型的电子发票后方可提现</text>
        <!-- <text @click="copyPhone('4008888888')">4008888888</text> -->
      </view>
    </view>
    <view v-if="payMoney.end_cycle" class="price" style="margin-top: 0;">
      <button v-if="is_draw" class="btn" @click="pay">全部提现</button>
      <button else class="btn disabled" @click="noPay">暂时不可提现</button>
    </view>
    <view class="price"><button class="btn btn2" @click="navToRecord">提现记录</button></view>
    <u-modal :show="modelShow" title="提醒" content="删除后上传的文件将会消失,你确定要删除吗?" showCancelButton
      @confirm="clear(del_name);modelShow=false" @cancel="modelShow=false"></u-modal>
  </view>
</template>

<script>
  import { userWithdraw, getCurrCycleWithdraw } from "@/api/pay.js"
  import { userInfo } from "@/api/oaUser.js"
  import { debounce, throttle } from 'lodash'
  import { Toast } from '@/libs/uniApi.js'
  import { FILE_URL } from '@/api/file.js'
  import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue'
  export default {
    components: {
      lsjUpload
    },
    data() {
      return {
        priceList: ['10元', '50元', '100元', '500元', '1000元', '5000元', '全部', '自定义'],
        changeMoney: -1,
        company_money: "0.00",
        payMoney: {
          money: '0.00',
          end_cycle: '',
          end_cycle_time: '',
          invoice: '' // 发票
        },
        is_draw: 1,
        payTimer: null,
        timeCount: 5,
        fileOption: {},
        files: new Map(),
        modelShow: false,
        del_name: ''
      }
    },
    // 点击提现记录
    onNavigationBarButtonTap(res) {
      if (res.index === 0) {
        this.navToRecord();
      }
    },
    onLoad() {
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on('company_money', (e) => {
        this.company_money = e.company_money;
      })
      this.loadWithddrawInfo();
      this.fileOption = {
        url: FILE_URL,
        name: 'file',
        header: {
          token: this.$store.state.app.token
        }
      }
    },
    methods: {
      navToRecord(){
        uni.navigateTo({
          url: '/subpkg/withdrawList/withdrawList'
        })
      },
      /**
       * 某文件上传结束回调(成功失败都回调)
       * @param {Object} item 当前上传完成的文件
       */
      onuploadEnd(item) {
        console.log(`${item.name}已上传结束，上传状态=${item.type}`);
        // 更新当前窗口状态变化的文件
        this.files.set(item.name, item);
        // 强制更新视图
        this.$forceUpdate();
      },
      /**
       * 上传进度回调
       * 如果网页上md文档没有渲染出事件名称onprogre，请复制代码的小伙伴自行添加上哈，没有哪个事件是只(item)的
       * @param {Object} item 当前正在上传的文件
       */
      onprogre(item) {
        // 更新当前状态变化的文件
        this.files.set(item.name, item);
        // 强制更新视图
        this.$forceUpdate();
      },
      /**
       * 文件选择回调
       * @param {Object} files 已选择的所有文件Map集合
       */
      changeFile(files) {
        console.log('当前选择的文件列表：', JSON.stringify([...files.values()]));
        // 更新选择的文件 
        this.files = files;
        // 强制更新视图
        this.$forceUpdate();
      },
      /**
       * 指定上传某个文件
       * @param {Object} name 带后缀名的文件名称
       */
      resetUpload(name) {
        this.$refs.lsjUpload.upload(name);
      },
      /**
       * 移除某个文件
       * @param {Object} name 带后缀名的文件名称
       */
      clear(name) {
        // name=指定文件名，不传name默认移除所有文件
        this.$refs.lsjUpload.clear(name);
      },
      clearShow(name) {
        console.log(name);
        this.del_name = name;
        this.modelShow = true;
      },
      copyPhone(str = "") {
        uni.setClipboardData({
          data: str + "",
          success: (e) => {
            Toast('号码已复制')
          },
          fail: (e) => {
            Toast('复制失败')
          }
        })
      },
      changePrice(index) {
        if (this.changeMoney == index) this.changeMoney = -1;
        else this.changeMoney = index;
        if (this.priceList[index] == '自定义') this.payMoney.money = '';
        else if (this.priceList[index] == '全部') this.payMoney.money = this.$store.state.app.userInfo.user_money || 0;
        else this.payMoney.money = this.priceList[index].split('元')[0];
      },
      // 获取可提现的数据
      async loadWithddrawInfo() {
        let res = await getCurrCycleWithdraw();
        this.payMoney.money = parseFloat(res.data?.user_currrent_total_withdraw_money).toFixed(2);
        this.payMoney.end_cycle_time = res.data.end_cycle;
        res.data.end_cycle ? this.payMoney.end_cycle = uni.$u.timeFormat(res.data.end_cycle, 'yyyy-mm-dd') : this
          .payMoney.end_cycle || '';
        this.is_draw = res.data.is_draw;
      },
      // 判断文件是否上传
      isFile() {
        try {
          let file;
          this.files.forEach((v, k) => {
            file = v.responseText;
          })
          file = JSON.parse(file);
          this.payMoney.invoice = file.data.uri;
          if (this.payMoney.invoice.slice(-4) != '.pdf') {
            return false;
          }
          return true;
        } catch (e) {
          return false
        }
      },
      // 提现
      pay() {
        if (!this.isFile()) return Toast('请先上传PDF类型的发票!')
        if (+this.payMoney.money <= 0) return Toast('提现金额不能小于0!')
        if (!this.payTimer) {
          this.goApply();
          this.payTimer = setInterval(() => {
            this.timeCount--;
            if (this.timeCount <= 0) {
              clearInterval(this.payTimer);
              this.payTimer = null;
              this.timeCount = 5;
            }
          }, 1000)
        } else Toast(this.timeCount + '秒后再提现!')
      },
      noPay(){
        Toast('暂时不可提现!')
      },
      // 提现
      async goApply() {
        this.payMoney.invoice = '';
        uni.showLoading({
          title: '申请中',
          mask: true
        })
        let res = await userWithdraw({
          amount: this.payMoney.money,
          transfer_end_cycel: this.payMoney.end_cycle_time
        });
        uni.hideLoading();
        uni.showToast({
          icon: 'success',
          title: res.msg
        })
      }
    },
    onPullDownRefresh() {
      uni.stopPullDownRefresh()
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #fff;
  }


  .info {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    height: 250rpx;

    .bg {
      background-color: $theme-oa-color;
      height: 170rpx;
      width: 100vw;
      border-radius: 0rpx 0rpx 28rpx 28rpx;
      position: absolute;
      top: 0;
      left: 0;
    }

    .card {
      width: 694rpx;
      min-height: 180rpx;
      background: #FFFFFF;
      margin-top: 38.55rpx;
      box-shadow: 0rpx 0rpx 18rpx 2rpx rgba(50, 116, 249, 0.1);
      border-radius: 14rpx 14rpx 14rpx 14rpx;
      opacity: 1;
      padding: 31.5rpx 28rpx;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      position: absolute;
      top: 0;

      .title {
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
        padding-bottom: 10rpx;
      }

      .item {
        display: flex;
        justify-content: space-between;
        font-size: 32rpx;
        font-weight: 400;
        color: #333333;

        text:nth-child(1) {
          flex-shrink: 0;
          margin-right: 48rpx;
        }
      }
    }
  }

  .price {
    display: flex;
    flex-direction: column;
    padding: 0 28rpx;
    margin-top: 50rpx;

    .title {
      font-size: 32rpx;
      font-weight: 600;
      color: #333333;
      line-height: 39rpx;
      margin-bottom: 42rpx;
    }

    .tab {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-evenly;

      .item {
        width: 217rpx;
        height: 131rpx;
        margin-bottom: 20rpx;
        border-radius: 7rpx 7rpx 7rpx 7rpx;
        border: 2rpx solid #F5F5F5;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 32rpx;
        font-weight: 500;
        line-height: 32rpx;
      }

      .active {
        border: 2rpx solid $theme-oa-color;
        color: $theme-oa-color;
      }
    }

    .input {
      width: 100%;
      height: 112rpx;
      background: #F5F5F5;
      border-radius: 56rpx 56rpx 56rpx 56rpx;
      opacity: 1;
      padding: 0 45.56rpx;
      box-sizing: border-box;
      font-size: 28rpx;
      font-weight: 400;
      color: #333;
      line-height: 35rpx;
    }

    .btn {
      width: 694rpx;
      height: 84rpx;
      margin-top: 73.6rpx;
      background: $theme-oa-color;
      box-shadow: 0rpx 14rpx 44rpx 2rpx #E9EFF5;
      border-radius: 42rpx 42rpx 42rpx 42rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 32rpx;
      font-weight: 400;
      color: #FFFFFF;
    }
    .btn2{
      background-color: #fff;
      color: #333;
      border: 1px solid #eee;
    }
    .disabled{
      background-color: #666;
    }
  }

  .tip {
    display: flex;
    flex-direction: column;
    padding: 0 28rpx;
    margin-top: 84rpx;

    .title {
      font-size: 32rpx;
      font-weight: 500;
      color: $theme-oa-color;
      line-height: 32rpx;
      margin-bottom: 21rpx;
    }

    .text {
      font-size: 28rpx;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.6);
      line-height: 42rpx;
      -webkit-background-clip: text;
    }
  }

  .change-file {
    margin: 0 auto;
    width: 694rpx;
    height: 200rpx;
    border: 2px dashed #ccc;
    border-radius: 14rpx;
    color: #999;
    font-size: 32rpx;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .fileList {
    display: flex;
    width: 694rpx;
    margin: 10rpx auto;
    justify-content: space-between;
    padding: 28rpx;
    border: 2px solid #999;
    border-radius: 14rpx;

    .name {
      display: flex;
    }

    .btn {
      color: red;
    }
  }
</style>