<template>
  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input v-model="data.name" prefix-icon="Search" style="width: 240px; margin-right: 10px; margin-bottom: 3px" placeholder="请输入医生姓名"></el-input>
      <el-button type="info" plain @click="load">查询</el-button>
      <el-button type="warning" plain style="margin-right: 10px" @click="reset">重置</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px;">
      <el-table :data="data.tableData" stripe>
        <el-table-column label="ID" prop="id" width="80"></el-table-column>
        <el-table-column label="账号" prop="username"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="手机号" prop="phone"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="执业证书编号" prop="certificateNo"></el-table-column>
        <el-table-column label="所属医院" prop="hospital"></el-table-column>
        <el-table-column label="科室" prop="department"></el-table-column>
        <el-table-column label="操作" align="center" width="200" fixed="right">
          <template v-slot="scope">
            <el-button type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" plain @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="card" v-if="data.total">
      <el-pagination 
        @current-change="load" 
        background 
        layout="total, prev, pager, next" 
        :page-size="10" 
        v-model:current-page="data.pageNum" 
        :total="data.total"/>
    </div>

    <el-dialog :title="data.dialogTitle" v-model="data.dialogVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="data.form" label-width="120px" style="padding: 20px 30px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="data.form.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="data.isAdd">
          <el-input v-model="data.form.password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="data.form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="data.form.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="data.form.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="data.form.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="执业证书编号" prop="certificateNo">
          <el-input v-model="data.form.certificateNo" placeholder="请输入执业证书编号"></el-input>
        </el-form-item>
        <el-form-item label="所属医院" prop="hospital">
          <el-input v-model="data.form.hospital" placeholder="请输入所属医院"></el-input>
        </el-form-item>
        <el-form-item label="科室" prop="department">
          <el-input v-model="data.form.department" placeholder="请输入科室"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, onMounted } from "vue";
import { Search, Plus, Edit, Delete, Refresh } from '@element-plus/icons-vue';
import request from "@/utils/request";
import { ElMessage } from "element-plus";

const data = reactive({
  name: "",
  tableData: [],
  total: 0,
  pageNum: 1,
  pageSize: 10,
  dialogVisible: false,
  dialogTitle: "",
  isAdd: false,
  form: {},
  search: {}
});

// 加载数据
const load = () => {
  data.search.pageNum = data.pageNum;
  data.search.pageSize = 10;
  data.search.name = data.name;
  
  request
    .get("/doctor/selectPage", {
      params: data.search
    })
    .then((res) => {
      if (res.code === "200") {
        data.tableData = res.data.list || [];
        data.total = res.data.total;
      } else {
        ElMessage.error(res.msg);
      }
    });
};

// 重置查询条件
const reset = () => {
  data.name = "";
  load();
};

// 打开新增对话框
const handleAdd = () => {
  data.dialogVisible = true;
  data.dialogTitle = "新增医生";
  data.form = { sex: "男" };
  data.isAdd = true;
};

// 打开编辑对话框
const handleEdit = (row) => {
  data.dialogVisible = true;
  data.dialogTitle = "编辑医生";
  data.form = JSON.parse(JSON.stringify(row));
  data.isAdd = false;
};

// 删除
const handleDelete = (id) => {
  request.delete("/doctor/" + id).then((res) => {
    if (res.code === "200") {
      ElMessage.success("删除成功");
      load();
    } else {
      ElMessage.error(res.msg);
    }
  });
};

// 保存
const save = () => {
  if (!data.form.username || (data.isAdd && !data.form.password) || !data.form.name) {
    ElMessage.warning("请填写完整信息");
    return;
  }

  let url = data.isAdd ? "/doctor" : "/doctor";
  let method = data.isAdd ? "post" : "put";

  // 确保表单中包含角色信息
  data.form.role = "doctor";

  request[method](url, data.form).then((res) => {
    if (res.code === "200") {
      ElMessage.success("保存成功");
      data.dialogVisible = false;
      load();
    } else {
      ElMessage.error(res.msg);
    }
  });
};

onMounted(() => {
  load();
});
</script>

<style scoped>
.card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
  background-color: #fff;
  color: #303133;
  margin: 10px 0;
  padding: 10px;
  border-radius: 6px;
}
</style> 