<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.question_title"
                        placeholder="请输入标题"></el-input>
                </el-form-item>
                <el-form-item label="算算状态：">
                    <el-select style="width:230px" v-model="searchForm.question_status" placeholder="请选择算算状态" clearable>
                        <el-option label="正常"  :value="1" />
                        <el-option label="未发布" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否隐藏：">
                    <el-select style="width:230px" v-model="searchForm.is_hide" 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:230px" v-model="searchForm.is_recommend" 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.click_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.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.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.price" 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.discount_price" 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.created_at" placeholder="请选择排序方式" clearable>
                        <el-option label="倒序" value="desc" />
                        <el-option label="正序" value="asc" />
                    </el-select>
                </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.pay_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="question_title" label="标题" width="280">
                   
                </el-table-column>
                <!-- <el-table-column prop="base_class" label="分类"  >
                    <template #default="scope">
                       <span v-if="scope.row.base_class">{{scope.row.base_class}}</span>
                       <span v-else>—</span>
                    </template>
                </el-table-column> -->
                <el-table-column prop="click_count" label="人气数"  >
                </el-table-column>
                <el-table-column prop="submit_count" label="算算次数"   width="100"/>
                <el-table-column prop="pay_count" label="支付次数"   width="100"/>
                <el-table-column prop="price" label="价格"  />
                <el-table-column prop="discount_price" label="优惠价格"  width="100" />
                <el-table-column prop="is_hide" label="是否推荐"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.is_recommend==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="question_status" label="发布状态" width="100" >
                    <template #default="scope">
                       <span>{{scope.row.question_status==1?'正常':'待发布'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_hide" label="是否隐藏"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.is_hide==1?'隐藏':'正常'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="created_at" label="创建时间"  width="165" />
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="scope">
                        <el-button type="default" size="small" @click="handleUpdateItem(scope.row.question_id,scope.row.is_hide)">{{ scope.row.is_hide==1?'恢复':'隐藏' }}</el-button> 
                        <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.question_id)">编辑</el-button>
                            <el-button size="small"
                             v-if="scope.row.question_status==2"
                             :type="scope.row.question_status==1?'warning':'success'"
                            @click="handleUpdatePublish(scope.row.question_id,scope.row.question_status)">{{scope.row.question_status==1?'下架':'发布'}}</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>
        <questionDetail ref="questionDetailRef" @refreshList="getUserList"></questionDetail>
    </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: '',
    order:{
        click_count:'',
        submit_count:'',
        pay_count:'',
        discount_price:'',
        discount_price:'',
        created_at:''
    }
})
const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const questionDetailRef = ref(null);
const openDetail = (id) => {
   
    questionDetailRef.value.init('edit',id)
}
const add = (id) => {
    questionDetailRef.value.init('view','')
}
// 获取用户列表
const getUserList = async () => {
    if(!searchForm.question_status){
        delete searchForm.question_status
    }
    if(searchForm.is_hide===''){
        delete searchForm.is_hide
    }
    if(searchForm.is_recommend===''){
        delete searchForm.is_recommend
    }
    const res = await userApi.getQuestionList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}
const handleUpdateItem=(id,status)=>{
    userApi.updateQuestionStatus(id,{
        is_hide:status==1?0:1
    }).then(res=>{
        getUserList()
        ElMessage.success("操作成功")
    })
}
const handleUpdatePublish=(id,status)=>{
    userApi.updateQuestionPublish(id,{
        question_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>

</style>