<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <!-- <TableAction :actions="getTableAction(record)" /> -->
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <template #tableTitle>
        <el-button type="primary" round @click="handleAdd" v-if="isShowByRoles('manageOrg')">新增信息材料</el-button>
        <el-button type="primary" round @click="handleBatchdownload"> 批量导出</el-button>
      </template>
    </BasicTable>
    <addAndModify @register="registerSubmitProjectArchive" @close="closeModel" />

  </div>
</template>

<script lang="ts" name="system-user" setup>
//ts语法
import { ActionItem, BasicTable, TableAction, useTable } from '@/components/Table';
import { useModal } from '@/components/Modal';
import { downloadResource } from "../../../api/common/api"
import { isShowByRoles } from '@/views/projectLib/projectInfo/projectInfo.api';
import { mechanismColumns, searchFormSchema } from './mechanism.data';
import addAndModify from "./addAndModify.vue"
import { informationMaterialPageList, batchdownloadInformationMaterialFiles, deleteInformationMaterial } from './mechanism.api';
const [registerSubmitProjectArchive, { openModal }] = useModal();
const [registerTable, { reload, getForm }] = useTable({
  title: '机制建设',
  api: informationMaterialPageList,
  columns: mechanismColumns,
  useSearchForm: true,
  actionColumn: {
    width: 140,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
  },
  //表单查询项设置
  formConfig: {
    schemas: searchFormSchema,
  }
});


function getTableAction(record): ActionItem[] {
  return [
    {
      label: '下载',
      onClick: handledown.bind(null, record),
    }

  ];
}
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '修改',
      ifShow: () => {
        return isShowByRoles('manageOrg')
      },
      onClick: handleModify.bind(null, record)
    },
    {
      label: '删除',
      ifShow: () => {
        return isShowByRoles('manageOrg')
      },
      popConfirm: {
        title: '确定删除吗?',
        confirm: handleDelete.bind(null, record),
      },
    }

  ];
}
function handleAdd() {
  openModal(true, { id: null })
}
function handleModify(record) {
  openModal(true, { id: record.id })

}
function handledown(record) {
  let param = {
    path: record.documentPath,
    fileName: record.documentName
  }
  downloadResource("/huzhouUploadfileinfo/downloadfile", record.documentName, param)

}
async function handleDelete(record) {
  await deleteInformationMaterial({ id: record.id })
  reload()
}
function handleBatchdownload() {
  let { getFieldsValue } = getForm()
  let fromData = getFieldsValue()
  batchdownloadInformationMaterialFiles(fromData)
}
function closeModel() {
  reload()
}

</script>

<style scoped></style>