<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"
      :before-open="onCrudBeforOpen"
    >
      <template #fileUrl-form="scope">
        <FilesUpload
          ref="filesUploadRef"
          v-model:src="photoUrlList"
          v-model:file-name-list="photoNameList"
          :showTip="false"
          :limit="1"
          :size="3000"
          @finished="onUploadFinish"
        />
      </template>
      <template #menu="{ row }">
        <el-button v-if="row.state == 1" style="color: red" link @click="sendMsg(row)">
          <el-icon>
            <Delete/>
          </el-icon>
          过期
        </el-button>
      </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";
import {endMessage} from "@/api/message/index";
// 传入一个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 fileList = [];
const filesUploadRef = ref();
const onCrudBeforOpen = (done, type) => {
  done();
  if (type === "edit") {
    nextTick(() => {
      filesUploadRef.value &&
      filesUploadRef.value.setFileList([{name: form.value.fileName}]);
    });
  }
};
const photoUrlList = ref([]);
const photoNameList = ref([]);
const deptList = ref([]); //部门列表
const getDeptList = async () => {
  let res = await treeList();
  if (res && res.code == 200) {
    deptList.value = res.data ?? []; //获取部门列表
  }
};
const sendMsg = (row) => {
  endMessage(row.id).then(() => {
    dataList();
  });
};
// 设置表格列或者其他自定义的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,
      rules: [
        {
          required: true,
          message: "请输入文件标题",
          trigger: "blur",
        },
      ],
    },
    {
      label: "描述",
      prop: "remark",
      search: false,
    },
    {
      label: "是否过期",
      prop: "state",
      search: false,
      type: "radio", //类型为单选框
      dicData: [
        {
          label: "过期",
          value: 0,
        },
        {
          label: "正常",
          value: 1,
        },
      ],
      value: 1,
    },
    {
      label: "文件上传",
      prop: "fileUrl",
      slot: true,
      hide: true,
      rules: [
        {
          required: true,
          message: "请选择文件",
          trigger: "blur",
        },
      ],
    },
    {
      label: "创建人",
      prop: "creator",
      search: false,
      display: false,
    },
    {
      label: "创建时间",
      prop: "created",
      search: false,
      display: false,
    },
  ],
});

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

// 测试上传文件相关
const onUploadFinish = () => {
  if (photoUrlList.value.length > 0) {
    form.value.fileUrl = photoUrlList.value[0];
    form.value.fileName = photoNameList.value[0];
    console.log("上传成功", photoUrlList);
    console.log("上传成功2", photoNameList);
  } else {
    form.value.fileUrl = null;
    form.value.fileName = null;
  }
};
</script>
