<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.name" clearable size="mini" class="filter-item"
        style="width: 200px;"
        placeholder="请输入用户名称"
      />
      <el-input
        v-model="listQuery.phone" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入手机号"
      />
      <el-select
        v-model="listQuery.state" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择提现状态"
      >
        <el-option label="全部" :value="null" />
        <el-option label="待审核" value="0" />
        <el-option label="打款中" value="1" />
        <el-option label="通过" value="1" />
        <el-option label="拒绝" value="2" />
        <el-option label="打款成功" value="3" />
        <el-option label="打款失败" value="4" />
        <el-option label="待确认" value="5" />
      </el-select>
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left: 10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <el-button size="mini" type="info" class="filter-item" @click="handleReset">
        重置
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="150" label="流水号" prop="orderSn"></el-table-column>
        <el-table-column align="center" min-width="150" label="用户名称" prop="name" show-overflow-tooltip />
        <el-table-column prop="phone" label="手机号码" />
        <el-table-column prop="withdrawalMoney" label="提现金额" />
        <el-table-column prop="cost" label="手续费"></el-table-column>
        <el-table-column label="处理状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 0">审核中</span>
            <!-- <span v-if="scope.row.state == 1">打款中</span> -->
            <span v-if="scope.row.state == 1">通过</span>
            <span v-if="scope.row.state == 2">拒绝</span>
            <span v-if="scope.row.state == 3">打款成功</span>
            <span v-if="scope.row.state == 4">打款失败</span>
            <span v-if="scope.row.state == 5">待确认</span>
          </template>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间"></el-table-column>
        <el-table-column align="center" width="150" label="备注" prop="cause" show-overflow-tooltip />
        <el-table-column label="操作" width="180" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button v-if="(row.state == 0) || (row.state == 1)" size="mini" @click="handleResolve(row)">
              处理
            </el-button>
            <el-button v-if="row.state !== 3" type="text" @click="handleConfirmTong(row)">
              通联确认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 提现处理 -->
    <WithdrawalProcessing ref="WithdrawalProcessing" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import {
  applicationGetAll,
  getTonglian
} from '@/api/application'
import WithdrawalProcessing from './components/WithdrawalProcessing'
import DetailModal from './components/DetailModal'
export default {
  name: 'Application',
  components: {
    WithdrawalProcessing,
    DetailModal
  },
  data() {
    return {
      listQuery: {
        name: '', // 用户名称
        phone: '', // 手机号码
        state: null, // 提现状态 空-全部 0-待审核 1-通过 2-拒绝
        page: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      listLoading: true
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await applicationGetAll(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = { name: '', phone: '', state: null, page: 1, pageSize: 10 }
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleResolve(row) {
      this.$refs.WithdrawalProcessing && this.$refs.WithdrawalProcessing.handleOpen(row)
    },
    // 通联确认
    handleConfirmTong(row) {
      this.$confirm('确定此项通联确认？')
        .then(async () => {
          await getTonglian({ orderSn: row.orderSn, withdrawalId: row.withdrawalId })
          this.$message({ message: '操作成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
