<template>
    <div>
        <el-card>
            <el-form :model="searchForm" label-width="auto" :inline="true">
                <el-form-item label="分类名称：">
                    <el-input style="width:230px" clearable v-model="searchForm.class_name"
                        placeholder="请输入分类名称"></el-input>
                </el-form-item>
                <!-- <el-form-item label="标题：">
                    <el-input style="width:230px" clearable v-model="searchForm.question_title"
                        placeholder="请输入标题"></el-input>
                </el-form-item> -->
                <el-form-item label="分类状态：">
                    <el-select style="width:230px" v-model="searchForm.class_status" placeholder="请选择分类状态" clearable>
                        <el-option label="正常" :value="1" />
                        <el-option label="隐藏" :value="2" />
                    </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="class_name" label="分类名称" >
                   
                </el-table-column>
                <el-table-column prop="sort" label="序号"  />
               
               
                <el-table-column prop="is_member" label="状态"  >
                    <template #default="scope">
                       <span>{{scope.row.class_status==1?'正常':'隐藏'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_recommend_search" label="是否推荐搜索"  >
                    <template #default="scope">
                       <span>{{scope.row.is_recommend_search==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
               
                <el-table-column label="操作" width="140">
                    <template #default="scope">
                        <el-button type="default" size="small" @click="handleUpdateItem(scope.row.class_id,scope.row.class_status)">{{ scope.row.class_status==1?'隐藏':'显示' }}</el-button> 
                        <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.class_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>
        <classifyDetail ref="classifyDetailRef" @refreshList="getUserList"></classifyDetail>
    </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({
    class_name: ''
})
const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const classifyDetailRef = ref(null);
const openDetail = (id) => {
   
    classifyDetailRef.value.init('edit',id)
}
const add = (id) => {
    classifyDetailRef.value.init('view','')
}
// 获取用户列表
const getUserList = async () => {
   if(!searchForm.class_status){
        delete searchForm.class_status
   }
    const res = await userApi.getClassifyList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}
const handleUpdateItem=(id,status)=>{
    userApi.updateClassifyStatus(id,{
        class_status:status==1?2:1
    }).then(res=>{
        getUserList()
        ElMessage.success("操作成功")
    })
}
const handleSizeChange = (size) => {
    pageFrom.page_size = size;
    getUserList();
}
const handleCurrentChange = (current) => {
    pageFrom.page = current;
    getUserList();
}
const search = () => {
    pageFrom.page =1;
    
    getUserList();
}
</script>

<style lang="scss" scoped>

 .el-form-item{
   margin-bottom: 0;

 }
</style>