<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="网络" prop="networkId">

            <el-select v-model="queryParams.networkId" clearable placeholder="请选择网络">
              <el-option
                v-for="item in networkList"
                :key="'querynetwork' + item.id"
                :label="item.networkName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>

        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permisaction="['admin:wmAutoLog:export']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleExport"
            >导出
            </el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="wmTransferItemList" @cell-dblclick="handleCellDblClick">
          <el-table-column
            label="网络"
            align="center"
            prop="networkName"
            width="140px"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="代币"
            align="center"
            prop="tokenName"
            width="80px"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="来源地址" align="center" prop="fromAddress" :show-overflow-tooltip="true" />
          <el-table-column label="目标地址" align="center" prop="toAddress" :show-overflow-tooltip="true" />
          <el-table-column label="代币数量" align="center" prop="amount" :show-overflow-tooltip="true" />
          <el-table-column label="私钥" align="center" prop="privateKey" :show-overflow-tooltip="true" />
          <el-table-column label="交易hash" prop="hash" :show-overflow-tooltip="true" />
          <el-table-column label="状态" prop="status" width="80" align="center">
            <template slot-scope="scope">
              {{ statusFormat(scope.row) }}
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createAt" width="155">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createAt) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import { getWmTransferItemAutoLogPage, exportWmTransferItemAutoLogExcel } from '@/api/admin/wm-transfer-item'
import { listWmNetwork } from '@/api/admin/wm-network'
import { resolveBlobByName } from '@/utils/zipdownload'

export default {
  name: 'AutoLog',
  props: {
    open: {
      type: Boolean,
      default: false
    },
    transferId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      wmTransferItemList: [],
      typeOptions: [
        {
          label: '百分比',
          value: 1
        },
        {
          label: '实际金额',
          value: 2
        }
      ],
      transferTypeOptions: [
        {
          label: '批量转出',
          value: 1
        },
        {
          label: '批量汇总',
          value: 2
        }
      ],
      statusOptions: [
        {
          label: '默认',
          value: 0
        },
        {
          label: '交易中',
          value: 1
        },
        {
          label: '已完成',
          value: 2
        },
        {
          label: '已失败',
          value: 3
        }
      ],
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        type: undefined,
        idOrder: 'desc'
      },
      networkList: [],
      total: 0,
      loading: false
    }
  },
  mounted() {
    this.getNetworkList()
    this.getList()
  },
  methods: {
    getNetworkList() {
      listWmNetwork({ pageIndex: 1, pageSize: 100 }).then(response => {
        this.networkList = response.data.list
      })
    },
    reloadDta() {
      this.getList()
    },
    typeFormat(row) {
      const type = this.typeOptions.find((item) => item.value === row.type)
      return type ? type.label : ''
    },
    transferTypeFormat(row) {
      const transferType = this.transferTypeOptions.find(
        (item) => item.value === row.transferType
      )
      return transferType ? transferType.label : ''
    },
    statusFormat(row) {
      const status = this.statusOptions.find((item) => item.value === row.status)
      return status ? status.label : ''
    },
    /** 查询参数列表 */
    getList() {
      this.queryParams.transferId = this.transferId
      this.loading = true

      getWmTransferItemAutoLogPage(this.addDateRange(this.queryParams, this.dateRange))
        .then((response) => {
          this.wmTransferItemList = response.data.list
          this.total = response.data.count
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 关系
    // 文件
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleCellDblClick(row, column, cell, event) {
      // 获取单元格文本内容
      const cellText = row[column.property]

      // 使用 Clipboard API 复制文本
      navigator.clipboard.writeText(cellText).then(() => {
        this.$message.success('已复制到剪贴板: ' + cellText)
      }).catch(err => {
        console.error('复制失败:', err)
        this.$message.error('复制失败')
      })
    },
    handleExport() {
      this.loading = true
      exportWmTransferItemAutoLogExcel(this.queryParams).then(res => {
        resolveBlobByName(res, null, '自动转账明细.xlsx')
        this.msgSuccess('导出成功')
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped></style>
