<template>
  <div class="distribution-user-withdraw-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.user_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入用户昵称')"
              remote
              :remote-method="remoteMethod"
              reserve-keyword
            >
              <el-option
                v-for="item in accountOptions"
                :key="item.user_id"
                :label="item.user_nickname"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.duw_state"
              clearable
              :placeholder="__('请选择')"
            >
              <el-option
                v-for="item in duwStateList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('编号')"
        prop="duw_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('会员支付编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('提现额度')"
        prop="duw_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否通过审核')"
        prop="duw_state"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-if="row.duw_state == 1">{{ __('已审核') }}</div>
          <el-switch
            v-else
            v-model="row.duw_state"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('操作管理员')"
        prop="duw_user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('创建时间')"
        prop="duw_time"
        show-overflow-tooltip
      />
    </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, doEdit } from '@/api/distribution/user/withdraw'
  import { renderTime } from '@/utils/date'
  import Edit from './components/DistributionUserWithdrawEdit'
  import { getAccounts } from '@/api/user/base'

  export default {
    name: 'DistributionUserWithdraw',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        accountOptions: [],
        list: [],
        loading: false,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
          duw_state: -1,
          user_id: undefined,
        },
        duwStateList: [
          {
            label: this.__('全部'),
            value: -1,
          },
          {
            label: this.__('待审核'),
            value: 0,
          },
          {
            label: this.__('已审核'),
            value: 1,
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      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 = []
        }
      },
      formatTime(row) {
        const cardTime = row.duw_time
        return cardTime ? renderTime(cardTime) : ''
      },
      handleEdit(row) {
        if (row.duw_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      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
      },
      async sChange(row) {
        const param = {
          duw_id: row.duw_id,
          duw_state: row.duw_state,
        }
        const { msg, status } = await doEdit(param)
        if (200 == status) {
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        } else {
          this.$baseMessage(msg, 'error', false, 'vab-hey-message-success')
        }
      },
    },
  }
</script>
