<template>
  <div class="common-diagnose-page">
    <!-- 搜索区域 -->
    <el-card shadow="never" class="search-card">
      <el-form :inline="true" :model="searchForm" @submit.native.prevent>
        <el-form-item label="诊断内容">
          <el-input
            v-model="searchForm.diagnose"
            placeholder="请输入诊断内容关键词"
            clearable
            @keyup.enter.native="fetchList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchList">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮 -->
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增常用诊断</el-button>
    </div>

    <!-- 诊断列表 -->
    <el-card shadow="never" class="table-card">
      <el-table
        :data="tableData"
        v-loading="loading"
        border
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
        <el-table-column prop="diagnose" label="诊断内容" min-width="300" show-overflow-tooltip></el-table-column>

        <el-table-column label="操作" width="160" fixed="right" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" size="small" style="color: #f56c6c" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-if="total > 0"
        class="pagination"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size.sync="pageSize"
        :current-page.sync="pageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="600px"
      @close="resetForm"
    >
      <el-form
        ref="diagnoseForm"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="诊断内容" prop="diagnose">
          <el-input
            type="textarea"
            :rows="4"
            v-model="form.diagnose"
            placeholder="请输入诊断内容，如：急性上呼吸道感染，建议休息、多饮水..."
            maxlength="500"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCommonDiagnoseList,
  addCommonDiagnose,
  editCommonDiagnose,
  deleteCommonDiagnose
} from '@/api/commonlyDiagnose'

export default {
  name: 'CommonDiagnose',
  data() {
    return {
      // 分页
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      tableData: [],

      // 搜索
      searchForm: {
        diagnose: ''
      },

      // 弹窗
      dialogVisible: false,
      isEdit: false,
      form: {
        commonDiagnoseId: null,
        diagnose: ''
      },
      rules: {
        diagnose: [
          { required: true, message: '请输入诊断内容', trigger: 'blur' },
          { max: 500, message: '最多500个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.isEdit ? '编辑常用诊断' : '新增常用诊断'
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    async fetchList() {
      this.loading = true
      try {
        const res = await getCommonDiagnoseList({
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          diagnose: this.searchForm.diagnose || undefined
        })
        const page = res.data
        this.tableData = Array.isArray(page.list) ? page.list : []
        this.total = page.total || 0
      } catch (error) {
        this.tableData = []
        this.total = 0
      } finally {
        this.loading = false
      }
    },

    resetSearch() {
      this.searchForm.diagnose = ''
      this.pageNum = 1
      this.fetchList()
    },

    handleSizeChange(val) {
      this.pageSize = val
      this.fetchList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchList()
    },

    handleAdd() {
      this.isEdit = false
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.diagnoseForm.resetFields()
      })
    },

    handleEdit(row) {
      this.isEdit = true
      this.form = { ...row }
      this.dialogVisible = true
    },

    handleDelete(row) {
      this.$confirm(`确定删除该诊断？`, '提示', {
        type: 'warning'
      }).then(async () => {
        await deleteCommonDiagnose(row.commonDiagnoseId)
        this.$message.success('删除成功')
        this.fetchList()
      }).catch(() => {})
    },

    submitForm() {
      this.$refs.diagnoseForm.validate(async (valid) => {
        if (!valid) return
        try {
          if (this.isEdit) {
            await editCommonDiagnose(this.form)
            this.$message.success('编辑成功')
          } else {
            await addCommonDiagnose(this.form)
            this.$message.success('新增成功')
          }
          this.dialogVisible = false
          this.fetchList()
        } catch (error) {
          console.error(error)
        }
      })
    },

    resetForm() {
      this.$refs.diagnoseForm.resetFields()
      this.form = {
        commonDiagnoseId: null,
        diagnose: ''
      }
    }
  }
}
</script>

<style scoped>
.common-diagnose-page {
  padding: 20px;
}
.search-card {
  margin-bottom: 16px;
}
.toolbar {
  margin-bottom: 16px;
  display: flex;
  justify-content: flex-start;
}
.table-card {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
