<template>
    <div>
        <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="success" @click="add">新增</el-button>
                <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="doctor_id"  @selection-change="(selection) => { deleteData = selection; }" border style="width: 100%;margin-top:20px">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="project_name" label="医生姓名" >
                    <template #default="scope">
                       <span v-if="scope.row.user">{{scope.row.user.user_name}}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="project_name" label="医生数量" width="280">
                    <template #default="scope">
                       <span v-if="scope.row.basic_hospital">{{scope.row.basic_hospital.doctor_number}}</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="created_at" label="创建时间"  width="165" />
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="scope">
                        <el-button type="default" size="small" @click="handleUpdateStatus(scope.row.white_doctor_id,scope.row.status)">{{ scope.row.status==1?'禁用':'启用' }}</el-button> 
                        <el-button type="default" size="small" @click="handleDel(scope.row.white_doctor_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>
       <doctorDetail ref="doctorDetailRef"  @refreshList="getList" :project_platform_id="searchForm.project_platform_id" ></doctorDetail>
    </div>
    
</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 deleteData = ref([]);
const router = useRouter();
const emits=defineEmits(['goBack'])

// 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 searchForm = reactive({
    
})
const goBack=()=>{
    emits('goBack')
}
const doctorDetailRef = ref(null);
const openDetail = (id) => {
    doctorDetailRef.value.init('edit',id)
}
const add = (id) => {
    doctorDetailRef.value.init('view','')
}
// 获取用户列表
const getList = async () => {
    if(!searchForm.status){
        delete searchForm.status
    }
    const res = await userApi.getDoctorList({ ...searchForm, ...pageFrom });
    tableData.value = res.data.data;
    total.value = res.data.total;
}

const handleUpdateStatus=(id,status)=>{
    userApi.updateDoctorStatus(id,{
        status:status==1?2:1
    }).then(res=>{
        getList()
        ElMessage.success("操作成功")
    })
}
const handleDel=(id)=>{
    ElMessageBox.confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userApi.delDoctor(id).then(res=>{
            getList()
            ElMessage.success("操作成功")
        })
      }).catch(() => {
        return false;
      });
}
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 project_platform_id=searchForm.project_platform_id;
  let fromData = null;
  if (type == 1) {

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

<style lang="scss" scoped>

</style>