<template>
  <div class="page-container">
    <div class="page-header">
      <span class="breadcrumb-label">当前位置：</span>
      <span class="breadcrumb-path">院区信息管理</span>
      <span class="breadcrumb-separator">&gt;</span>
      <span class="breadcrumb-current">科室管理</span>
    </div>
    
    <!-- 查询条件 -->
    <div class="search-bar">
      <div class="search-item">
        <label>科室名称：</label>
        <input v-model="searchForm.name" type="text" placeholder="请输入科室名称" />
      </div>
      <div class="search-item">
        <label>是否启用：</label>
        <select v-model="searchForm.enabled">
          <option value="">全部</option>
          <option :value="1">是</option>
          <option :value="0">否</option>
        </select>
      </div>
      <button class="search-btn" @click="handleSearch">查询</button>
      <button class="reset-btn" @click="handleReset">重置</button>
    </div>

    <!-- 数据列表 -->
    <div class="table-container" v-loading="loading">
      <div v-if="loading" class="loading-mask">
        <div class="loading-spinner"></div>
        <span>加载中...</span>
      </div>
      <table class="data-table" v-show="!loading">
        <thead>
          <tr>
            <th>科室代码</th>
            <th>科室名称</th>
            <th>科室地址</th>
            <th>科室电话</th>
            <th>科室主任</th>
            <th>是否启用</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in tableData" :key="item.id">
            <td>{{ item.code }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.address }}</td>
            <td>{{ item.telephone }}</td>
            <td>{{ item.director }}</td>
            <td>{{ String(item.enabled) === '0' ? '不启用' : '启用' }}</td>
            <td>
              <button class="action-btn detail-btn" @click="showDetail(item)">详情</button>
            </td>
          </tr>
          <tr v-if="tableData.length === 0">
            <td colspan="7" class="no-data">暂无数据</td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 分页 -->
    <div class="pagination" v-if="total > 0">
      <span class="pagination-info">共 {{ total }} 条记录</span>
      <div class="pagination-controls">
        <button class="page-btn" :disabled="currentPage === 1" @click="changePage(1)">首页</button>
        <button class="page-btn" :disabled="currentPage === 1" @click="changePage(currentPage - 1)">上一页</button>
        <span class="page-numbers">
          <button 
            v-for="page in displayPages" 
            :key="page" 
            class="page-num" 
            :class="{ active: page === currentPage }"
            @click="changePage(page)"
          >{{ page }}</button>
        </span>
        <button class="page-btn" :disabled="currentPage === totalPages" @click="changePage(currentPage + 1)">下一页</button>
        <button class="page-btn" :disabled="currentPage === totalPages" @click="changePage(totalPages)">末页</button>
        <span class="page-size-selector">
          <select v-model="pageSize" @change="handlePageSizeChange">
            <option :value="10">10条/页</option>
            <option :value="20">20条/页</option>
            <option :value="50">50条/页</option>
            <option :value="100">100条/页</option>
          </select>
        </span>
        <span class="page-jump">
          跳至 <input type="number" v-model.number="jumpPage" min="1" :max="totalPages" @keyup.enter="handleJumpPage" /> 页
        </span>
      </div>
    </div>

    <!-- 详情弹窗 -->
    <div class="modal-overlay" v-if="detailVisible" @click.self="detailVisible = false">
      <div class="modal-content">
        <div class="modal-header">
          <h3>科室详情</h3>
          <span class="close-btn" @click="detailVisible = false">&times;</span>
        </div>
        <div class="modal-body">
          <div class="detail-row">
            <label>科室代码：</label>
            <span>{{ currentDept.code }}</span>
          </div>
          <div class="detail-row">
            <label>科室外部代码：</label>
            <span>{{ currentDept.outCode }}</span>
          </div>
          <div class="detail-row">
            <label>科室名称：</label>
            <span>{{ currentDept.name }}</span>
          </div>
          <div class="detail-row">
            <label>科室地址：</label>
            <span>{{ currentDept.address }}</span>
          </div>
          <div class="detail-row">
            <label>科室电话：</label>
            <span>{{ currentDept.telephone }}</span>
          </div>
          <div class="detail-row">
            <label>科室主任：</label>
            <span>{{ currentDept.director }}</span>
          </div>
          <div class="detail-row">
            <label>是否启用：</label>
            <span>{{ currentDept.enabled == 0 ? '不启用' : '启用' }}</span>
          </div>
          <div class="detail-row">
            <label>备注：</label>
            <span>{{ currentDept.remark }}</span>
          </div>
          <div class="detail-row intro-row">
            <label>科室介绍：</label>
            <div class="intro-content" v-html="currentDept.introduction"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import request from '@/utils/request'

// 查询表单
const searchForm = reactive({
  name: '',
  enabled: ''
})

// 表格数据
const tableData = ref([])

// 加载状态
const loading = ref(false)

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const jumpPage = ref(1)

// 计算总页数
const totalPages = computed(() => Math.ceil(total.value / pageSize.value) || 1)

// 计算显示的页码
const displayPages = computed(() => {
  const pages = []
  const maxShow = 5
  let start = Math.max(1, currentPage.value - Math.floor(maxShow / 2))
  let end = Math.min(totalPages.value, start + maxShow - 1)
  if (end - start + 1 < maxShow) {
    start = Math.max(1, end - maxShow + 1)
  }
  for (let i = start; i <= end; i++) {
    pages.push(i)
  }
  return pages
})

