<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item class="w-[280px]" label="用户信息">
                    <el-input v-model="queryParams.keyword" placeholder="账号/昵称/手机号码" clearable
                        @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item class="w-[280px]" label="门店搜索">
                    <el-select v-model="queryParams.store_id" filterable>
                        <el-option :label="item.name" :value="item.id" v-for="item in storeListData"
                            :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="城市搜索">
                    <el-input v-model="queryParams.city" placeholder="请输入城市名称" clearable @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item label="注册时间">
                    <daterange-picker v-model:startTime="queryParams.create_time_start"
                        v-model:endTime="queryParams.create_time_end" />
                </el-form-item>
                <el-form-item class="w-[280px]" label="渠道标识">
                    <el-select v-model="queryParams.source">
                        <el-option :label="item.name" :value="item.id" v-for="item in sourceListData"
                            :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export-data class="ml-2.5" :fetch-fun="getUserList" :params="queryParams"
                        :page-size="pager.size" />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="头像" min-width="100">
                    <template #default="{ row }">
                        <el-avatar :src="row.avatar" :size="50" />
                    </template>
                </el-table-column>
                <el-table-column label="昵称" prop="nickname" min-width="100" />
                <el-table-column label="手机号码" prop="mobile" min-width="100" />
                <el-table-column label="渠道标识" prop="source_name" min-width="100" />
                <el-table-column label="城市" prop="store_name" min-width="100">
                    <template #default="{ row }">
                        <span>{{ row.city || '—' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="门店" prop="store_name" min-width="100">
                    <template #default="{ row }">
                        <span>{{ row.store_name || '—' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="注册时间" prop="create_time" min-width="120" />
                <el-table-column label="操作" width="180" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['user.user/detail']" type="primary" link>
                            <router-link :to="{
                                path: getRoutePath('user.user/detail'),
                                query: {
                                    id: row.id
                                }
                            }">
                                详情
                            </router-link>
                        </el-button>

                        <el-button v-perms="['user.user/store_balance']" type="primary" link>
                            <router-link :to="{
                                path: getRoutePath('user.user/store_balance'),
                                query: {
                                    id: row.id
                                }
                            }">
                                门店余额
                            </router-link>
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="consumerLists">
import { getUserList, sourceList, storeList } from '@/api/consumer'
import { ClientMap } from '@/enums/appEnums'
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'

const queryParams = reactive({
    keyword: '',
    source: '',
    create_time_start: '',
    create_time_end: '',
    store_id: '',
    city: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUserList,
    params: queryParams
})

getLists()

const sourceListData = ref([])
const getSourceLabel = async () => {
    const sourceOptions = await sourceList()
    sourceListData.value = sourceOptions
}

getSourceLabel()

const storeListData = ref([])
const getStoreLabel = async () => {
    const storeOptions = await storeList()
    storeListData.value = storeOptions
}
getStoreLabel()
</script>
