<template>
  <view>
    <view class="application-record" v-if="Object.values(list).length > 0">
      <view class="card-list" v-for="(item,index) in list" :key="index">
        <view class="card-top">
          <view class="title">{{item.contact_name}}({{agentType(item.agent_type)}})</view>
          <view class="time">提交时间：{{item.create_time}}</view>
          <view v-if="item.reason && item.status == 2" class="reason">原因：{{item.reason}}</view>
        </view>
        <view class="line"></view>
        <view class="card-bottom">
          <block v-if="item.orderInfo && Number(item.orderInfo.status) !== 3">
            <view class="card-status">
              <text class="status-text">{{statusText(item)}}</text>
            </view>
            <view class="status-btn" @click="goPay(item.orderInfo)">去支付</view>
          </block>
          <block v-else>
            <view class="card-status">
              <image class="status-icon" v-if="item.status === 0" src="@/static/images/pending.png"></image>
              <image class="status-icon" v-else-if="item.status === 1" src="@/static/images/passed.png"></image>
              <image class="status-icon" v-else-if="item.status === 2" src="@/static/images/not-pass.png"></image>
              <text class="status-text">{{statusText(item)}}</text>
            </view>
            <view class="status-btn" v-if="item.status == 2" @click="jump(item)">修改</view>
          </block>
        </view>
      </view>
    </view>
    <emptyPage v-else title="暂无记录～"></emptyPage>

    <!-- 支付弹框 -->
    <payment
        :payMode='pay_list'
        :pay_close="pay_close"
        @onChangeFun='onChangeFun'
        :order_id="pay_order_id"
        :totalPrice='totalPrice'
        :returnUrl="'/pages/agent/invite/record'">
    </payment>
    <!-- 授权登录 -->
    <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authClose"></authorize>
  </view>
</template>

<script>
import {mapGetters} from "vuex";
import emptyPage from '@/components/emptyPage.vue';
import {agentApplyRecord} from "@/api/agent";
import authorize from '@/components/Authorize';
import payment from '@/components/payment';
import {getUserInfo} from "@/api/user";

export default {
  components: {
    authorize,
    emptyPage,
    payment
  },
  computed: {
    ...mapGetters(['isLogin', 'uid', 'userInfo', 'viewColor'])
  },
  data() {
    return {
      list: [],
      page: 1,
      // 登录相关
      isAuto: false, //没有授权的不会自动授权
      isShowAuth: false,//是否隐藏授权
      // 支付相关
      pay_list: [
        {
          name: "微信支付",
          icon: "icon-weixinzhifu",
          value: 'wechat',
          title: '微信快捷支付',
          payStatus: 1
        },
        {
          name: "支付宝支付",
          icon: "icon-zhifubao",
          value: 'alipay',
          title: '支付宝支付',
          payStatus: this.$store.getters.globalData.alipay_open
        },
        {
          name: "余额支付",
          icon: "icon-yuezhifu",
          value: 'balance',
          title: '可用余额:',
          number: 0,
          payStatus: this.$store.getters.globalData.yue_pay_status
        }
      ],
      pay_close: false,
      pay_order_id: '',
      totalPrice: 0,
    }
  },
  onLoad() {
    let _this = this;
    // 判断：是否登录
    if (!this.isLogin) {
      // 未登录  授权登录
      this.isAuto = true;
      this.isShowAuth = true
    }else{
      // 已登录  获取信息
      this.init();
    }
  },
  // 滚动到底部
  onReachBottom() {
    this.getRecordList();
  },
  methods: {
    // 授权回调
    onLoadFun() {
      if(this.isLogin){
        this.isShowAuth = false;
        this.init();
      }
    },
    // 授权关闭
    authClose(e) {
      this.isShowAuth = e
    },
    // 授权成功 初始化
    init () {
      this.getRecordList();
      getUserInfo().then(res => {
        this.pay_list[2].number = res.data.now_money;
      });
    },
    // 记录
    getRecordList() {
      let _this = this;
      agentApplyRecord({page: _this.page}).then(res => {
        let list = res.data.list || {};
        if (Object.values(list).length > 0) {
          _this.list = _this.$util.SplitArray(list, _this.list);
          _this.$set(_this, 'list', _this.list);
          _this.page++;
        }
      }).catch(err => {
        this.$util.Tips({
          title: err
        });
      });
    },
    //状态判断
    statusText(item) {
      let orderStatus = item.orderInfo ? item.orderInfo.status : 0;
      if(Number(orderStatus) != 3 && item.orderInfo){
        return '待支付：' + '¥' + item.orderInfo.pay_price;
      }else{
        let statusData = {
          0: "审核中",
          1: "已通过",
          2: "已驳回"
        };
        return statusData[Number(item.status)]
      }
    },
    //状态判断
    agentType(number) {
      let title = '';
      switch (Number(number)) {
        case 2: title = '省公司';break;
        case 3: title = '省公司外勤';break;
        case 4: title = '省公司内勤';break;
        case 5: title = '运营商';break;
        case 6: title = '合伙人';break;
        case 7: title = '餐厅';break;
        case 8: title = '配送商';break;
        case 9: title = '总部外勤';break;
        case 10: title = '总部内勤';break;
        case 11: title = '烟酒店代销商';break;
      }
      return title;
    },
    // 跳转逻辑
    jump(item) {
      uni.navigateTo({
        url: `/pages/agent/invite/index?apply_id=${item.id}`
      })
    },
    // 支付 - 发起支付
    goPay(orderInfo) {
      this.$set(this, 'pay_close', true);
      this.order_id = orderInfo.group_order_id;
      this.pay_order_id = orderInfo.group_order_id.toString()
      this.$set(this, 'totalPrice', orderInfo.pay_price);
    },
    // 支付 - 事件回调
    onChangeFun(e) {
      let opt = e;
      let action = opt.action || null;
      let value = opt.value != undefined ? opt.value : null;
      (action && this[action]) && this[action](value);
    },
    // 支付 - 关闭弹框
    payClose: function() {
      this.pay_close = false;
    },


  }
}
</script>

<style lang="scss" scoped>
.application-record {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #F5F5F5;
  padding: 20rpx 30rpx;
  .card-list {
    width: 100%;
    background-color: #fff;
    padding: 20rpx 24rpx;
    margin: 10rpx 20rpx;
    border-radius: 12rpx;
    .card-top {
      min-height: 140rpx;
      .title {
        font-size: 28rpx;
        font-weight: bold;
        color: #333333;
      }
      .time {
        color: #999999;
        font-size: 24rpx;
        padding: 5rpx 0;
      }
      .reason {
        color: #E93323;
        font-weight: bold;
        font-size: 24rpx;
      }
    }
    .line {
      height: 2rpx;
      margin: 20rpx 0 20rpx 0;
      background-color: #EEEEEE;
    }
    .card-bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #333;
      .card-status {
        display: flex;
        align-items: center;
        .status-icon {
          width: 30rpx;
          height: 30rpx;
          margin: 10rpx;
        }
        .status-text {
          font-size: 28rpx;
          font-weight: 500;
        }
      }
      .status-btn {
        font-size: 26rpx;
        color: #555;
        border: 1px solid #999999;
        padding: 8rpx 32rpx;
        border-radius: 40rpx;
      }
    }
  }
}
.no-shop {
  width: 100%;
  background-color: #fff;
  height: 100vh;
  .pictrue {
    display: flex;
    flex-direction: column;
    align-items: center;
    color: $uni-nothing-text;
    image {
      width: 414rpx;
      height: 380rpx;
    }
  }
}
</style>
