<template>
  <!-- 搜索 -->
  <div class="search-one">
    <div style="text-align: right;" class="search-input">
      <el-input v-model="queryParams.nickname" 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="68px">
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="queryParams.nickname" class="!w-240px" clearable placeholder="请输入用户昵称"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" class="!w-240px" clearable placeholder="请输入手机号"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="注册时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" end-placeholder="结束日期"
          start-placeholder="开始日期" type="daterange" value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
      <el-form-item label="登录时间" prop="loginDate">
        <el-date-picker v-model="queryParams.loginDate" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px" end-placeholder="结束日期" start-placeholder="开始日期" type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
      <!-- <el-form-item label="用户标签" prop="tagIds">
        <MemberTagSelect v-model="queryParams.tagIds" />
      </el-form-item>
      <el-form-item label="用户等级" prop="levelId">
        <MemberLevelSelect v-model="queryParams.levelId" />
      </el-form-item>
      <el-form-item label="用户分组" prop="groupId">
        <MemberGroupSelect v-model="queryParams.groupId" />
      </el-form-item> -->
      <el-row>
        <el-col>
          <el-form-item>
            <el-button @click="hideSearchMore">
              收起
            </el-button>
            <el-button @click="resetQuery">
              重置
            </el-button>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" />搜索
            </el-button>
            <!-- <el-button v-hasPermi="['promotion:coupon:send']" @click="openCoupon">发送优惠券</el-button> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
  <el-row>
    <el-col v-for="(o, index) in list" class="card" :key="index" :span="7" :xl="7" :lg="7" :md="7" :sm="24" :xs="24">
      <el-card @click="openTabs('view', o)">
        <div style="display: flex;justify-content: space-between;align-items: center;margin-bottom: 5px;">
          <p style="width: 100%;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;font-size: 16px;color:#000"
            :title="o.nickname">{{ o.nickname }}
          </p>
          <div v-show="!mobile" class="setting" @click.stop="openTabs('update', o)" v-hasPermi="['system:user:update']">
            <el-icon size="20" color="rgb(220 223 231)">
              <Setting />
            </el-icon>
          </div>
        </div>
        <div style="display:flex;">
          <div
            style="width: 70px;height: 70px;margin-right:10px;border:1px solid rgb(220 223 231);border-radius: 50%;display: flex;align-items: center;justify-content: center;overflow: hidden;">
            <img :src="o.avatar" style="width: 100%; height: 100%; object-fit: cover; object-position: center;" />
          </div>

          <div style='line-height: 25px;'>
            <p>{{ o.point }}积分 / {{ o.levelName || 0 }}级</p>
            <p>{{ o.name || "无设置" }}</p>
            <p>{{ o.mobile || "无手机号" }}</p>
            <p>最后登陆时间:{{ formatDate(o.loginDate, 'YYYY/MM/DD hh:mm') }}</p>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
  <div v-if="list.length == 0">
    <p style="text-align: center;margin-top: 100px;">暂无平台用户</p>
  </div>
  <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
    @pagination="getList" />

  <MemberUserTab ref="MemberUserTabRef" @success="getList" />

</template>
<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'
import * as UserApi from '@/api/member/user'
import { DICT_TYPE } from '@/utils/dict'
import MemberTagSelect from '@/views/member/tag/components/MemberTagSelect.vue'
import MemberLevelSelect from '@/views/member/level/components/MemberLevelSelect.vue'
import MemberGroupSelect from '@/views/member/group/components/MemberGroupSelect.vue'

import MemberUserTab from './components/MemberUserTab.vue'
import { useRouter } from 'vue-router'
import { CouponSendForm } from '@/views/mall/promotion/coupon/components'
import { checkPermi } from '@/utils/permission'

defineOptions({ name: 'MemberUser' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: null,
  mobile: null,
  loginDate: [],
  createTime: [],
  tagIds: [],
  levelId: null,
  groupId: null
})

/*********************** 中星查询列表开始 **********************/

import { Setting, Search, Operation } from '@element-plus/icons-vue'
// 从路由中引入store中的app仓库
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
}
const MemberUserTabRef = ref()
const openTabs = (type: string, obj?: object) => {
  if (mobile.value) {
    const userObj = JSON.stringify(obj)
    // MemberUserMobileTab
    push({ name: 'MemberUserMobileTab', query: { 'type': type, 'obj': userObj } })
  } else {
    MemberUserTabRef.value.open(type, obj)
  }

}
/************************ 中星查询列表结束 ***************************/

const queryFormRef = ref() // 搜索的表单


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

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

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

/** 打开会员详情 */
const { push } = useRouter()
const openDetail = (id: number) => {
  push({ name: 'MemberUserDetail', params: { id } })
}

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

/** 发送优惠券 */
const couponSendFormRef = ref()
const openCoupon = () => {
  if (selectedIds.value.length === 0) {
    message.warning('请选择要发送优惠券的用户')
    return
  }
  couponSendFormRef.value.open(selectedIds.value)
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style src="@/styles/SearchIndex.css"  lang="scss" scoped></style>
<style lang="scss" scoped>
.setting {
  display: none !important;
}

.card:hover {
  .setting {
    display: block !important;

    .el-icon {
      margin-top: 5px;
    }
  }
}
</style>
