<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">

      <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号码" />
      <el-button v-permission="['GET /admin/commissionApply/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
      <el-button class="filter-item" type="info" icon="el-icon-info">云账户余额：{{ blance }}</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" label="申请人" prop="referee.refereeName" />
      <el-table-column align="center" label="手机" prop="referee.refereeMobile" />
      <el-table-column align="center" label="总提成额" prop="referee.commissionTotal" />
      <el-table-column align="center" label="已提成" prop="referee.commissionCashout" />
      <el-table-column align="center" label="可提成" prop="referee.commissionBlance"/> 
      <el-table-column align="center" label="本次提请额度" prop="applyAmount">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.applyAmount }}</el-tag>
        </template>
      </el-table-column> 
       <el-table-column align="center" label="本次提请税后" prop="applyActual">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.applyActual }}</el-tag>
        </template>
      </el-table-column> 
      <el-table-column align="center" label="申请时间" width="160" prop="applyTime" />
      <el-table-column align="center" label="申请状态" prop="applyStatus">
        <template slot-scope="scope">
          <el-tag :type="typeTag[scope.row.applyStatus]">{{ scope.row.applyStatus | detailStatusFilter }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.applyStatus === 0" v-permission="['POST /admin/commissionApply/pass']" type="success" size="mini" @click="handlePassConfirm(scope.row)">通过</el-button>
          <el-button v-if="scope.row.applyStatus === 0" v-permission="['POST /admin/commissionApply/reject']" type="danger" size="mini" @click="handleRejectBtn(scope.row)">拒绝</el-button>
          <el-button v-if="scope.row.applyStatus === 5" type="warning" @click="handleViewBtn(scope.row)">失败原因</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :visible.sync="rejectDialogVisible" title="拒绝原因" width="600px">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="拒绝原因" prop="confirmMemo">
          <el-input v-model="dataForm.confirmMemo" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="rejectDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleRejectConfirm()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="viewDialogVisible" title="提现失败原因" width="400">
      <div class="table-layout">
        <el-row>
          <el-col :span="6" class="table-cell-title">支付时间</el-col>
          <el-col :span="18" class="table-cell-title">失败原因</el-col>
        </el-row>
        <el-row>
          <el-col :span="6" class="table-cell">{{ wx_pay.paytime }}</el-col>
          <el-col :span="18" class="table-cell">{{ wx_pay.reason }}</el-col>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewDialogVisible = false">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listApply, pass, reject, blance } from '@/api/refereeCommissionApply'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import _ from 'lodash'

const statusMap = {
  0: '未处理',
  1: '审核通过待发放',
  2: '驳回',
  3: '提现已完成',
  4: '提现处理中',
  5: '提现失败'
}

export default {
  name: 'apply',
  components: { BackToTop, Pagination },
  filters: {
    detailStatusFilter(status) {
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      blance: 0,
      listLoading: true,
      dataForm: {},
      listQuery: {
        page: 1,
        limit: 20,
        mobile: undefined,
        sort: 'id',
        order: 'desc'
      },
      typeTag: [
        '',
        'success',
        'warning',
        'success',
        'info',
        'danger'
      ],
      downloadLoading: false,
      rejectDialogVisible: false,
      rules: {
        confirmMemo: [{ required: true, message: '拒绝原因不能为空', trigger: 'blur' }]
      },
      viewDialogVisible: false,
      wx_pay: {
        paytime: null,
        reason: ''
      }
    }
  },
  created() {
    this.getList()
    // 账户余额
    blance().then(res => {
      console.log(res)
      if (res.data.errno == 0) { this.blance = res.data.data }
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      listApply(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleViewBtn(row) {
      this.viewDialogVisible = true
      this.wx_pay.reason = row.payStatusDesc
      this.wx_pay.paytime = row.payTime
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handlePassConfirm(row) {
      this.$confirm('确定【通过】申请人' + row.referee.refereeMobile + '的提现申请，提现金额【' + row.applyAmount + '】?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handlePass(row)
      })
    },
    handlePass(row) {
      pass(row.id)
        .then(res => {
          row.applyStatus = 1
          this.$notify.success({
            title: '成功',
            message: '审核通过操作成功'
          })
          this.getList()
        })
        .catch((res) => {
          this.$notify.error({
            title: '失败',
            message: res.data.errmsg
          })
        })
    },
    handleRejectBtn(row) {
      this.dataForm = row
      this.rejectDialogVisible = true
    },
    handleRejectConfirm() {
      console.log(this.dataForm)
      this.$confirm('确定【拒绝】申请人' + this.dataForm.applyMobile + '的提现申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleReject()
      })
    },
    handleReject() {
      reject({id:this.dataForm.id,reason:this.dataForm.confirmMemo})
        .then(res => {
          console.log(res)
          this.dataForm.applyStatus = 2
          this.rejectDialogVisible = false
          this.$notify.success({
            title: '成功',
            message: '拒绝通过操作成功'
          })
        })
        .catch((res) => {
          console.log(res)
          this.$notify.error({
            title: '失败',
            message: res.data.errmsg
          })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = [
          '行星名',
          '手机号',
          '总提成额',
          '已提成',
          '本次提请额度',
          '申请时间'
        ]
        const filterVal = ['user.username', 'user.mobile', 'commission.total', 'commission.cashout', 'applyAmount', 'applyTime']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '提现申请')
        this.downloadLoading = false
      })
    }
  }
}
</script>

<style>
.el-table th.gutter {
  display: table-cell !important;
}
</style>
