<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 v-if="userRole === 'admin'" 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="分类名称" prop="name"></el-table-column>
        <el-table-column label="分类描述" prop="description"></el-table-column>
        <el-table-column label="父分类ID" prop="parentId" width="100"></el-table-column>
        <el-table-column label="操作" align="center" width="200" fixed="right">
          <template #default="scope">
            <el-button v-if="userRole === 'admin'" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button v-if="userRole === 'admin'" type="danger" plain @click="del(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.formTitle" v-model="data.formVisible" 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="name">
          <el-input v-model="data.form.name" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="分类描述" prop="description">
          <el-input v-model="data.form.description" type="textarea" :rows="3" placeholder="请输入分类描述"></el-input>
        </el-form-item>
        <el-form-item label="父分类ID" prop="parentId">
          <el-select v-model="data.form.parentId" placeholder="请选择父分类" style="width: 100%">
            <el-option label="无 (根分类)" :value="0"></el-option>
            <el-option v-for="item in data.categories" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog title="提出分类建议" v-model="data.suggestVisible" width="50%">
      <el-form :model="data.suggestForm" label-width="120px">
        <el-form-item label="分类名称" required>
          <el-input v-model="data.suggestForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="建议类型" required>
          <el-select v-model="data.suggestForm.type" placeholder="请选择建议类型" style="width: 100%">
            <el-option label="修改分类" value="modify"></el-option>
            <el-option label="新增子分类" value="add_child"></el-option>
            <el-option label="合并分类" value="merge"></el-option>
            <el-option label="其他" value="other"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="建议内容" required>
          <el-input v-model="data.suggestForm.content" type="textarea" :rows="4" placeholder="请详细描述您的建议内容"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.suggestVisible = false">取消</el-button>
          <el-button type="primary" @click="submitSuggestion">提交建议</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, onMounted } from "vue";
import request from "@/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

// 获取当前用户信息
const userInfo = JSON.parse(localStorage.getItem('xm-user') || '{}');
const userRole = userInfo.role || '';

const data = reactive({
  name: "",
  tableData: [],
  formVisible: false,
  formTitle: "",
  form: {
    parentId: 0
  },
  categories: [],
  total: 0,
  pageNum: 1,
  pageSize: 10,
  suggestVisible: false,
  suggestForm: {
    categoryId: null,
    name: '',
    type: '',
    content: ''
  },
  search: {}
});

// 加载数据
const load = () => {
  data.search.pageNum = data.pageNum;
  data.search.pageSize = data.pageSize;
  data.search.name = data.name;
  data.search.role = userRole;
  
  request
    .get("/resourceCategory/selectPage", {
      params: data.search
    })
    .then((res) => {
      if (res.code === "200") {
        data.tableData = res.data.list || [];
        data.total = res.data.total;
        
        // 获取所有分类用于选择父分类
        request.get("/resourceCategory").then((categoryRes) => {
          if (categoryRes.code === "200") {
            data.categories = categoryRes.data;
          }
        });
      } else {
        ElMessage.error(res.msg);
      }
    });
};

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

// 打开新增对话框
const handleAdd = () => {
  data.formVisible = true;
  data.formTitle = "新增分类";
  data.form = { parentId: 0 };
};

// 打开编辑对话框
const handleEdit = (row) => {
  data.formVisible = true;
  data.formTitle = "编辑分类";
  data.form = JSON.parse(JSON.stringify(row));
};

// 删除
const del = (id) => {
  ElMessageBox.confirm('确定要删除该分类吗？', '删除提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    request.delete("/resourceCategory/" + id).then((res) => {
      if (res.code === "200") {
        ElMessage.success("删除成功");
        load();
      } else {
        ElMessage.error(res.msg);
      }
    });
  }).catch(() => {});
};

// 保存
const save = () => {
  if (!data.form.name) {
    ElMessage.warning("请填写分类名称");
    return;
  }

  let method = data.form.id ? "put" : "post";

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

// 医生提出分类建议
const handleSuggest = (row) => {
  data.suggestVisible = true;
  data.suggestForm = {
    categoryId: row.id,
    name: row.name,
    type: '',
    content: ''
  };
};

// 提交分类建议
const submitSuggestion = () => {
  if (!data.suggestForm.type || !data.suggestForm.content) {
    ElMessage.warning("请填写完整的建议信息");
    return;
  }
  
  request.post("/resourceCategory/suggestion", data.suggestForm).then((res) => {
    if (res.code === "200") {
      ElMessage.success("分类建议提交成功");
      data.suggestVisible = false;
    } 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> 