<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.nick_name"
                        placeholder="请输入昵称"></el-input>
                </el-form-item>
                <el-form-item label="手机号：">
                    <el-input style="width:210px" clearable v-model="searchForm.phone"
                        placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item label="是否是管理员：">
                    <el-select style="width:210px" v-model="searchForm.is_admin" placeholder="请选择是否是管理员" 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.status" placeholder="请选择用户状态" clearable>
                        <el-option label="正常" :value="1" />
                        <el-option label="审核中" :value="2" />
                        <el-option label="审核失败" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否被删除：">
                    <el-select style="width:210px" v-model="searchForm.is_deleted" placeholder="请选择是否被删除" 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_disabled" placeholder="请选择是否禁用" 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.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-divider />
            <el-row>
                <el-button type="success" @click="add">新增</el-button>
            </el-row>
            <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="nick_name" label="昵称" width="120" />
                
                <el-table-column prop="access" label="账号" width="120" />
                <el-table-column prop="phone" label="手机号" width="120" />
                <el-table-column prop="is_admin" label="是否管理员" >
                    <template #default="scope">
                       <span v-if="scope.row.is_admin==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_disabled==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_deleted" label="是否被删除">
                    <template #default="scope">
                       <span v-if="scope.row.is_deleted==1">是</span>
                       <span v-else>否</span>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="用户状态">
                    <template #default="scope">
                       <span v-if="scope.row.status==1">正常</span>
                       <span v-else-if="scope.row.status==2">审核中</span>
                       <span v-else-if="scope.row.status==3">审核失败</span>
                    </template>
                </el-table-column>
               <el-table-column label="操作" width="200">
                    <template #default="scope">
                        <el-button size="small"
                            type="primary"
                            @click="openDetail(scope.row.user_id)">修改</el-button>
                            <el-button size="small"
                            :type="scope.row.is_disabled==1?'success':'danger'"
                            @click="handleUpdate(scope.row.user_id,scope.row.is_disabled)">{{scope.row.is_disabled==1?'启用':'禁用'}}</el-button>
                            <el-button size="small"
                            type="danger" v-if="scope.row.is_deleted===0"
                            @click="deleteUser(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>
        <accountSysDetail ref="accountSysDetailRef" @refreshList="getUserList"></accountSysDetail>
    </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:{

    },
    nick_name: ''
})
const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const accountSysDetailRef = ref(null);
const openDetail = (id) => {
    accountSysDetailRef.value.init('edit',id)
}
const add = (id) => {
    accountSysDetailRef.value.init('view',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.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;
     }
     if(searchForm.status===''){
        delete searchForm.status;
     }
     if(searchForm.is_deleted===''){
        delete searchForm.is_deleted;
     }
     if(searchForm.is_disabled===''){
        delete searchForm.is_disabled;
     }
     if(searchForm.is_member===''){
        delete searchForm.is_member;
     }
   
    const res = await userApi.getAccountList({ ...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.delAccount({ user_ids: [String(id)] });
          console.log(res);
        if (res.code==200) {
            ElMessage.success("删除成功")
            getUserList();
        } else {
            ElMessage.error("删除失败")
        }
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消删除',
        })
    })
}
const handleUpdate=(id,status)=>{
    userApi.updateAccountStatus(id,{
        is_disabled:status==1?0:1
    }).then(res=>{
        getUserList()
        ElMessage.success("操作成功")
    })
}
</script>

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