<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.keyword"
                        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="primary" @click="handlExport(1)">当前搜索数据导出</el-button>
                <el-button type="primary" @click="handlExport(2)">当前选择数据导出</el-button>
                <el-button type="primary" @click="handlExport(3)">全部数据导出</el-button>
            </el-row>
            <el-table :data="tableData" row-key="case_user_id" @selection-change="(selection) => { deleteData = selection; }" border style="width: 100%;margin-top:20px">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="platform_name" label="平台名称" width="280">
                </el-table-column>
                
                <el-table-column prop="user_name" label="姓名"  width="100">
                </el-table-column>
                <el-table-column prop="title" label="医生职称"  width="100">
                </el-table-column>
                <el-table-column prop="hospital_name" label="医院名称"  width="100">
                </el-table-column>
                <el-table-column prop="department_name" label="科室"  width="180">
                </el-table-column>
                <el-table-column prop="area" label="省份"  width="100">
                </el-table-column>
                <el-table-column prop="total_score" label="单个病例获得福利数"  width="200">
                </el-table-column>
                <el-table-column prop="start_time" label="开始时间"  width="165" />
                <el-table-column prop="end_time" label="结束时间"  width="165" />
                <!-- <el-table-column label="操作" width="350" fixed="right">
                    <template #default="scope">
                        <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.platform_id)">查看</el-button>
                        <el-button type="default" size="small" @click="handleUpdateStatus(scope.row.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" ></whiteHospital>
     <whiteDoctor ref="whiteDoctorRef" v-else-if="!showPlatform && whiteType==2" ></whiteDoctor>
     <dynamic ref="dynamicRef" v-else-if="!showPlatform && whiteType==3" ></dynamic>
</template>

<script setup>
import userApi from "../api/user";
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox,ElLoading } from 'element-plus';
import { useRouter } from 'vue-router'
import { formatDate } from "../utils/format";
import { downloadFile } from '../utils/downloadFile';
const router = useRouter();
const platformDetailRef = ref(null);
const showPlatform=ref(true)
const whiteHospitalRef = ref(null)
const whiteDoctorRef = ref(null)
const dynamicRef = ref(null)
const emits=defineEmits(['goBack'])
const whiteType=ref(null);
const deleteData = ref([]);
const platform_id = ref('')


// 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 project_id = ref('')

const searchForm = reactive({
    platform_id:'',
    case_id:''
})
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.getStaticUserList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    if(res.data.total>0 && pageFrom.page==1){
        platform_id.value= (res.data.data)[0].platform_id;
    }
    total.value = res.data.total;
}

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

const handleSizeChange = (size) => {
    pageFrom.page_size = size;
    getList();
}
const handleCurrentChange = (current) => {
    pageFrom.page = current;
    getList();
}
const search = () => {
    pageFrom.page =1;
    tableData.value = [];
    getList();
}
const handlExport = async (type) => {
  
  let case_id=searchForm.case_id
  let fromData = null;
  if (type == 1) {

    fromData = {
      type,
      keyword:searchForm.platform_id,
      case_id,
      platform_id:platform_id.value,
    }
  } else if (type == 2) {
    if (deleteData.value.length == 0) {
        ElMessage.warning('请勾选数据');
     
      return false;
    };
    let id = '';
    deleteData.value.forEach((item) => {
      if (id) {
        id += "," + item.case_user_id
      } else {
        id = item.case_user_id;
      }
    })
    fromData = {
      type,
      platform_id:platform_id.value,
      case_id,
      id
    }
  } else if (type == 3) {
    fromData = {
      type,
      case_id,
      platform_id:platform_id.value,
    }
  }
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.2)',
  })
  const { code, data } = await userApi.exportJoin(fromData);
  if (code == 200) {
    downloadFile(data, '参与列表');
  }
  loading.close();
}
const init = (id) => {
    searchForm.case_id= id;
    getList()
}
defineExpose({init})
</script>

<style lang="scss" scoped>

</style>