<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>
      <el-button type="primary" plain @click="openForm('create',{},merchantId)" v-hasPermi="['system:user:create']" v-if="!mobile && !isDetail">
        <Icon icon="ep:plus" /> 新增
      </el-button>
    </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-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-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="openForm('view', o,merchantId)" style="cursor: pointer;">
        <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="openForm('update', o,merchantId)" 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.username || "无手机号" }}</p>
            <p><span v-if="!o.roleName.length">未分配角色
            </span><span v-for="(rolename,index) in o.roleName" :key="rolename">{{ rolename }} <span v-if="index < o.roleName.length - 1 && o.roleName.length > 1">,</span></span></p>
            <p>{{ o.status == 1 ? "停用" : "在用" }}</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" />
    
  <UserTab ref="userTabRef" @success="getList" />

</template>

<script setup>
import { formatDate } from '@/utils/formatTime'
import * as UserApi from '@/api/member/user'
import { MerchantApi } from '@/api/system/sale/merchant'
import { DICT_TYPE } from '@/utils/dict'
// import MemberUserTab from './components/MemberUserTab.vue'
import { useRouter } from 'vue-router'
import { CouponSendForm } from '@/views/mall/promotion/coupon/components'
import { checkPermi } from '@/utils/permission'
import UserTab from './MerchantUserTab.vue'
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const merId = wsCache.get('merId');

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
})
const props = defineProps({
  isDetail: {
    default: false,
    type: Boolean
  }
})
const merchantId = props.id
/*********************** 中星查询列表开始 **********************/

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 userTabRef = ref()
const openForm = (type, obj,merchantId) => {
  userTabRef.value.open(type, obj,merchantId)
}

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

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


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MerchantApi.getAllUserByMer(merId)
    list.value = data
    total.value = data.length
  } finally {
    loading.value = false
  }
}

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

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



/** 初始化 **/
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;
    }
  }
}
::v-deep .el-card__body {
  padding: 0px !important
}
</style>
