<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入医生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属医院" prop="hospitalId" >
        <el-select v-model="queryParams.hospitalId" placeholder="请选择所属医院" clearable filterable size="small">
          <el-option v-for="(option, index) in hospitalList" :key="index" :value="option.dictValue" :label="option.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择所属科室" clearable size="small">
          <el-option
            v-for="dict in depList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职称" prop="position">
        <el-select v-model="queryParams.position" placeholder="职称" clearable size="small">
          <el-option
            v-for="dict in positionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="doctorTable" :row-key="'doctorId'" :reserve-selection="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医生名称" align="center" prop="doctorName"/>
      <el-table-column label="所属医院" align="center" prop="hospitalName"/>
      <el-table-column label="科室" align="center" prop="deptName"/>
      <el-table-column label="职称" align="center" prop="position"/>
      <el-table-column label="手机号" align="center" prop="mobile"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div class="footer-button">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </div>
</template>

<script>
import{listDepartment} from "@/api/his/disease";
import{listAllHospital} from "@/api/his/hospital";
import { getChooseDoctorList } from '@/api/his/doctor'

export default {
  name: "ChooseDoctorComponent",
  props: {
    doctorIds: {
      type: Array,
      default: () => []
    }
  },
  activated() {
    // keep-alive 激活时恢复选中
    this.$nextTick(() => {
      this.restoreSelection()
    })
  },
  watch: {
    // doctorIds 变化时重新恢复选中
    doctorIds: {
      immediate: false,
      handler() {
        this.$nextTick(() => this.restoreSelection())
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorName: null,
        hospitalId: null,
        deptId: null,
        position: null,
        mobile: null
      },
      //医院列表
      hospitalList:[],
      //科室列表
      depList:[],
      positionOptions: [],
      // 总条数
      total: 0,
      // 课程列表
      list: [],
      selected: [],
    }
  },
  created() {
    this.getHospitaldeplist();
    this.getdeplist();
    this.getDicts("sys_doc_position").then(response => {
      this.positionOptions = response.data;
    });
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        doctorName: null,
        hospitalId: null,
        deptId: null,
        position: null,
        mobile: null
      }
      this.handleQuery()
    },
    getList() {
      this.loading = true
      getChooseDoctorList(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
        // 列表加载后，根据传入的 videoIds 自动选中
        this.$nextTick(() => this.restoreSelection())
      })
    },
    /** 查询医院列表 */
    getHospitaldeplist() {
      listAllHospital().then(response => {
        this.hospitalList = response.rows;
      });
    },
    /** 查询科室列表 */
    getdeplist() {
      listDepartment().then(response => {
        this.depList = response.rows;
      });
    },
    handleSelectionChange(selection) {
      this.selected = selection
    },
    cancel() {
      this.$emit('closeChoose')
    },
    submit() {
      this.$emit('selectConfirm', this.selected)
    },
    // 恢复勾选状态
    restoreSelection() {
      const tableRef = this.$refs.doctorTable
      if (!tableRef || !Array.isArray(this.list) || !Array.isArray(this.doctorIds)) return
      tableRef.clearSelection()
      if (this.doctorIds.length === 0) return
      const idsSet = new Set(this.doctorIds)
      this.list.forEach(row => {
        if (row && idsSet.has(row.doctorId)) {
          tableRef.toggleRowSelection(row, true)
        }
      })
    }
  },
}
</script>

<style scoped>
.footer-button {
  display: flex;
  justify-content: flex-end;
  margin-top: 40px;
}
</style>
