<!--  -->
<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.shopName" clearable size="mini" class="filter-item" style="width: 200px" placeholder="请输入店铺名称" />
      <el-input v-model="listQuery.shopCode" clearable size="mini" class="filter-item" style="width: 200px; margin-left: 10px" placeholder="请输入店铺编码" />
      <el-date-picker v-model="listQuery.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" size="mini" class="filter-item" style="width: 200px; margin-left: 10px" />
      <el-select v-model="listQuery.state" clearable size="mini" class="filter-item" style="width: 200px; margin-left: 10px" placeholder="请选择处理状态">
        <el-option label="待处理" value="0" />
        <el-option label="已处理" value="1" />
        <el-option label="通过" value="2" />
        <el-option label="拒绝" value="3" />
        <el-option label="待确认" value="4" />
      </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="shopName" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" label="店铺编码" prop="shopCode" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" label="提现类型" prop="withdrawalType">
          <template slot-scope="{ row }">
            <el-tag v-if="row.withdrawalType === 1" effect="plain" type="info">普通订单</el-tag>
            <el-tag v-else-if="row.withdrawalType === 2" effect="plain" type="success">交易金</el-tag>
            <el-tag v-else-if="row.withdrawalType === 3" effect="plain" type="success">代金券</el-tag>
            <el-tag v-else-if="row.withdrawalType === 4" effect="plain" type="success">余额支付</el-tag>
            <el-tag v-else-if="row.withdrawalType === 5" effect="plain" type="success"></el-tag>
            <el-tag v-else-if="row.withdrawalType === 31" effect="plain" type="warning">充值代金券</el-tag>
            <el-tag v-else-if="row.withdrawalType === 32" effect="plain" type="danger">兑换代金券(非兑换专区)</el-tag>
            <el-tag v-else-if="row.withdrawalType === 50" effect="plain" type="danger">联盟卡支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="提现金额" prop="withdrawalMoney" show-overflow-tooltip />
        <el-table-column align="center" label="处理状态" prop="state">
          <template slot-scope="{ row }">
            <el-tag v-if="row.state === 0" effect="plain" type="info">待处理</el-tag>
            <el-tag v-else-if="row.state === 1" effect="plain" type="success">已处理</el-tag>
            <el-tag v-else-if="row.state === 2" effect="plain">通过</el-tag>
            <el-tag v-else-if="row.state === 3" effect="plain" type="warning">拒绝</el-tag>
            <el-tag v-else-if="row.state === 4" effect="plain" type="danger">待确认</el-tag>
            <span v-else>--</span>
          </template>
        </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 == 2" size="mini" @click="handleResolve(row)">处理</el-button>
            <el-button v-if="row.state == 4" 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 WithdrawalProcessing from './components/WithdrawalProcessing'
import DetailModal from './components/DetailModal'
import { withdrawalGetAll, updateWithdrawalByAllinpay } from '@/api/withdrawal'
export default {
  name: 'Withdrawal',
  components: {
    WithdrawalProcessing,
    DetailModal
  },
  data() {
    return {
      listQuery: {
        shopName: '', // 店铺名称
        shopCode: '', // 店铺编号
        startTime: '', // 申请时间数组
        state: '', // 处理状态 1-已处理 0-未处理
        page: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      listLoading: true
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await withdrawalGetAll(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 = { shopName: '', shopCode: '', dates: [], state: '', 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 updateWithdrawalByAllinpay({ handleSn: row.handleSn, 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>
