<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-save="addRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #filePath-form="scope">
        <!--        <single-upload v-model="form.drawingPath" :generatePdf="true"/>-->
        <FilesUpload
          v-model:src-list="srcList"
          v-model:pdf-list="pdfUrlList"
          v-model:file-name-list="fileNameList"
          :generate-pdf="true"
          @finished="testFiles"
        />
      </template>
      <template #menu-left="{ row, index, type }">
        <el-button @click="toSyncPdmFiles()" type="success">同步PDM</el-button>
      </template>
      <template #menu="{ row, index, type }">
        <PDFView
          :need-to-show-pdf="true"
          content-type="button"
          :is-link="true"
          :pdf-source="setSource(row)"
        />
        <el-button @click="deleteRecord(row, index, done)" text type="primary"
          >删除</el-button
        >
      </template>
    </avue-crud>

    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
  <CommonTable
    ref="ctableRef"
    tableTitle="文档添加"
    tableType="MARTERIAL"
    @selected-sure="onSelectedFinish"
  />
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import { useCommonStoreHook, useDictionaryStore } from "@/store";
import { updateDrawaing } from "@/api/drawing";
import dictDataUtil from "@/common/configs/dictDataUtil";
import PDFView from "@/components/PDFView/index.vue";
import { syncPdmFiles } from "@/api/document/index.ts";

const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const setSource = (row) => {
  if (row.sourceType == 1) {
    return row.filePath;
  } else if (row.sourceType == 2) {
    return filePath + row.filePath;
  }
};
const SyncPdmCode = ref("");
const SyncPdmStatus = ref(false);
//同步
const toSyncPdmFiles = () => {
  SyncPdmStatus.value = true;
  ctableRef.value.startSelect();
};
// 数据字典相关
const { dicts } = useDictionaryStore();
const fileUrl = ref(""); //单文件
const pdfUrlList = ref([]);
const srcList = ref([]);
const fileNameList = ref([]);
const testFiles = () => {
  form.value.filePath = pdfUrlList.value[0];
};
const filePath = import.meta.env.VITE_APP_UPLOAD_URL;
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const ctableRef = ref(null);
const changeItem = (row) => {
  updateDrawaing(row).then(() => {
    ElMessage.success("操作成功");
    dataList();
  });
};
const form2 = ref(null);
const addRow = (form2, done) => {
  createRow(form, done, done);
  pdfUrlList.value = [];
  srcList.value = [];
  fileNameList.value = [];
};
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/processFile",
  });
const {
  dataEditList,
  createRow,
  updateRow,
  deleteRow,
  searchChange,
  dataList,
  resetChange,
} = Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
// checkBtnPerm(ButtonPermKeys.PLAN.BTNS.order_add) :permission="permission"
// const permission = reactive({
//   delBtn: checkPerm(buttonPermission.PLAN.BTNS.order_del),
//   addBtn: checkPerm(buttonPermission.PLAN.BTNS.order_add),
//   editBtn: checkPerm(buttonPermission.PLAN.BTNS.order_edit),
//   menu: true,
// });

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

onMounted?.(() => {
  dataEditList();
});

const onSelectedFinish = async (selectedValue) => {
  form.value.materialCode = selectedValue.materialCode;
  SyncPdmCode.value = selectedValue.materialCode;
  form.value.operationId = 1;
  if (SyncPdmStatus.value == true) {
    try {
      const { code } = await syncPdmFiles(SyncPdmCode.value);
      if (code == "200") {
        ElMessage.success("同步成功");
        dataList();
      }
    } catch (e) {
      ElMessage.error(e);
    } finally {
      SyncPdmStatus.value = false;
    }
  }
};

/**
 * 上传excel相关
 */
const uploadRef = ref(null);
const uploadFinished = () => {
  // 上传完成后的刷新操作
  page.currentPage = 1;
  dataEditList();
};

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: false,
  viewBtn: false,
  editBtn: false,
  delBtn: false,
  menu: true,
  column: [
    {
      label: "文档名称",
      prop: "fileName",
      search: true,
      overHidden: true,
      rules: [
        {
          required: true,
          trigger: "change",
        },
      ],
    },
    {
      label: "文档编码",
      prop: "fileCode",
      search: true,
      overHidden: true,
      rules: [
        {
          required: true,
          trigger: "change",
        },
      ],
    },
    {
      label: "物料编码",
      prop: "materialCode",
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,

          trigger: "change",
        },
      ],
      click({ value, column }) {
        if (column.rules) {
          ctableRef.value.startSelect();
        }
      },
    },
    {
      label: "来源类型",
      prop: "sourceType",
      search: true,
      overHidden: true,
      editDisplay: false,
      type: "select",
      addDisabled: true,
      rules: [
        {
          required: true,
          trigger: "blur",
        },
      ],
      dicData: [
        {
          label: "PDM同步",
          value: 1,
        },
        {
          label: "手动维护",
          value: 2,
        },
      ],
      value: 2,
    },
    {
      label: "文件地址",
      prop: "filePath",
      span: 24,
      slot: true,
      hide: true,
      rules: [
        {
          required: true,
          trigger: "change",
        },
      ],
    },

    {
      label: "创建时间",
      prop: "created",
      width: 120,
      overHidden: true,
      display: false,
    },
  ],
});

const deleteRecord = (row, index, done) => {
  deleteRow(row, index, done);
  dataEditList();
};
</script>
