<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.user_name"
                        placeholder="请输入昵称"></el-input>
                        
                </el-form-item>
                <el-form-item label="手机号：">
                    <el-input style="width:230px" clearable v-model="searchForm.user_mobile"
                        placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item label="订单编号：">
                    <el-input style="width:230px" clearable v-model="searchForm.order_no"
                        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.pay_status" placeholder="请选择支付状态" clearable>
                        <!-- 未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款 -->
                        <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.order_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:230px" v-model="searchForm.sort" 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-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="user_mobile" label="手机号" width="120" />
                <el-table-column prop="is_member" label="标题" width="200">
                    <template #default="scope">
                       <span>{{scope.row.question.question_title}}</span>
                    </template>
                </el-table-column>
               <el-table-column prop="order_no" label="订单编号" width="120" />
               
               <el-table-column prop="order_status" label="订单状态" width="90" >
                    <template #default="scope">
                       <span>{{ formatOrderStatus(scope.row.order_status) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="pay_status" label="支付状态" width="90" >
                    <template #default="scope">
                       <span>{{ formatPayStatus(scope.row.pay_status) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_member" label="订单金额" width="90" >
                    <template #default="scope">
                       <span>{{scope.row.amount_total}}元</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_member" label="支付金额" width="90">
                    <template #default="scope">
                       <span>{{scope.row.payment_amount_total}}元</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_member" label="支付时间"  width="180">
                    <template #default="scope">
                       <span>{{formatDate(scope.row.pay_time)}}</span>
                    </template>
                </el-table-column>
               
                <el-table-column prop="is_member" label="提交时间"  width="180">
                    <template #default="scope">
                       <span>{{formatDate(scope.row.created_at)}}</span>
                    </template>
                </el-table-column>
                
                <el-table-column label="操作" fixed="right" width="140">
                    <template #default="scope">
                        <el-button type="default" size="small" @click="handleUpdateItem(scope.row.order_id,scope.row.is_delete)">{{ scope.row.is_delete==1?'显示':'隐藏' }}</el-button> 
                        <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.order_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>
        <itemDetail ref="itemDetailRef"></itemDetail>
    </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,formatOrderStatus,formatRefundStatus,formatPayStatus} from "../../utils/format";

const router = useRouter();

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

    user_name: ''
})
const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const itemDetailRef = ref(null);
const openDetail = (id) => {
    itemDetailRef.value.init('view',id)
}

// 获取用户列表
const getUserList = async () => {
    if(!searchForm.order_status){
        delete searchForm.order_status
    }
    if(!searchForm.pay_status){
        delete searchForm.pay_status
    }
    const res = await userApi.getItemList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}
const handleUpdateItem=(id,status)=>{
    userApi.updateItemStatus(id,{
        is_delete:status==1?0: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();
}
// 删除用户
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>