<template>
  <div class="profit-share-apply">
    <!-- 搜索条件 -->
    <el-card class="search-card">
      <el-form :model="searchForm" label-width="140px" inline>
        <el-form-item label="商户分账指令流水号">
          <el-input 
            v-model="searchForm.out_separate_no" 
            placeholder="请输入商户分账指令流水号" 
            clearable
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item label="分账状态">
          <el-select v-model="searchForm.status" placeholder="请选择分账状态" clearable>
            <el-option label="全部" value="" />
            <el-option label="待处理" value="PENDING" />
            <el-option label="处理中" value="PROCESSING" />
            <el-option label="已受理" value="ACCEPTED" />
            <el-option label="成功" value="SUCCESS" />
            <el-option label="失败" value="FAIL" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表数据 -->
    <el-card class="table-card">
      <template #header>
        <div class="table-header">
          <div class="header-title">分账申请记录列表</div>
        </div>
      </template>
      
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        stripe
      >
        <el-table-column prop="out_separate_no" label="商户分账指令流水号" min-width="200" />
        <el-table-column prop="user_id" label="用户ID" width="100" />
        <el-table-column prop="total_amt" label="订单金额(分)" width="120">
          <template #default="{ row }">
            {{ formatAmount(row.total_amt) }}
          </template>
        </el-table-column>
        <el-table-column prop="recv_no" label="分账接收方编号" width="150" />
        <el-table-column prop="status" label="分账状态" width="100">
          <template #default="{ row }">
            {{ getStatusText(row.status) }}
          </template>
        </el-table-column>
        <el-table-column prop="fail_reason" label="分账失败原因" min-width="150" />
        <el-table-column prop="create_time" label="创建时间" width="180" />
        <el-table-column prop="update_time" label="更新时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.limit"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="pagination"
      />
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="分账申请详情"
      width="600px"
      :before-close="handleCloseDetailDialog"
    >
      <el-form
        v-loading="detailLoading"
        :model="detailData"
        label-width="160px"
        label-position="left"
      >
        <el-form-item label="商户分账指令流水号：">
          <span>{{ detailData.out_separate_no }}</span>
        </el-form-item>
        <el-form-item label="用户ID：">
          <span>{{ detailData.user_id }}</span>
        </el-form-item>
        <el-form-item label="订单金额(分)：">
          <span>{{ formatAmount(detailData.total_amt) }}</span>
        </el-form-item>
        <el-form-item label="分账接收方编号：">
          <span>{{ detailData.recv_no }}</span>
        </el-form-item>
        <el-form-item label="分账状态：">
          <span>{{ getStatusText(detailData.status) }}</span>
        </el-form-item>
        <el-form-item v-if="detailData.fail_reason" label="分账失败原因：">
          <span>{{ detailData.fail_reason }}</span>
        </el-form-item>
        <el-form-item label="创建时间：">
          <span>{{ detailData.create_time }}</span>
        </el-form-item>
        <el-form-item label="更新时间：">
          <span>{{ detailData.update_time }}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleCloseDetailDialog">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ProfitShareOrder",
  props: {
    axiosInstance: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      detailLoading: false,
      searchForm: {
        out_separate_no: '',
        status: ''
      },
      tableData: [],
      pagination: {
        page: 1,
        limit: 10,
        total: 0
      },
      detailDialogVisible: false,
      detailData: {}
    }
  },
  async created() {
    this.fetchData()
  },
  methods: {
    // 格式化金额显示
    formatAmount(amount) {
      return amount || amount === 0 ? amount : '-';
    },

    // 获取分账状态文本
    getStatusText(status) {
      const statusMap = {
        'PENDING': '待处理',
        'PROCESSING': '处理中',
        'ACCEPTED': '已受理',
        'SUCCESS': '成功',
        'FAIL': '失败'
      }
      return statusMap[status] || status
    },

    // 获取列表数据
    async fetchData() {
      if (!this.axiosInstance) {
        this.$message.error('无法获取请求实例')
        return
      }
      
      this.loading = true
      try {
        const params = {
          page: this.pagination.page,
          limit: this.pagination.limit,
          out_separate_no: this.searchForm.out_separate_no,
          status: this.searchForm.status
        }
        
        const res = await this.axiosInstance.get('/lakala/profit_share_order', { params })
        if (res.code === 200) {
          this.tableData = res.page.data
          this.pagination.total = res.page.total
          this.pagination.limit = res.page.per_page
        } else {
          this.$message.error(res.msg || res.message || '获取数据失败')
        }
      } catch (error) {
        console.error('获取分账申请记录列表失败:', error)
        this.$message.error('获取数据失败: ' + (error.message || '未知错误'))
      } finally {
        this.loading = false
      }
    },

    // 获取详情数据
    async fetchDetail(id) {
      if (!this.axiosInstance) {
        this.$message.error('无法获取请求实例')
        return
      }

      this.detailLoading = true
      try {
        const res = await this.axiosInstance.get(`/lakala/profit_share_order/${id}`)
        if (res.code === 200) {
          this.detailData = res.data
        } else {
          this.$message.error(res.msg || res.message || '获取详情失败')
        }
      } catch (error) {
        console.error('获取分账申请详情失败:', error)
        this.$message.error('获取详情失败: ' + (error.message || '未知错误'))
      } finally {
        this.detailLoading = false
      }
    },
    
    // 查询
    handleSearch() {
      this.pagination.page = 1
      this.fetchData()
    },
    
    // 重置
    handleReset() {
      this.searchForm = {
        out_separate_no: '',
        status: ''
      }
      this.pagination.page = 1
      this.fetchData()
    },
    
    // 查看详情
    async handleView(row) {
      await this.fetchDetail(row.id)
      this.detailDialogVisible = true
    },

    // 关闭详情对话框
    handleCloseDetailDialog() {
      this.detailDialogVisible = false
      this.detailData = {}
    },
    
    // 分页相关
    handleSizeChange(val) {
      this.pagination.limit = val
      this.pagination.page = 1
      this.fetchData()
    },
    
    handleCurrentChange(val) {
      this.pagination.page = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.profit-share-apply {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  font-size: 16px;
  font-weight: bold;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>