<template>
  <div class="consume-withdraw-container">
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="120"
      >
        <template #default="{ row }">
          <el-button
            :disabled="row.order_is_confirm === 0"
            type="text"
            @click="handleEdit(row)"
          >
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="exportCsv(row.withdraw_id)">
            {{ __('导出所属订单') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('编号')"
        prop="withdraw_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属店铺')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属订单')"
        prop="order_id"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('提现额度')"
        prop="withdraw_amount"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('提现服务费')"
        prop="withdraw_fee"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('打款额度')"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <h4 style="color: red">{{ getFinallyAmount(row) }}</h4>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('提现状态')"
        prop="withdraw_state"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('描述')"
        prop="withdraw_desc"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('银行')"
        prop="withdraw_bank"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('银行账户')"
        prop="withdraw_account_no"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('开户名称')"
        prop="withdraw_account_name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('联系手机')"
        prop="withdraw_mobile"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('创建时间')"
        prop="withdraw_time"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('银行流水账号')"
        prop="withdraw_bankflow"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('操作管理员')"
        prop="withdraw_user_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="forOperTime"
        :label="__('操作时间')"
        prop="withdraw_opertime"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('导出订单')"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('退款单')"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('发票号')"
        prop="withdraw_invoice_no"
        show-overflow-tooltip
        width="100"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, exportFile } from '@/api/consume/withdraw'
  import { getAccounts } from '@/api/user/base'
  import Edit from './components/ConsumeWithdrawInvoiceno'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ConsumeWithdraw',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        accountOptions: [],
        stateOptions: [
          {
            value: 0,
            label: this.__('申请中'),
          },
          {
            value: 1,
            label: this.__('打款完成'),
          },
          {
            value: 2,
            label: this.__('驳回'),
          },
        ],
        list: [],
        listLoading: true,
        loading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      getFinallyAmount(row) {
        let withdrawFee = row.withdraw_fee
        let withdraw_amount = row.withdraw_amount
        return withdraw_amount - withdrawFee
      },
      formatTime(row) {
        return renderTime(row.withdraw_time)
      },
      forOperTime(row) {
        return renderTime(row.withdraw_opertime)
      },
      formatState(row) {
        const withdrawState = row.withdraw_state
        const obj = this.stateOptions.find(
          (item) => item.value === withdrawState
        )
        if (obj) return obj.label
        return ''
      },
      exportCsv(withdraw_id) {
        exportFile({ withdraw_id: withdraw_id }).then(
          (data) => {
            const blob = new Blob([data])
            const downloadElement = document.createElement('a')
            const href = window.URL.createObjectURL(blob) //创建下载的链接
            downloadElement.href = href
            downloadElement.download = decodeURIComponent('所属订单号.csv') //下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() //点击下载
            document.body.removeChild(downloadElement) //下载完成移除元素
            window.URL.revokeObjectURL(href) //释放掉blob对象
          }
        )
      },
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },
      // formatMoney(row) {
      //   return this.sub(row.withdraw_amount, row.withdraw_fee)
      // },
      __,

      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.withdraw_id) {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
