<template>
  <div class="body">
    <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">
        <FilesUpload
          v-model:src-list="srcList"
          v-model:pdf-list="pdfUrlList"
          v-model:file-name-list="fileNameList"
          :limit="1"
          :generate-pdf="true"
          @finished="testFiles"
        />
      </template>
      <template #enable="scope">
        <el-switch
          active-value="1"
          inactive-value="0"
          inline-prompt
          active-text="启用"
          inactive-text="禁用"
          v-model="scope.row.enable"
          @click="changeItem(scope.row)"
          class="ml-2"
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
        />
      </template>
      <template #menu="{ row, index, type }">
        <PDFView
          :need-to-show-pdf="true"
          content-type="button"
          :is-link="true"
          :pdf-source="filePath + row.pdfPath"
        />
        <el-button
          @click="deleteRecord(row, index, done)"
          text
          icon="el-icon-delete"
          type="primary"
          >删除</el-button
        >
      </template>
    </avue-crud>

    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
  <CommonTable
    ref="ctableRef"
    tableTitle="BOM添加"
    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";
const operationId = inject("operationId");
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
// 数据字典相关
const { dicts } = useDictionaryStore();
const fileUrl = ref(""); //单文件
const pdfUrlList = ref([]);
const srcList = ref([]);
const fileNameList = ref([]);
const testFiles = () => {
  form.value.fileName = fileNameList.value[0];
  form.value.filePath = srcList.value[0];
  form.value.operationId = operationId.value;
  form.value.pdfPath = 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();
  });
};

// 传入一个url，后面不带/
const {
  form,
  data,
  option,
  search,
  page,
  toDeleteIds,
  Methords,
  Utils,
  commonConfig,
} = useCrud({
  src: "/api/v1/operation/test/record",
});
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?.(() => {
  // console.log("crudRef", crudRef)
  commonConfig.value.params = {
    operationId: operationId.value,
  };
  dataEditList();
});
const addRow = (form2, done) => {
  createRow(form, done, done);
  pdfUrlList.value = [];
  srcList.value = [];
  fileNameList.value = [];
};
const onSelectedFinish = (selectedValue) => {
  form.value.associationCode = selectedValue.materialCode;
  form.value.associationName = selectedValue.materialName;
  form.value.materialCode = selectedValue.materialCode;
};

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

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: false,
  viewBtn: false,
  searchBtn: false,
  editBtn: false,
  delBtn: false,
  menu: true,
  column: [
    {
      label: "文件名",
      prop: "fileName",
      span: 24,
      slot: true,
      display: false,
    },
    {
      label: "文件",
      prop: "filePath",
      span: 24,
      slot: true,
      hide: true,
      rules: [
        {
          required: true,
          message: "请选择文件",
          trigger: "blur",
        },
      ],
    },
  ],
});

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