<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model="form"
      v-model:page="page"
      v-model:search="search"
      :data="data"
      :option="option"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @row-save="createRow"
      @row-del="deleteRow"
      @row-update="updateRow"
      @selelction-change="selectionChange"
    >
      <template #fileUrl-form="scope">
        <FilesUpload
          v-model:src="form.fileUrl"
          v-model:file-name-list="filesNamesList"
          :showTip="false"
          :limit="1"
          :size="3000"
          @finished="onUploadFinish"
        />
      </template>
    </avue-crud>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useCrud } from "@/hooks/userCrud";
import {
  addSystemUserMenu,
  bindedRoleMenuList,
  bindRoleMenuList,
  systemMenuList,
  systemUserMenuList,
  treeList,
} from "@/api/user/index";
import SingleUpload from "@/components/Upload/SingleUpload.vue";
import FilesUpload from "@/components/Upload/FilesUpload.vue";
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/synFileShare",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef = ref(null); //crudRef.value 获取avue-crud对象

const deptList = ref([]); //部门列表
const getDeptList = async () => {
  let res = await treeList();
  if (res && res.code == 200) {
    deptList.value = res.data ?? []; //获取部门列表
  }
};

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  editBtn: true, //是否显示编辑按钮
  delBtn: true, //是否显示删除按钮
  addBtn: true, //是否显示新增按钮
  viewBtn: false, //是否显示查看按钮
  menu: true, //是否显示操作列
  column: [
    {
      label: "文件名",
      prop: "fileName",
      search: true,
      display: false,
    },
    {
      label: "文件标题",
      prop: "fileTitle",
      search: true,
    },
    {
      label: "描述",
      prop: "remark",
      search: false,
    },
    {
      label: "文件上传",
      prop: "fileUrl",
      slot: true,
      hide: true,
    },
    {
      label: "创建人",
      prop: "creator",
      search: false,
      display: false,
    },
    {
      label: "创建时间",
      prop: "created",
      search: false,
      display: false,
    },
  ],
});

onMounted(() => {
  getDeptList(); //获取部门列表
  dataList();
});

// 测试上传文件相关
const fileUrl = ref(""); //单文件
const filesNamesList = ref([]);
const onUploadFinish = () => {
  console.log("fileUrl", fileUrl.value, filesNamesList.value);
  if (filesNamesList.value.length > 0) {
    form.value.fileName = filesNamesList.value[0];
  }
  console.log(form.value);
};
</script>