// 详情弹窗
const detailVisible = ref(false)
const currentDept = ref({})

onMounted(() => {
  loadData()
})

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    const params = {
      page: currentPage.value,
      pageSize: pageSize.value
    }
    if (searchForm.name) params.name = searchForm.name
    if (searchForm.enabled !== '') params.enabled = searchForm.enabled
    
    const res = await request.get('/api/department/list', { params })
    if (res.code === 200) {
      tableData.value = res.data?.records || res.data || []
      total.value = res.data?.total || tableData.value.length
    }
  } catch (error) {
    console.error('加载数据失败：', error)
  } finally {
    loading.value = false
  }
}

// 切换页码
const changePage = (page) => {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page
    jumpPage.value = page
    loadData()
  }
}

// 修改每页条数
const handlePageSizeChange = () => {
  currentPage.value = 1
  loadData()
}

// 跳转到指定页
const handleJumpPage = () => {
  const page = Math.min(Math.max(1, jumpPage.value), totalPages.value)
  changePage(page)
}

// 查询
const handleSearch = () => {
  currentPage.value = 1
  loadData()
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  searchForm.enabled = ''
  currentPage.value = 1
  loadData()
}

// 查看详情
const showDetail = async (item) => {
  try {
    const res = await request.get(`/api/department/detail/${item.id}`)
    if (res.code === 200) {
      currentDept.value = res.data
      detailVisible.value = true
    }
  } catch (error) {
    console.error('获取详情失败：', error)
  }
}
</script>

<style scoped>
.page-container {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
}
.page-header {
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
  margin-bottom: 20px;
  font-size: 14px;
}
.breadcrumb-label {
  color: #666;
}
.breadcrumb-path {
  color: #666;
}
.breadcrumb-separator {
  margin: 0 5px;
  color: #666;
}
.breadcrumb-current {
  color: #409eff;
}

/* 查询栏 */
.search-bar {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
}
.search-item {
  display: flex;
  align-items: center;
}
.search-item label {
  margin-right: 8px;
  color: #666;
  font-size: 14px;
}
.search-item input,
.search-item select {
  padding: 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
}
.search-item input {
  width: 180px;
}
.search-item select {
  width: 120px;
}
.search-btn {
  padding: 8px 20px;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.search-btn:hover {
  background: #66b1ff;
}
.reset-btn {
  padding: 8px 20px;
  background: #fff;
  color: #606266;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
}
.reset-btn:hover {
  color: #409eff;
  border-color: #c6e2ff;
}

/* 表格 */
.table-container {
  overflow-x: auto;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
}
.data-table th,
.data-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ebeef5;
  font-size: 14px;
}
.data-table th {
  background: #fafafa;
  color: #909399;
  font-weight: 500;
}
.data-table tr:hover {
  background: #f5f7fa;
}
.no-data {
  text-align: center;
  color: #909399;
  padding: 40px 0;
}
.action-btn {
  padding: 5px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.detail-btn {
  background: #409eff;
  color: #fff;
}
.detail-btn:hover {
  background: #66b1ff;
}

/* 加载状态 */
.loading-mask {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  color: #909399;
}
.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 12px;
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 弹窗 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-content {
  background: #fff;
  border-radius: 8px;
  width: 600px;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}
.modal-header h3 {
  margin: 0;
  font-size: 16px;
}
.close-btn {
  font-size: 24px;
  cursor: pointer;
  color: #909399;
}
.close-btn:hover {
  color: #606266;
}
.modal-body {
  padding: 20px;
}
.detail-row {
  display: flex;
  margin-bottom: 15px;
  font-size: 14px;
}
.detail-row label {
  width: 120px;
  color: #909399;
  flex-shrink: 0;
}
.detail-row span {
  color: #333;
}
.intro-row {
  flex-direction: column;
}
.intro-row label {
  margin-bottom: 10px;
}
.intro-content {
  padding: 10px;
  background: #f5f7fa;
  border-radius: 4px;
  min-height: 100px;
}

/* 分页样式 */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 15px 0;
  border-top: 1px solid #eee;
}
.pagination-info {
  color: #666;
  font-size: 14px;
}
.pagination-controls {
  display: flex;
  align-items: center;
  gap: 5px;
}
.page-btn {
  padding: 6px 12px;
  background: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  color: #606266;
}
.page-btn:hover:not(:disabled) {
  color: #409eff;
  border-color: #c6e2ff;
}
.page-btn:disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}
.page-numbers {
  display: flex;
  gap: 5px;
}
.page-num {
  min-width: 32px;
  height: 28px;
  padding: 0 6px;
  background: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  color: #606266;
}
.page-num:hover {
  color: #409eff;
  border-color: #c6e2ff;
}
.page-num.active {
  background: #409eff;
  border-color: #409eff;
  color: #fff;
}
.page-size-selector select {
  padding: 5px 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 13px;
  margin-left: 10px;
}
.page-jump {
  font-size: 13px;
  color: #606266;
  margin-left: 10px;
}
.page-jump input {
  width: 50px;
  padding: 5px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  text-align: center;
  margin: 0 5px;
}
</style>
