<template>
  <!-- 搜索 -->
  <div class="search-one">
    <div style="text-align: right;" class="search-input">
      <el-input v-model="queryParams.username" placeholder="用户名" clearable @keyup.enter="handleQuery">
        <template #suffix>
          <Icon class="mr-5px" icon="ep:search" @click="handleQuery" style="cursor: pointer;" />
        </template>
        <template #append>
          <el-button :icon="Operation" @click="showSearchMore" />
        </template>
      </el-input>
    </div>
  </div>
  <div class="searchMore" v-show="searchMoreShow" :style="{ width: (mobile ? '88.7%' : '96%') }">
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
      <!-- <el-form-item label="团队人员ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入团队人员ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入用户手机号" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <!-- <el-form-item label="团队人员来源" prop="source">
        <el-input v-model="queryParams.source" placeholder="请输入团队人员来源" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="用户佣金" prop="integralId">
        <el-input v-model="queryParams.integralId" placeholder="请输入团队用户当前佣金" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="身价等级" prop="socialStatus">
        <el-select v-model="queryParams.socialStatus" placeholder="请选择团队用户当前身价(等级)" clearable class="!w-240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="身价值" prop="socialStatusPoint">
        <el-input v-model="queryParams.socialStatusPoint" placeholder="请输入团队用户当前点数" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="距下一级点数" prop="categoryId">
        <el-input v-model="queryParams.categoryId" placeholder="请输入用户类型" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" />
      </el-form-item> -->
      <el-row>
        <el-col>
          <el-form-item>
            <el-button @click="hideSearchMore">
              收起
            </el-button>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" class="mr-5px" /> 搜索
            </el-button>
            <el-button @click="resetQuery">
              <Icon icon="ep:refresh" class="mr-5px" /> 重置
            </el-button>
            <el-button type="success" plain @click="handleExport" :loading="exportLoading"
              v-hasPermi="['distri:integral:export']">
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>


  <!-- 列表 -->
  <ContentWrap style="margin-top: 10px;">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户编号" align="center" prop="id" />
      <!-- <el-table-column label="团队人员ID" align="center" prop="userId" /> -->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="用户手机号" align="center" prop="mobile" />
      <!-- <el-table-column label="团队人员来源" align="center" prop="source" /> -->
      <el-table-column label="用户佣金" align="center" prop="integral">
        <template #default="scope">
          {{ pointsToPoint(scope.row.integral) }}
        </template>
      </el-table-column>
      <el-table-column label="冻结佣金" align="center" prop="freezeIntegral">
        <template #default="scope">
          {{ pointsToPoint(scope.row.freezeIntegral) }}
        </template>
      </el-table-column>
      <el-table-column label="身价等级" align="center" prop="socialStatusLevel" />
      <el-table-column label="身价值" align="center" prop="socialStatusPoint" />
      <el-table-column label="距下一级点数" align="center" prop="socialUpNeed" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)" v-hasPermi="['distri:duser:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['distri:duser:delete']">
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DuserForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { pointsToPoint } from '@/utils'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DuserApi, DuserVO } from '@/api/system/distri/duser'
import DuserForm from './DuserForm.vue'
import { Setting, Search, Operation } from '@element-plus/icons-vue'
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
const searchMoreShow = ref(false)
// 打开更多搜索条件
const showSearchMore = () => {
  searchMoreShow.value = !searchMoreShow.value
}
const hideSearchMore = () => {
  searchMoreShow.value = false
}
/** 推荐用户 列表 */
defineOptions({ name: 'Duser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DuserVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  username:undefined,
  nickName: undefined,
  mobile: undefined,
  source: undefined,
  integralId: undefined,
  socialStatus: undefined,
  socialStatusPoint: undefined,
  categoryId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DuserApi.getDuserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DuserApi.deleteDuser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DuserApi.exportDuser(queryParams)
    download.excel(data, '推荐用户.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style src="@/styles/SearchIndex.css"  lang="scss" scoped></style>
