<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字典文本" prop="dictText">
        <el-input
          v-model="queryParams.dictText"
          placeholder="请输入字典文本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典值" prop="dictValue">
        <el-input
          v-model="queryParams.dictValue"
          placeholder="请输入字典值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父ID" prop="pid">
        <el-input
          v-model="queryParams.pid"
          placeholder="请输入父ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="dictSort">
        <el-input
          v-model="queryParams.dictSort"
          placeholder="请输入排序"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @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="Plus"
          @click="handleAdd"
          v-hasPermi="['system:treeDict:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="treeDictList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="字典文本" prop="dictText" />
      <el-table-column label="字典值" align="center" prop="dictValue" />
      <el-table-column label="父ID" align="center" prop="pid" />
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column label="排序" align="center" prop="dictSort" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:treeDict:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:treeDict:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:treeDict:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改树形字典对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="treeDictRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典文本" prop="dictText">
          <el-input v-model="form.dictText" placeholder="请输入字典文本" />
        </el-form-item>
        <el-form-item label="字典值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入字典值" />
        </el-form-item>
        <el-form-item label="父ID" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入父ID" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="排序" prop="dictSort">
          <el-input v-model="form.dictSort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TreeDict">
import { listTreeDict, getTreeDict, delTreeDict, addTreeDict, updateTreeDict } from "@/api/system/treeDict";

const { proxy } = getCurrentInstance();

const treeDictList = ref([]);
const treeDictOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    dictText: null,
    dictValue: null,
    pid: null,
    deptId: null,
    dictSort: null,
    status: null,
  },
  rules: {
    dictText: [
      { required: true, message: "字典文本不能为空", trigger: "blur" }
    ],
    dictValue: [
      { required: true, message: "字典值不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询树形字典列表 */
function getList() {
  loading.value = true;
  listTreeDict(queryParams.value).then(response => {
    treeDictList.value = proxy.handleTree(response.data, "id", "id");
    loading.value = false;
  });
}

/** 查询树形字典下拉树结构 */
function getTreeselect() {
  listTreeDict().then(response => {
    treeDictOptions.value = [];
    const data = { id: 0, dictText: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "id", "id");
    treeDictOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    dictText: null,
    dictValue: null,
    pid: null,
    deptId: null,
    dictSort: null,
    status: null,
    remark: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    createTime: null
  };
  proxy.resetForm("treeDictRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.id = row.id;
  } else {
    form.value.id = 0;
  }
  open.value = true;
  title.value = "添加树形字典";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.id = row.id;
  }
  getTreeDict(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改树形字典";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["treeDictRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTreeDict(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTreeDict(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除树形字典编号为"' + row.id + '"的数据项？').then(function() {
    return delTreeDict(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
