<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="平台名称：">
                    <el-input style="width:230px" clearable v-model="searchForm.platform_name"
                        placeholder="请输入平台名称"></el-input>
                </el-form-item>
                <el-form-item label="状态：">
                    <el-select style="width:230px" v-model="searchForm.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="platform_name" label="平台名称" >
                </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="status" label="项目状态"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.status==1?'正常':'禁用'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="read_duration" label="阅读时长（秒）"  width="130">
                </el-table-column>
                <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_platform_id)">查看</el-button>
                        <el-button type="default" size="small" @click="handleUpdateStatus(scope.row.project_platform_id,scope.row.status)">{{ scope.row.status==1?'禁用':'启用' }}</el-button> 
                        <el-button type="default" size="small" @click="openWhite(scope.row.project_platform_id,scope.row.white_type)" v-if="scope.row.white_type">白名单{{scope.row.white_type==1?'医院':scope.row.white_type==2?'医生':scope.row.white_type==3?'动态':''}}</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>
        <platformDetail ref="platformDetailRef"  @refreshList="getList" :project_id="project_id"></platformDetail> 
    </div>
     <whiteHospital ref="whiteHospitalRef" v-if="!showPlatform && whiteType==1" @goBack="goPrev"></whiteHospital>
     <whiteDoctor ref="whiteDoctorRef" v-else-if="!showPlatform && whiteType==2" @goBack="goPrev"></whiteDoctor>
     <dynamic ref="dynamicRef" v-else-if="!showPlatform && whiteType==3" @goBack="goPrev"></dynamic>
</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 platformDetailRef = ref(null);
const showPlatform=ref(true)
const whiteHospitalRef = ref(null)
const whiteDoctorRef = ref(null)
const dynamicRef = ref(null)

const whiteType=ref(null);

// Dom 挂载之后

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

const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
// const props=defineProps({
//      project_id:{
//         type:String,
//         default:'1'
//     },
// })
const emits=defineEmits(['goBack'])
const goPrev=()=>{
    showPlatform.value=true;
    whiteType.value=null;
}
const project_id = ref('')

const searchForm = reactive({
})
const goBack=()=>{
    emits('goBack')
}
const openDetail = (id) => {
    platformDetailRef.value.init('look',id)
}
const add = (id) => {
    platformDetailRef.value.init('view','')
}
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.status){
        delete searchForm.status
    }
    const res = await userApi.getPlatformList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}

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

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

<style lang="scss" scoped>

</style>