<template>
    <div v-show="showPlatform">
        <el-card style="min-height: calc(100vh - 102px);">
            
            <el-form :model="searchForm" label-width="auto" :inline="true">
                <backPage @click="goBack"></backPage>
                <el-form-item label="病例名称：" prop="keyword">
                    <el-input style="width:230px" clearable v-model="searchForm.keyword"
                        placeholder="请输入病例名称或者作者"></el-input>
                </el-form-item>
                <el-form-item label="状态："  prop="status">
                    <el-select style="width:230px" v-model="searchForm.case_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:210px" v-model="searchForm.order.updated_at" placeholder="请选择是排序方式" clearable>
                        <el-option label="正序" value="asc" />
                        <el-option label="倒序" value="desc" />
                    </el-select>
                </el-form-item>  -->
               <el-form-item label="参加人数排序：">
                    <el-select style="width:210px" v-model="searchForm.order.join_num" placeholder="请选择是排序方式" clearable>
                        <el-option label="正序" value="asc" />
                        <el-option label="倒序" value="desc" />
                    </el-select>
                </el-form-item> 
                 <el-form-item label="白名单人数排序：">
                    <el-select style="width:210px" v-model="searchForm.order.join_white_num" placeholder="请选择是排序方式" clearable>
                        <el-option label="正序" value="asc" />
                        <el-option label="倒序" value="desc" />
                    </el-select>
                </el-form-item> 
                <el-form-item label="互动数排序：">
                    <el-select style="width:210px" v-model="searchForm.order.message_num" placeholder="请选择是排序方式" clearable>
                        <el-option label="正序" value="asc" />
                        <el-option label="倒序" value="desc" />
                    </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="case_name" label="病例名称" width="100">
                </el-table-column>
                <el-table-column prop="case_author" label="病例作者" width="100">
                </el-table-column>
                <el-table-column prop="remaining_score" label="剩余福利数"  width="100">
                </el-table-column>
                <el-table-column prop="issued_score" label="已发放积分" width="100">
                </el-table-column>
                <el-table-column prop="platform_join_num" label="参加人数" width="100">
                </el-table-column>
                <el-table-column prop="platform_join_white_num" label="白名单人数" width="100">
                </el-table-column>
                <el-table-column prop="message_num" label="互动数" width="90">
                </el-table-column>
                <!-- <el-table-column prop="is_welfare" label="开启福利"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.is_welfare==1?'是':'否'}}</span>
                    </template>
                </el-table-column> -->
                <el-table-column prop="case_status" label="病例状态"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.case_status==1?'正常':'禁用'}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="updated_at" label="上传时间"  width="165" />
                <el-table-column label="操作" width="480" fixed="right">
                    <template #default="scope">
                        <el-button size="small" type="primary" @click="handleEdit(scope.row.case_id)">编辑</el-button>
                        <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.case_id)">病例明细</el-button>
                        <el-button type="default" size="small" @click="handleUpdateStatus(scope.row.case_id,scope.row.case_status)">{{ scope.row.case_status==1?'禁用':'启用' }}</el-button>
                        <el-button type="default" size="small" @click="goMessage(scope.row.case_id)" >互动留言</el-button> 
                        <el-button type="default" size="small" @click="goJoinDetail(scope.row.case_id)" >参与详情</el-button> 
                        <el-button type="default" size="small" @click="goPlatformStatics(scope.row.case_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>
    </div>
    <platformStatics ref="platformStaticsRef" v-if="!showPlatform && openType==4" @goBack="goPrev"></platformStatics>
    <joinDetail ref="joinDetailRef" v-if="!showPlatform && openType==3" @goBack="goPrev"></joinDetail>
    <messageList ref="messageListRef" v-if="!showPlatform && openType==2" @goBack="goPrev"></messageList>
    <caseDetail ref="caseDetailRef" v-if="!showPlatform && openType==1" @goBack="goPrev"></caseDetail>
    <addCase ref="addCaseRef" @refreshList="getList"></addCase>
</template>

<script setup>
import userApi from "../api/user";
import { nextTick, onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router'
import { formatDate } from "../utils/format";
const router = useRouter();
const showPlatform=ref(true)
const platformStaticsRef = ref(null)
const joinDetailRef = ref(null)
const messageListRef = ref(null);
const caseDetailRef = ref(null);
const openType=ref(null);
const addCaseRef = ref(null);
const emits=defineEmits(['goBack'])


// Dom 挂载之后

// 用户数据
let tableData = ref([]);
let total = ref(0);
// 搜索条件

const pageFrom = reactive({
    page: 1,
    page_size: 10,
})

const project_id = ref('')

const searchForm = reactive({
    order:{}
})
const goBack=()=>{
    emits('goBack')

}
const goPrev=()=>{
    showPlatform.value=true;
    openType.value=null;
}
const goMessage=(id)=>{
    showPlatform.value=false;
    openType.value=2;
    nextTick(()=>{
        messageListRef.value.init(id)
    })
}
const goJoinDetail=(id)=>{
    showPlatform.value=false;
    openType.value=3;
    nextTick(()=>{
        joinDetailRef.value.init(id)
    })
}
const goPlatformStatics=(id)=>{
    showPlatform.value=false;
    openType.value=4
    nextTick(()=>{
        platformStaticsRef.value.init(id,project_id.value)
    })

}
const openDetail = (id) => {
    showPlatform.value=false;
    openType.value=1
    nextTick(()=>{
        caseDetailRef.value.init(id)
    })
}
const add = () => {
    nextTick(()=>{
        addCaseRef.value.init('',project_id.value);
    })
   
    // 中文注释：调用 platformDetailRef 的 value 属性，并调用其 init 方法，传入 'view' 和空字符串 ''
}
const handleEdit = (id) => {
    nextTick(()=>{
        addCaseRef.value.init('edit',project_id.value,id);
    })
}
const openWhite=(id,type)=>{
    whiteType.value=type
    showPlatform.value=false
    if(type==1){
        nextTick(()=>{
           whiteHospitalRef.value.init(id)
        })
    }else if(type==2){
        nextTick(()=>{
           whiteDoctorRef.value.init(id)
        })
    }else if(type==3){
        nextTick(()=>{
            dynamicRef.value.init(id)
        })
    }
   
}
// 获取用户列表
const getList = async () => {
    if(!searchForm.case_status){
        delete searchForm.case_status
    }
    if(!(searchForm.order && searchForm.order.join_num)){
        delete searchForm.order.join_num;
     }
     if(!(searchForm.order && searchForm.order.join_white_num)){
        delete searchForm.order.join_white_num;
     }
     if(!(searchForm.order && searchForm.order.message_num)){
        delete searchForm.order.message_num;
     }
     if(!(searchForm.order && searchForm.order.updated_at)){
        delete searchForm.order.updated_at;
     }
     
    const res = await userApi.getCaseList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}

const handleUpdateStatus=(id,status)=>{
    userApi.updateCaseStatus(id,{
        case_status:status==1?2:1
    }).then(res=>{
        getList()
        ElMessage.success("操作成功")
    })
}
const handleGocase=()=>{

}
const handleGoWhite=()=>{
    
}

const handleSizeChange = (size) => {
    pageFrom.page_size = size;
    getList();
}
const handleCurrentChange = (current) => {
    pageFrom.page = current;
    getList();
}
const search = () => {
    pageFrom.page =1;
    getList();
}
const init = (id) => {
    project_id.value = id;
    searchForm.project_id = id;
    getList()
}
defineExpose({
    init
})
</script>

<style lang="scss" scoped>

</style>