<template>
  <div class="mod-user">
    <avue-crud
      ref="crudRef"
      :page="page"
      :data="dataList"
      :option="tableOption"
      @search-change="onSearch"
      @selection-change="selectionChange"
      @on-load="getDataList"
    >
      <template #pic="scope">
        <span
          v-if="scope.row.pic"
          class="avue-crud__img"
        >
          <el-icon><Document /></el-icon>
        </span>
        <span v-else>-</span>
      </template>

      <template #portfolioStatus="scope">
        <el-tag
          v-if="scope.row.portfolioStatus === 0"
          type="info"
        >
          待审核
        </el-tag>
        <el-tag
          v-if="scope.row.portfolioStatus === 1"
          type="success"
        >
          审核通过
        </el-tag>
        <el-tag
          v-if="scope.row.portfolioStatus === -1"
          type="danger"
        >
          审核未通过
        </el-tag>
      </template>
      <template #menu="scope">
        <el-button
          v-if="isAuth('user:userPortfolio:detail')"
          type="primary"
          icon="el-icon-edit"
          @click.stop="onDetailOrCheck({portfolioId: scope.row.portfolioId,detailOrCheck:0})"
        >
          详情
        </el-button>
        <el-button
          v-if="isAuth('user:userPortfolio:approve')"
          type="danger"
          icon="el-icon-edit"
          @click.stop="onDetailOrCheck({portfolioId: scope.row.portfolioId,detailOrCheck:1})"
        >
          审核
        </el-button>
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <detail-or-check
      v-if="detailOrCheckVisible"
      ref="detailOrCheckRef"
      @refresh-data-list="getDataList"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import { tableOption } from '@/crud/user/portfolio.js'
import DetailOrCheck from './detail-or-check.vue'

const dataList = ref([])
const dataListLoading = ref(false)
const dataListSelections = ref([])
const detailOrCheckVisible = ref(false)
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

/**
 * 获取数据列表
 */
const getDataList = (pageParam, params, done) => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/user/portfolio/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        params
      )
    )
  })
    .then(({ data }) => {
      dataList.value = data.records
      page.total = data.total
      dataListLoading.value = false
      if (done) done()
    })
}

const detailOrCheckRef = ref(null)
/**
 * 详情 / 审核
 * @param param
 */
const onDetailOrCheck = (param) => {
  detailOrCheckVisible.value = true
  nextTick(() => {
    detailOrCheckRef.value?.init(param)
  })
}

/**
 * 条件查询
 */
const onSearch = (params, done) => {
  getDataList(page, params, done)
}

/**
 * 多选变化
 */
const selectionChange = (val) => {
  dataListSelections.value = val
}
</script>
