<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="1000px"
    @close="close"
  >

    <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="queryForm.user_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入用户名称')"
              remote
              :remote-method="remoteFindUserList"
              reserve-keyword
            >
              <el-option
                v-for="item in dataList"
                :key="item.user_id"
                :label="item.user_account"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="fetchData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
    >
      <el-table-column align="center" :label="__('操作')" width="80">
        <template #default="{ row }">
          <el-button
            :loading="loadingPickUp"
            :disabled="row.card_is_use === 1"
            type="text"
            @click="handlePickUp(row)"
          >
            {{ __('核销') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('卡号')"
        prop="card_write_code"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('次卡名称')"
        prop="card_history_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('是否使用')"
        prop="card_is_use"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <el-row v-if="scope.row.card_is_use === 0">
            {{ __('未使用') }}
          </el-row>
          <el-row v-if="scope.row.card_is_use === 1">
            {{ __('已使用') }}
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatHistoryTime"
        :label="__('生成时间')"
        prop="card_history_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('操作人员')"
        prop="operation_user_id"
        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"
    />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {getHistoryList} from '@/api/card/type'
  import {renderTime} from "@/utils/date";
  import {getList} from "@/api/user/base";
  import {doPickUp} from "@/api/order/chain/code";

  export default {
    name: 'PayCardHistoryRecord',
    components: {},
    data() {
      return {
        list: [],
        form: {},
        total: 0,
        loading: false,
        loadingPickUp: false,
        dataList: [],
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    methods: {
      __,
      showTable(row) {
        this.queryForm.card_type_id = row.card_type_id
        this.dialogFormVisible = true
        this.title = this.__('次卡记录')
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      formatHistoryTime(row) {
        return renderTime(row.card_history_time)
      },
      async remoteFindUserList(user_nickname) {
        if (user_nickname !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            getList({ user_nickname: user_nickname }).then((res) => {
              this.dataList = res.data.items
            })
          }, 200)
        } else {
          this.dataList = []
        }
      },
      async handlePickUp(row) {
        this.loadingPickUp = true
        const { msg, status } = await doPickUp({
          chain_code: row.card_write_code,
          order_id: row.order_id,
        })
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        this.loadingPickUp = false
        await this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
        this.queryForm ={
          pageNum: 1,
          pageSize: 20
        }
        this.dataList= []
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getHistoryList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
