<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="用户昵称" prop="nickname">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.nickname"
                        @keydown.enter="resetPage"
                        clearable
                        placeholder="请输入用户昵称"
                    />
                </el-form-item>
                <el-form-item label="用户账号" prop="account">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.account"
                        @keydown.enter="resetPage"
                        clearable
                        placeholder="请输入用户账号"
                    />
                </el-form-item>
                <el-form-item label="用户电话" prop="mobile">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.mobile"
                        @keydown.enter="resetPage"
                        clearable
                        placeholder="请输入用户电话"
                    />
                </el-form-item>
                <el-form-item label="用户类型" prop="user_ship">
                    <el-select
                        class="w-[280px]"
                        v-model="queryParams.user_ship"
                        placeholder="请选择用户类型"
                        @change="resetPage"
                    >
                        <el-option
                            :label="item.title"
                            :value="item.id"
                            v-for="item in user_ship_data"
                            :key="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="所属门店" prop="store_id">
                    <el-select
                        v-model="queryParams.store_id"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="输入门店名称搜索"
                        remote-show-suffix
                        :remote-method="remoteMethod"
                        :loading="storeloading"
                        style="width: 240px"
                    >
                        <el-option
                            v-for="item in storeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="" prop="create_time">
                    <el-date-picker
                        v-model="startEndTime"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        unlink-panels
                        @change="changeStartEndTime"
                    />
                </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="apiUserLists"
                        :params="queryParams"
                        :page-size="pager.size"
                    />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <!-- <el-button v-perms="['user.user/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
新增
</el-button>
<el-button v-perms="['user.user/delete']" :disabled="!selectData.length" @click="handleDelete(selectData)">
    删除
</el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <!-- <el-table-column type="selection" width="55" /> -->
                    <el-table-column label="ID" prop="id" min-width="55" />
                    <!-- <el-table-column label="头像" prop="avatar" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-avatar :src="row.avatar" />
                        </template>
                    </el-table-column> -->
                    <el-table-column label="真实姓名" prop="real_name" />
                    <el-table-column label="用户昵称" prop="nickname" min-width="100" />
                    <el-table-column label="用户账号" prop="account" min-width="100" />
                    <!-- <el-table-column label="用户密码" prop="password" /> -->
                    <el-table-column label="用户电话" prop="mobile" min-width="100" />
                    <!-- <el-table-column label="用户性别" prop="sex">
                        <template #default="{ row }">
                            <dict-value :options="dictData.sex" :value="row.sex" />
                        </template>
                    </el-table-column>
                    <el-table-column label="注册渠道" prop="channel" /> -->
                    <el-table-column label="用户类型" prop="vip_name" />
                    <el-table-column label="开通时间" prop="vip_time" />

                    <!-- <el-table-column label="用户标签" prop="label_name" /> -->
                    <el-table-column label="礼品券" prop="integral" />
                    <el-table-column label="返还金" prop="return_money">
                        <template #default="{ row }">
                            {{ Number(row.return_money).toFixed(2) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="采购款" prop="purchase_funds" />
                    <el-table-column label="用户余额" prop="now_money" />
                    <el-table-column label="累计消费" prop="total_recharge_amount" />
                    <el-table-column label="地址" prop="format_address" width="150" />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['user.user/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['user.user/detail']"
                                type="primary"
                                link
                                @click="handleDetail(row)"
                            >
                                详情
                            </el-button>
                            <el-dropdown trigger="click">
                                <el-button type="primary" link>更多</el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item
                                            @click="PurchaseFunds(row)"
                                            v-perms="['user.user/purchase_funds']"
                                            >设置采购金</el-dropdown-item
                                        >
                                        <el-dropdown-item
                                            v-perms="['user.user/now_money']"
                                            @click="NowMoney(row)"
                                            >设置余额</el-dropdown-item
                                        >
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <Details ref="detailsRef" :dict-data="dictData"></Details>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
        <PurchaseFundsPopup
            v-if="PurchaseFundsEdit"
            ref="purchaseFundsRef"
            @success="getLists"
            @close="PurchaseFundsEdit = false"
        />
        <NowMoneyPopup
            v-if="nowmoneyRefEdit"
            ref="nowmoneyRef"
            @success="getLists"
            @close="nowmoneyRefEdit = false"
        />
    </div>
</template>

<script lang="ts" setup name="userList">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiUserLists, apiUserDelete } from '@/api/user'
import { apiUserShipLists } from '@/api/user_ship'
import { apiSystemStoreLists } from '@/api/system_store'

import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import PurchaseFundsPopup from './components/purchase_funds.vue'
import NowMoneyPopup from './components/now_money.vue'
import Details from './details.vue'
import moment from 'moment'

const route = useRoute()
const detailsRef = ref(null)
const user_ship_data = ref([])
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const purchaseFundsRef = shallowRef<InstanceType<typeof PurchaseFundsPopup>>()
const nowmoneyRef = shallowRef<InstanceType<typeof NowMoneyPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
const PurchaseFundsEdit = ref(false)
const nowmoneyRefEdit = ref(false)

// 查询条件
const queryParams = reactive({
    nickname: '',
    account: '',
    mobile: '',
    store_id: route.query.store_id ?? '',
    user_ship: '',
    start_time: '',
    end_time: ''
})
apiUserShipLists([]).then((res) => {
    user_ship_data.value = res.lists
})
// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('sex,system_disable')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiUserLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}
const PurchaseFunds = async (data: any) => {
    PurchaseFundsEdit.value = true
    await nextTick()
    purchaseFundsRef.value?.open('edit')
    purchaseFundsRef.value?.setFormData(data)
}
const NowMoney = async (data: any) => {
    nowmoneyRefEdit.value = true
    await nextTick()
    nowmoneyRef.value?.open('edit')
    nowmoneyRef.value?.setFormData(data)
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 详情
const handleDetail = async (data: any) => {
    detailsRef.value?.open()
    detailsRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiUserDelete({ id })
    getLists()
}
const storeloading = ref(false)
const storeList = ref([])
const remoteMethod = (e = '') => {
    storeloading.value = true
    apiSystemStoreLists({
        name: e,
        page_size: 50
    })
        .then((res) => {
            storeList.value = res.lists
            setTimeout(() => {
                storeloading.value = false
            }, 300)
        })
        .catch((err) => {
            setTimeout(() => {
                storeloading.value = false
            }, 300)
        })
}
const startEndTime = ref([new Date(), new Date()])
const changeStartEndTime = () => {
    if (startEndTime.value[0] && startEndTime.value[1]) {
        queryParams.start_time = moment(startEndTime.value[0]).format('YYYY-MM-DD') + ' 00:00:00'
        queryParams.end_time = moment(startEndTime.value[1]).format('YYYY-MM-DD') + ' 23:59:59'
    } else {
        queryParams.start_time = ''
        queryParams.end_time = ''
    }
    getLists()
}
onMounted(() => {
    getLists()
})
</script>
