Newer
Older
KaiFengPC / src / views / document / loanManagement / loanRequest / operate.vue
@zhangdeliang zhangdeliang on 23 May 4 KB 初始化项目
<template>
  <div class="operate">
    <el-form ref="ruleForm" :model="form" :rules="rules" class="dialogForm">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="借阅时长:" prop="borrowDays">
            <el-input v-model="form.borrowDays" type="text">
              <template #append>天</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="借阅原因:" prop="borrowReason">
            <el-input v-model="form.borrowReason" type="textarea" placeholder="请输入借阅原因" resize="none" :rows="4" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table ref="multipleTableRef" :data="tableData" v-loading="loading" stripe :max-height="500">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="55" label="序号" />
      <el-table-column label="文件名称" prop="fileName" show-overflow-tooltip />
      <el-table-column label="上传时间" prop="createTime" show-overflow-tooltip />
      <el-table-column label="借阅方式" prop="saveType" show-overflow-tooltip>
        <template #default="{ row }">
          <el-checkbox-group v-model="row.borrowType">
            <el-checkbox v-for="borrowType in row.borrowTypeList" :label="borrowType.value" :disabled="borrowType.value === 'electronic'">{{
              borrowType.text
            }}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { documentTypeTree } from '@/api/document/dataClassify';
import { getDocumentFileAllowBorrowList, documentBorrowRecordAdd } from '@/api/document/loanManagement/loanRequest';
import { required } from '@/utils/validate-helper';
const emit = defineEmits(['close']);
const { proxy } = getCurrentInstance();
const borrowTypeMap = new Map([
  ['electronic', '电子档'],
  ['paper', '纸质原件'],
]);
const form = reactive({
  typeId: '',
  fileName: '',
  saveType: '',
  borrowDays: '',
  borrowReason: '',
});
const rules = reactive({
  saveType: required('借阅方式'),
  borrowReason: required('借阅原因'),
  borrowDays: required('借阅天数'),
});
const treeData = ref([]);
const tableData = ref([]);
const loading = ref(false);

const getTreeData = async () => {
  const res = await documentTypeTree();
  if (res?.code !== 200) return;
  treeData.value = res.data || [];
};

const search = () => {
  getTableList();
};

const resetQuery = () => {
  proxy.$refs.ruleForm.resetFields();
  search();
};

const submit = () => {
  proxy.$refs.ruleForm.validate(async (valid, fields) => {
    if (valid) {
      const checkedList = proxy.$refs.multipleTableRef.getSelectionRows();
      if (!checkedList.length) return proxy.$modal.msgError('请至少选择一个文件');
      console.log(checkedList);
      const files = checkedList.map(it => ({
        fileId: it.id,
        borrowType: it.borrowType.join(),
      }));
      const params = {
        borrowDays: form.borrowDays,
        borrowReason: form.borrowReason,
        files,
      };
      console.log(params);
      const res = await documentBorrowRecordAdd(params);
      if (res?.code !== 200) return;
      proxy.$modal.msgSuccess('操作成功');
      emit('close', { isRefresh: true });
    } else {
      console.log('error submit!', fields);
    }
  });
};

const getTableList = async () => {
  const { typeId, fileName, saveType } = form;
  const res = await getDocumentFileAllowBorrowList({ typeId, fileName, saveType });
  if (res?.code !== 200) return;
  for (const item of res.data) {
    if (item.saveType) {
      item.borrowTypeList = item.saveType.split(',').map(it => ({
        text: borrowTypeMap.get(it),
        value: it,
      }));
      item.borrowType = ['electronic'];
    } else {
      item.borrowTypeList = [];
      item.borrowType = [];
    }
  }
  tableData.value = res.data;
};

onMounted(() => {
  getTreeData();
  getTableList();
});

defineExpose({
  submit,
});
</script>

<style lang="scss" scoped></style>