<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="分类名称" prop="cateName">
        <el-input v-model="queryParams.cateName" placeholder="请输入分类名称" clearable style="width: 200px" @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="['slgc:docCate:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['slgc:docCate:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['slgc:docCate:remove']">删除</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" />
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="docCateList"
      row-key="cateCode"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="cateName" label="分类名称" min-width="260" />
      <el-table-column prop="cateDesc" label="分类描述" min-width="300" :show-overflow-tooltip="true" />
      <el-table-column prop="cateSeq" label="排序" width="100" />
      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['slgc:docCate:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['slgc:docCate:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['slgc:docCate:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24" v-if="form.catePcode !== undefined">
            <el-form-item label="上级分类" prop="catePcode">
              <el-tree-select
                v-model="form.catePcode"
                :data="cateOptions"
                :props="{ value: 'cateCode', label: 'cateName', children: 'children' }"
                value-key="cateCode"
                placeholder="选择上级分类"
                check-strictly
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="分类名称" prop="cateName">
              <el-input v-model="form.cateName" placeholder="请输入分类名称" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="分类描述" prop="cateDesc">
              <el-input v-model="form.cateDesc" type="textarea" placeholder="请输入分类描述" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="cateSeq">
              <el-input-number v-model="form.cateSeq" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
      </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="DocCate">
import { listDocCate, getDocCate, addDocCate, updateDocCate, delDocCate } from "@/api/slgc/docCate"

const { proxy } = getCurrentInstance()

const docCateList = ref([])
const cateOptions = 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 ids = ref([])
const single = ref(true)
const multiple = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    cateName: undefined
  },
  rules: {
    cateName: [{ required: true, message: "分类名称不能为空", trigger: "blur" }]
  }
})

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

function getList() {
  loading.value = true
  listDocCate(queryParams.value).then(response => {
    docCateList.value = proxy.handleTree(response.data, "catePcode")
    loading.value = false
  })
}

function handleQuery() {
  getList()
}

function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

function toggleExpandAll() {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

function handleAdd(row) {
  reset()
  listDocCate().then(response => {
    cateOptions.value = proxy.handleTree(response.data, "cateCode")
  })
  if (row != undefined && row.cateCode) {
    form.value.catePcode = row.cateCode
  }
  open.value = true
  title.value = "添加文档分类"
}

function handleUpdate(row) {
  reset()
  const cateCode = row.cateCode || ids.value
  listDocCate().then(response => {
    cateOptions.value = proxy.handleTree(response.data, "cateCode")
  })
  getDocCate(cateCode).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改文档分类"
  })
}

function handleDelete(row) {
  const cateCodes = row.cateCode || ids.value
  proxy.$modal.confirm('是否确认删除分类编号为"' + cateCodes + '"的数据项？').then(function() {
    return delDocCate(cateCodes)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.cateCode)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function reset() {
  form.value = {
    cateCode: undefined,
    catePcode: undefined,
    cateName: undefined,
    cateDesc: undefined,
    cateSeq: 0
  }
  proxy.resetForm("formRef")
}

function cancel() {
  open.value = false
  reset()
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.cateCode != undefined) {
        updateDocCate(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDocCate(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

onMounted(() => {
  getList()
})
</script>
