<template>
  <div class="app-container">
    <!-- 搜索部分 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="性别" prop="gender">-->
<!--        <el-input-->
<!--          v-model="queryParams.gender"-->
<!--          placeholder="请输入性别"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="工作经验" prop="workExperience">-->
<!--        <el-input-->
<!--          v-model="queryParams.workExperience"-->
<!--          placeholder="请输入工作经验"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="所属医院" prop="hospital">
        <el-input
          v-model="queryParams.hospital"
          placeholder="请输入所属医院"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" 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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vet:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vet:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vet:info:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格部分 -->
    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="真实姓名" align="center" prop="realName" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="出生日期" align="center" prop="birthday" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idCard"  width="200"/>
      <el-table-column label="擅长领域" align="center" prop="specialty" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="工作经验" align="center" prop="workExperience"/>
      <el-table-column label="职称" align="center" prop="title" width="150"/>
      <el-table-column label="联系电话" align="center" prop="phone" width="200"/>
      <el-table-column label="电子邮箱" align="center" prop="email" width="200"/>
      <el-table-column label="专家类型" align="center" prop="expertType" width="150"/>
      <el-table-column label="所属医院" align="center" prop="hospital" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="联系地址" align="center" prop="address" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="个人简介" align="center" prop="introduction" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            class = "info-btn view-btn"
            @click="handleView(scope.row)"
            v-hasPermi="['vet:info:view']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="color: #42B983"
            class = "info-btn alter-btn"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vet:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: #f28888"
            class = "info-btn delete-btn"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vet:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改兽医个人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别" clearable>
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="职称" prop="title">
          <el-input v-model="form.title" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="专家类型" prop="expertType">
          <el-select v-model="form.expertType" placeholder="请选择专家类型" clearable>
            <el-option v-for="dict in dict.type.expert_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="擅长领域" prop="specialty">
          <el-input v-model="form.specialty" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作经验" prop="workExperience">
          <el-input v-model="form.workExperience" placeholder="请输入工作经验" />
        </el-form-item>
        <el-form-item label="所属医院" prop="hospital">
          <el-input v-model="form.hospital" placeholder="请输入所属医院" />
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入联系地址" />
        </el-form-item>
        <el-form-item label="个人简介" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="detailTitle" :visible.sync="detailVisible" width="80%" append-to-body>
      <div v-if="detailData" style="padding: 20px;">
        <!-- 显示兽医基本信息 -->
        <el-descriptions title="兽医基本信息" :column="2" border>
          <el-descriptions-item label="真实姓名">{{ detailData.realName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="性别">{{ detailData.gender || '-' }}</el-descriptions-item>
          <el-descriptions-item label="出生日期">{{ detailData.birthday || '-' }}</el-descriptions-item>
          <el-descriptions-item label="身份证号">{{ detailData.idCard || '-' }}</el-descriptions-item>
          <el-descriptions-item label="擅长领域">{{ detailData.specialty || '-' }}</el-descriptions-item>
          <el-descriptions-item label="工作经验">{{ detailData.workExperience ? detailData.workExperience + '年' : '-' }}</el-descriptions-item>
          <el-descriptions-item label="职称">{{ detailData.title || '-' }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ detailData.phone || '-' }}</el-descriptions-item>
          <el-descriptions-item label="电子邮箱">{{ detailData.email || '-' }}</el-descriptions-item>
          <el-descriptions-item label="专家类型">
            <dict-tag :options="dict.type.expert_type" :value="detailData.expertType"/>
          </el-descriptions-item>
          <el-descriptions-item label="所属医院">{{ detailData.hospital || '-' }}</el-descriptions-item>
          <el-descriptions-item label="联系地址" :span="2">{{ detailData.address || '-' }}</el-descriptions-item>
          <el-descriptions-item label="个人简介" :span="2">{{ detailData.introduction || '-' }}</el-descriptions-item>
        </el-descriptions>

        <!-- 显示资质证书信息 -->
        <div style="margin-top: 30px;">
          <h3 style="margin-bottom: 15px;color: #303133;font-weight: bold;">资质证书信息</h3>
          <el-alert
            v-if="!certificateList || certificateList.length === 0"
            title="暂无资质证书"
            type="info"
            :closable="false"
            style="margin-bottom: 20px;"
          />
          <el-table v-else :data="certificateList" border style="width: 100%">
<!--            <el-table-column prop="qualificationName" label="资质名称" align="center" width="180"></el-table-column>-->
            <el-table-column prop="certName" label="证书名称" align="center" width="180"></el-table-column>
            <el-table-column prop="certificateNo" label="证书编号" align="center" width="200"></el-table-column>
            <el-table-column prop="issueDate" label="颁发日期" align="center" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="expireDate" label="到期日期" align="center" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="auditStatus" label="审核状态" align="center" width="100">
              <template slot-scope="scope">
                <el-tag :type="getAuditStatusTagType(scope.row.auditStatus)">
                  {{ getAuditStatusText(scope.row.auditStatus) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="certStatus" label="证书状态" align="center" width="100">
              <template slot-scope="scope">
                <el-tag :type="getCertStatusTagType(scope.row.certStatus)">
                  {{ getCertStatusText(scope.row.certStatus) }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDetail">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, getfull, delInfo, addInfo, updateInfo } from "@/api/vet/info"

export default {
  name: "Info",
  dicts: ['expert_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 兽医个人信息表格数据
      infoList: [],
      // 资质证书列表
      certificateList: [],

      // 弹出层标题
      title: "",
      // 详情弹出层标题
      detailTitle: "",
      // 是否显示新增/修改弹出层
      open: false,
      // 是否显示详情弹出层
      detailVisible: false,
      // 详情数据
      detailData: null,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        realName: null,
        gender: null,
        birthday: null,
        idCard: null,
        specialty: null,
        workExperience: null,
        hospital: null,
        address: null,
        introduction: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    // 显示完善信息的提示
    if (this.$route.query.needCompleteInfo === 'true') {
      this.$message.warning('请完善您的个人信息');
      this.$router.replace({
        ...this.$route,
        query: {}
      });
    }
  },
  methods: {
    /** 查询兽医个人信息列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 关闭详情对话框
    closeDetail() {
      this.detailVisible = false
      this.detailData = null
      this.certificateList = []
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        realName: null,
        gender: null,
        birthday: null,
        idCard: null,
        specialty: null,
        workExperience: null,
        hospital: null,
        address: null,
        introduction: null,
        title: null,
        phone: null,
        email: null,
        expertType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.detailVisible = true
      this.detailTitle = "兽医详情信息"

      const id = row.id || this.ids
      getfull(id).then(response => {
        if (response.code === 200) {
          const data = response.data
          console.log('详情返回数据:', data)

          if (data.vetInfo) {
            this.detailData = data.vetInfo
            this.certificateList = data.qualifications || []
          } else if (data.personalInfo) {
            this.detailData = data.personalInfo
            this.certificateList = data.certificates || data.qualifications || []
          } else {
            this.detailData = data
            this.certificateList = data.qualifications || []
          }

          if (this.certificateList.length === 0) {
            console.log('没有证书数据')
          }
        } else {
          this.$message.error(response.msg || '获取详情失败')
        }
      }).catch(error => {
        console.error('获取详情失败:', error)
        this.$message.error('获取详情失败')
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加兽医个人信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改兽医个人信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除兽医个人信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    // 获取审核状态文本
    getAuditStatusText(status) {
      const statusMap = {
        '0': '待审核',
        '1': '审核通过',
        '2': '审核不通过'
      }
      return statusMap[status] || '未知'
    },

    // 获取审核状态标签类型
    getAuditStatusTagType(status) {
      const typeMap = {
        '0': 'warning',
        '1': 'success',
        '2': 'danger'
      }
      return typeMap[status] || 'info'
    },

    // 获取证书状态文本
    getCertStatusText(status) {
      const statusMap = {
        '0': '有效',
        '1': '即将过期',
        '2': '已过期'
      }
      return statusMap[status] || '未知'
    },

    // 获取证书状态标签类型
    getCertStatusTagType(status) {
      const typeMap = {
        '0': 'success',
        '1': 'warning',
        '2': 'danger'
      }
      return typeMap[status] || 'info'
    }
  }
}
</script>

<style scoped>
::v-deep .el-descriptions-item__cell{
  width: 90px;
}

::v-deep .pagestyle .el-input{
  width: auto !important;
}

::v-deep .el-descriptions-row th{
  text-align: center !important;color: #7b7c7f;
  background: #faf7f7;
}

::v-deep .el-descriptions .is-bordered .el-descriptions-item__cell {
  border: 1px solid #bcbbbb;
  padding: 12px 10px;
}
</style>

<style scoped lang="scss">
// 操作按钮样式
.info-btn {
  padding: 6px 10px;
  border-radius: 4px;
  margin: 0 10px;
  transition: all 0.3s ease;
}

.view-btn:hover {
  background-color: rgb(216, 238, 248);
  transform: translateY(-1px);
}

.alter-btn:hover{
  background-color: rgb(230, 255, 238);
  transform: translateY(-1px);
}

.delete-btn:hover {
  background-color: rgba(245, 108, 108, 0.1);
  transform: translateY(-1px);
}

// 新增/修改的弹窗
::v-deep .el-dialog {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  animation: dialogFadeIn 0.3s ease;
}

::v-deep .el-dialog__header {
  background: linear-gradient(135deg, #42b983 0%, #83df92 100%);
  padding: 18px 24px;
  border-bottom: none;
  position: relative;
}

::v-deep .el-dialog__title {
  font-size: 17px;
  font-weight: 600;
  color: white;
  letter-spacing: 0.5px;
}

::v-deep .el-dialog__headerbtn:hover .el-dialog__close {
  color: #ffd04b;
  transform: rotate(90deg);
}

::v-deep .el-dialog__body {
  padding: 28px 24px 20px;
  background-color: #f8fafc;
  max-height: 70vh;
  overflow-y: auto;
}

::v-deep .el-form-item {
  margin-bottom: 20px;
  transition: all 0.3s;
}

::v-deep .el-form-item__label {
  font-weight: 500;
  color: #2d3748;
  font-size: 14px;
  transition: color 0.3s;
}

::v-deep .el-input,
::v-deep .el-textarea,
::v-deep .el-select {
  width: 100%;
}

::v-deep .el-input__inner,
::v-deep .el-textarea__inner {
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  font-size: 14px;
  transition: all 0.3s;
  background-color: #fcfdfe;
}

::v-deep .el-input__inner:focus,
::v-deep .el-textarea__inner:focus {
  border-color: #42B983;
  box-shadow: 0 0 0 3px rgb(230, 255, 238);
  background-color: white;
}

::v-deep .el-select .el-input__inner {
  padding-right: 35px;
}

::v-deep .el-dialog__footer {
  padding: 20px 24px;
  background-color: #f8fafc;
  border-top: 1px solid #eef2f7;
  border-radius: 0 0 12px 12px;
}
</style>
