<template>
    <div v-show="showProject">
        <el-card  >
            <el-form :model="searchForm" label-width="auto" :inline="true">
                <el-form-item label="项目名称：">
                    <el-input style="width:230px" clearable v-model="searchForm.project_name"
                        placeholder="请输入项目名称"></el-input>
                </el-form-item>
                <el-form-item label="项目状态：">
                    <el-select style="width:230px" v-model="searchForm.project_status" placeholder="请选择项目状态" clearable>
                        <el-option label="有效"  :value="1" />
                        <el-option label="无效" :value="0" />
                    </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;min-height:320px">
                <el-table-column prop="project_name" 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="case_count" label="目前病例数"   width="100"/>
                <el-table-column prop="project_platform" label="关联平台"   width="100"/>
              
                <el-table-column prop="project_status" label="项目状态"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.project_status==0?'无效':'有效'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="last_case_updated_at" label="病例最新更新时间"  width="165" />
                <el-table-column prop="created_at" label="创建时间"  width="165" />
                <el-table-column label="操作" width="300" fixed="right">
                    <template #default="scope">
                        <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.project_id)">查看</el-button>
                        <el-button type="default" size="small" @click="handleUpdateStatus(scope.row.project_id,scope.row.project_status)">{{ scope.row.project_status==1?'无效':'有效' }}</el-button> 
                            <el-button size="small"
                            @click="handleGocase(scope.row.project_id)">病例</el-button>
                            <!-- <el-button size="small"
                            @click="handleGoWhite(scope.row.project_id)">白名单</el-button> -->
                            <el-button size="small"
                            @click="handleGoPlatform(scope.row.project_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>
        <projectDetail ref="projectDetailRef"  @refreshList="getUserList"></projectDetail>
       
    </div>
    <platformList ref="platformRef" v-if="!showProject && showPlatform" @goBack="goBack"></platformList>
    <caseList ref="caseRef" v-if="!showProject && showCase" @goBack="goBack"></caseList>
</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();
const platformRef = ref(null);
const caseRef = ref(null);
const projectDetailRef = ref(null);
const showProject = ref(true);
const showPlatform = ref(false);
const showCase = ref(false);

// Dom 挂载之后
onMounted(() => {
    getUserList();
})
// 用户数据
let tableData = ref([]);
let total = ref(0);
// 搜索条件
const searchForm = reactive({
   
   
})
const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const goBack=()=>{
    showProject.value=true;
    showCase.value=false;
    showPlatform.value=false;
}
const openDetail = (id) => {
    projectDetailRef.value.init('edit',id)
}
const add = (id) => {
    projectDetailRef.value.init('view','')
}
// 获取用户列表
const getUserList = async () => {
    if(searchForm.project_status===''){
        delete searchForm.project_status
    }
    const res = await userApi.getItemList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}

const handleUpdateStatus=(id,status)=>{
    userApi.updateItemStatus(id,{
        project_status:status==0?1:0
    }).then(res=>{
        getUserList()
        ElMessage.success("操作成功")
    })
}
const handleGocase=(id)=>{
    showProject.value=false;
    showCase.value=true;
    nextTick(()=>{
        caseRef.value.init(id)
    })
}
const handleGoWhite=(id)=>{
   
    hospitalRef.value.init(id)
}
const handleGoPlatform=(id)=>{
    showProject.value=false;
    showPlatform.value=true;

    nextTick(()=>{
        platformRef.value.init(id)
    })
    
}
const handleSizeChange = (size) => {
    pageFrom.page_size = size;
    getUserList();
}
const handleCurrentChange = (current) => {
    pageFrom.page = current;
    getUserList();
}
const search = () => {
    pageFrom.page =1;
    tableData.value = [];
    getUserList();
}
</script>

<style lang="scss" scoped>

</style>