<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="incomeList" @selection-change="handleSelectionChange">
      <el-table-column label="id" align="center" prop="id" />
<!--      <el-table-column label="代理id" align="center" prop="agentId" />-->
      <el-table-column label="借款id" align="center" prop="orderId" />
      <el-table-column label="用戶id" align="center" prop="loanUserId" />
      <el-table-column label="用戶名稱" align="center" prop="username" />
      <el-table-column label="借款金額" align="center" prop="loanAmount" />
      <el-table-column label="收益" align="center" prop="income" />
      <el-table-column label="創建時間" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-tooltip content="查看" placement="top">
            <el-button link type="primary" icon="View" @click="handleView(row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    <!-- 添加或修改代理收益對話框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" label-width="80px">
<!--        <el-form-item label="代理id" prop="agentId">-->
<!--          <el-input v-model="form.agentId" placeholder="請輸入代理id" />-->
<!--        </el-form-item>-->
        <el-form-item label="借款id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="請輸入借款id" />
        </el-form-item>
        <el-form-item label="用戶id" prop="loanUserId">
          <el-input v-model="form.loanUserId" placeholder="請輸入用戶id" />
        </el-form-item>
        <el-form-item label="用戶名稱" prop="username">
          <el-input v-model="form.username" placeholder="請輸入用戶名稱" />
        </el-form-item>
        <el-form-item label="借款金額" prop="loanAmount">
          <el-input v-model="form.loanAmount" placeholder="請輸入借款金額" />
        </el-form-item>
        <el-form-item label="收益" prop="income">
          <el-input v-model="form.income" placeholder="請輸入收益" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script setup="income">
import {reactive, ref, onMounted} from "vue";
import {getAgentIncome} from "@/api/system/income.js";

const loading = ref(true)
const isView=ref(true)
const total = ref(0)
const incomeList = ref([])
const title = ref("")
const open = ref(false)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10
})
// 表單參數
const form = ref({})
const formRef = ref()

onMounted(() => {
  getList()
})

const getList = () => {
  loading.value = true
  const params = {
    pageNum: queryParams.pageNum,
    pageSize: queryParams.pageSize,
  }
  getAgentIncome(params).then(response => {
    incomeList.value = response.data.records
    total.value = response.data.total
    loading.value = false
    console.log(incomeList)
  })
}
// 多選框選中資料
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.loanUserId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 查看按鈕操作 */
const handleView = (row) => {
  reset()
  form.value = { ...row }
  isView.value = true
  open.value = true
  title.value = "查看收益"
}

// 表單重設
const reset = () => {
  form.value = {
    id: null,
    agentId: null,
    orderId: null,
    loanUserId: null,
    loanAmount: null,
    income: null,
    createTime: null,
  }
  if (formRef.value) {
    formRef.value.resetFields && formRef.value.resetFields()
  }
}


// 取消按鈕
const cancel = () => {
  open.value = false
  reset()
  isView.value = false
}

</script>
<style scoped lang="scss">

</style>