<template>
  <div id="cloud-job">
    <div
      class="title"
    >企业账户：可用余额{{titleInfo.availableAmount}}元 待确认金额{{titleInfo.totalWaitConfirm}}元 帐户总金额{{titleInfo.amount}}元 待付款金额{{titleInfo.totalWaitPay}}元 成功支付金额{{titleInfo.totalSuccessPay}}元</div>
    <div class="table">
      <el-table v-if="tableData.length" height="100%" border style="width: 100%" :data="tableData">
        <el-table-column
          v-for="(prop, index) of tableProps"
          :key="index"
          :prop="prop"
          :label="tableHeaders[index]"
        >
          <template slot-scope="scope">
            <a
              v-if="prop === 'projectName'"
              :href="scope.row | projectLink"
              target="_blank"
            >{{scope.row[prop]}}</a>
            <a
              v-else-if="prop === 'nickname'"
              :href="`/rooter/user/${scope.row['uid']}`"
              target="_blank"
            >{{scope.row[prop]}}</a>
            <span v-else-if="prop === 'create_time'">{{scope.row[prop] | toDate}}</span>
            <template v-else-if="prop === 'state'">
              <el-button
                v-if="scope.row[prop] === '1'"
                type="primary"
                @click="confirmPay(scope.row)"
              >确认付款</el-button>
              <span v-else-if="scope.row[prop] === '4'">已提交</span>
              <span v-else-if="scope.row[prop] === '8'">已成功</span>
              <el-button
                v-else-if="scope.row[prop] === '16'"
                type="danger"
                @click="rePay(scope.row)"
              >失败重试</el-button>
              <span v-else>--</span>
            </template>
            <span v-else>{{scope.row[prop]}}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @current-change="changePagination"
      :current-page.sync="currentPage"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="totalCount"
    ></el-pagination>
  </div>
</template>

<script>
const tableHeaders = [
  "项目名称",
  "付款类型",
  "用户昵称",
  "银行卡号",
  "税前应付",
  "平台服务费",
  "所得税费",
  "开发者净收入",
  "本次应付",
  "实际到账",
  "结算时间",
  "付款流水号",
  "付款时间",
  "操作",
]
const tableProps = [
  "projectName",
  "payTypeShow",
  "nickname",
  "bankCode",
  "prePay",
  "servicePay",
  "getPay",
  "realGet",
  "current_gongmall",
  "amount",
  "create_time",
  "gongmall_order_id",
  "pay_timeShow",
  "state",
]

export default {
  data() {
    return {
      // 头部信息
      titleInfo: {},
      // 数据总条目
      totalCount: 0,
      // 当前页面
      currentPage: 1,
      // 列表头显示内容
      tableHeaders,
      // 列表头字段
      tableProps,
      // 列表数据
      tableData: [],
    }
  },
  mounted() {
    this.getTableData()
  },
  filters: {
    toDate(val) {
      return new Date(val * 1000).toLocaleDateString()
    },
    projectLink(i) {
      const type = i.entity_type
      let link = 'javascript:void(0);'
      if(type === '1') link = `/rooter/outsourceitem/${i.entity_id}>`
      else if(type === '3') link = `/rooter/wagedetails?job_id=${i.entity_id}`
      return link
    }
  },
  methods: {
    // 重新支付
    async rePay(i) {
      const res = await this.$post('/api/admin/gongmall/confirm_pay', {
        id: i.id,
        amount: i.current_gongmall,
        nickname: i.nickname,
      })
      if(res) {
        this.getTableData()
      }
    },
    // 确认支付
    confirmPay(i) {
      this.rePay(i)
    },
    // 页码变动
    changePagination() {
      this.getTableData()
    },
    // 格式化列表数据
    formatTableData(data, rData) {
      this.titleInfo = {
        availableAmount: rData.accountBalance.availableAmount,
        totalWaitConfirm: rData.totalWaitConfirm ? rData.totalWaitConfirm / 100 : '--',
        amount: rData.accountBalance.amount,
        totalWaitPay: rData.totalWaitPay ? rData.totalWaitPay / 100 : '--',
        totalSuccessPay: rData.totalSuccessPay ? rData.totalSuccessPay / 100 : '--',
      }
      return data.map(i => {
        let projectName = '--'
        let prePay = ''
        let servicePay = ''
        let getPay = ''
        let realGet = ''
        // 3 比较特殊,所以这样写
        if(i.entity_type === '3') {
          projectName = rData.periodList[i.entity_id].pro_name
          prePay = ((rData.periodList[i.entity_id].origin_price || 0) / 100).toFixed(2)
          servicePay = ((rData.periodList[i.entity_id].person_platform_fee || 0) / 100).toFixed(2)
          getPay = rData.periodList[i.entity_id].total_person_tax_fee || 0
          realGet = rData.periodList[i.entity_id].developer_fee || 0
        } else {
          if(i.entity_type === '1') projectName = rData.projectList[i.entity_id].pro_name

          prePay = ((rData.projectList[i.entity_id].origin_price || 0) / 100).toFixed(2)
          servicePay = ((rData.projectList[i.entity_id].person_platform_fee || 0) / 100).toFixed(2)
          getPay = rData.projectList[i.entity_id].total_person_tax_fee || 0
          realGet = rData.projectList[i.entity_id].developer_fee || 0
        }
        return {
          ...i,
          projectName,
          payTypeShow: i.pay_type === '1' ? '首付款' : '解冻款',
          bankCode: rData.bankAccounts[i.uid] || '--',
          prePay,
          servicePay,
          getPay,
          realGet,
          pay_timeShow: i.pay_time > 0 ? new Date(i.pay_time).toLocaleDateString() : '--',
        }
      })
    },
    // 获取列表数据
    async getTableData() {
      this.tableData = []
      const p = this.currentPage
      const res = await this.$post("/api/admin/gongmall/orders", { p })
      // console.log(res)
      const data = res.data
      const list = data.list
      this.tableData = this.formatTableData(list, data)
      this.totalCount = Number(data.total)
      this.totalPage = data.totalPage
    }
  }
}
</script>

<style scoped>
.table {
  height: 100%;
  height: calc(100% - 80px);
}
</style>
