<template>
  <el-drawer
    v-model="drawerData.drawerShow"
    title="编辑文件夹"
    direction="rtl"
    :before-close="drawerData.handleClose"
  >
    <CustomInputTable
      :tableTitle="drawerData.tableTitle"
      :tableData="drawerData.tableData"
      @onAdd="drawerData.onAdd"
    >
      <template #default="scope">
        <el-button type="danger" size="small" @click="drawerData.delete(scope.$index)">删除</el-button>
      </template>
    </CustomInputTable>
  </el-drawer>
</template>

<script lang="ts">
import { reactive } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
export default {
  name: 'Folder',
  setup() {
    const tableData: {
      isLeaf: true;
      key: string;
      level: number;
      name: string;
      parentId: string;
      title: string;
      value: string;
    }[] = []
    const drawerData = reactive({
      drawerShow: false,
      tableTitle: [
        {
          lable: '文件夹',
          prop: 'name'
        }
      ],
      tableData: tableData,
      show: () => {
        drawerData.drawerShow = true
      },
      onAdd: (value: string) => {
        if (!value) {
          return false
        }
        drawerData.tableData.push({
          isLeaf: true,
          key: '',
          level: 0,
          name: value,
          parentId: '',
          title: value,
          value: ''
        })
      },
      delete: (index: number) => {
        ElMessageBox.confirm('确定删除吗？', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          drawerData.tableData.splice(index, 1)
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
        })
      }
    })
    return { drawerData }
  }
}
</script>

<style>

</style>
