<template>
    <div>
        <el-card>
            <el-form :model="searchForm" label-width="auto" :inline="true">
                <el-form-item label="昵称：">
                    <el-input style="width:210px" clearable v-model="searchForm.user_name"
                        placeholder="请输入昵称"></el-input>
                </el-form-item>
                <el-form-item label="手机号：">
                    <el-input style="width:210px" clearable v-model="searchForm.mobile"
                        placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item label="VIP会员：">
                    <el-select style="width:210px" v-model="searchForm.is_member" placeholder="请选择是否是VIP会员" clearable>
                        <el-option label="是" :value="1" />
                        <el-option label="否" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item label="信息完善：">
                    <el-select style="width:210px" v-model="searchForm.is_info_complete" placeholder="请选择信息是否完善" clearable>
                        <el-option label="是" :value="true" />
                        <el-option label="否" :value="false" />
                    </el-select>
                </el-form-item>
                <el-form-item label="算算次数排序：">
                    <el-select style="width:210px" v-model="searchForm.order.single_submit_count" placeholder="请选择是排序方式" clearable>
                        <el-option label="倒序" value="desc" />
                        <el-option label="正序" value="asc" />
                    </el-select>
                </el-form-item>
                <el-form-item label="支付次数排序：">
                    <el-select style="width:210px" v-model="searchForm.order.single_pay_count" placeholder="请选择是排序方式" clearable>
                        <el-option label="倒序" value="desc" />
                        <el-option label="正序" value="asc" />
                    </el-select>
                </el-form-item>
                <el-form-item label="会员购买次数排序：">
                    <el-select style="width:210px" v-model="searchForm.order.member_buy_count" placeholder="请选择是排序方式" clearable>
                        <el-option label="倒序" value="desc" />
                        <el-option label="正序" value="asc" />
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                <el-button type="primary" @click="search">查询</el-button>
                <!-- <el-button type="default" @click="onSubmit">重置</el-button> -->
                </el-form-item>
            </el-form>
            <el-table :data="tableData" border style="width: 100%;margin-top:20px">
                <el-table-column prop="avatar" label="头像" width="100">
                    <template #default="scope">
                        <el-avatar :size="50" :src="scope.row.avatar" />
                    </template>
                </el-table-column>
                <el-table-column prop="user_name" label="昵称" width="120" />
                <el-table-column prop="mobile" label="手机号" width="120" />
                <el-table-column prop="is_member" label="VIP会员" >
                    <template #default="scope">
                       <span v-if="scope.row.is_member==1">是</span>
                       <span v-else>否</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_info_complete" label="信息完善">
                    <template #default="scope">
                       <span >{{scope.row.is_info_complete?'是':'否'}}</span>
                    </template>
                </el-table-column>
               <el-table-column prop="single_submit_count" label="算算次数" >
                 
                </el-table-column>
              
               
                <el-table-column prop="single_pay_count" label="支付次数" />
                <el-table-column prop="member_buy_count" label="会员购买次数" />
                <el-table-column prop="member_expire_date" label="会员截止时间" width="180">
                    <template #default="scope">
                       <span >{{formatDate(scope.row.member_expire_date)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="80">
                    <template #default="scope">
                        <el-button size="small"
                            @click="openDetail(scope.row.user_id)">详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top:20px" :current-page="pageFrom.page" :page-size="pageFrom.page_size"
                :page-sizes="[10, 20, 30, 40]" layout="->,total, sizes, prev, pager, next, jumper" :total="total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </el-card>
        <accountDetail ref="accountDetailRef"></accountDetail>
    </div>
    
</template>

<script setup>
import userApi from "../../api/user";
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router'
import { formatDate } from "../../utils/format";
const router = useRouter();

// Dom 挂载之后
onMounted(() => {
    getUserList();
})
// 用户数据
let tableData = ref([]);
let total = ref(0);
// 搜索条件
const searchForm = reactive({
    order:{

    },
    user_name: ''
})
const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const accountDetailRef = ref(null);
const openDetail = (id) => {
  
    accountDetailRef.value.init('look',id)
}
// 获取用户列表
const getUserList = async () => {
    // if(!(searchForm.order && searchForm.order.single_submit_count) && !(searchForm.order && searchForm.order.single_pay_count) && !(searchForm.order && searchForm.order.member_buy_count)){
    //         delete searchForm.order;
    // };
    if(searchForm.is_member===''){
        delete searchForm.is_member;
    }
    if(searchForm.is_info_complete===''){
        delete searchForm.is_info_complete;
    }
    if(!(searchForm.order && searchForm.order.single_submit_count)){
        delete searchForm.order.single_submit_count;
     }
     if(!(searchForm.order && searchForm.order.single_pay_count)){
        delete searchForm.order.single_pay_count;
     }
     if(!(searchForm.order && searchForm.order.member_buy_count)){
        delete searchForm.order.member_buy_count;
     }
   
   
    const res = await userApi.getUserList({ ...searchForm, ...pageFrom });
   
    tableData.value = res.data.data;
    
    total.value = res.data.total;
}
const handleSizeChange = (size) => {
    pageFrom.page_size = size;
    getUserList();
}
const handleCurrentChange = (current) => {
    pageFrom.page = current;
    getUserList();
}
const search = () => {
    pageFrom.page = 1;
    getUserList();
}
// 删除用户
const deleteUser = (id) => {
    ElMessageBox.confirm(
        '确定要删除该用户信息吗?',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        const res = await userApi.delUser({ id: id });
        if (res.data.success) {
            ElMessage.success("删除成功")
            getUserList();
        } else {
            ElMessage.error("删除失败")
        }
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消删除',
        })
    })
}
</script>

<style lang="scss" scoped></style>