<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="createRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #drawingPath-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"
          :limit="10"
          :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 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 lang="ts">
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 { isShowTable, tableType } = toRefs(useCommonStoreHook());
// 数据字典相关
const { dicts } = useDictionaryStore();
const fileUrl = ref(""); //单文件
const pdfUrlList = ref([]);
const srcList = ref([]);
const fileNameList = ref([]);
const testFiles = () => {
  form.value.pdfPathList = pdfUrlList.value;
  form.value.drawingPathList = srcList.value;
  form.value.drawingPath = srcList.value[0];
  form.value.fileNameList = fileNameList.value;
};
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 } =
  useCrud({
    src: "/api/v1/base/drawing",
  });
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)
  dataEditList();
});

const onSelectedFinish = (selectedValue) => {
  form.value.associationCode = selectedValue.materialCode;
  form.value.associationName = selectedValue.materialCode;
  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,
  editBtn: false,
  delBtn: false,
  menu: true,
  column: [
    {
      label: "图纸编号",
      prop: "drawingCode",
      width: 150,
      search: true,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "请填写图纸编号",
          trigger: "blur",
        },
      ],
    },
    {
      label: "图纸名称",
      prop: "drawingTitle",
      width: 150,
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,
          message: "请填写图纸名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "物料编号",
      prop: "associationCode",
      width: 150,
      search: true,
      overHidden: true,
      editDisplay: false,
      addDisplay: false,
      rules: [
        {
          required: true,
          message: "请填写物料编号",
          trigger: "blur",
        },
      ],
    },

    {
      label: "物料编号",
      prop: "materialCode",
      width: 150,
      overHidden: true,
      editDisplay: false,
      hide: true,
      rules: [
        {
          required: true,
          message: "请填写物料编号",
          trigger: "blur",
        },
      ],
      click() {
        if (ctableRef.value) {
          ctableRef.value.startSelect();
        }
      },
    },
    {
      label: "物料名称",
      prop: "associationName",
      width: 180,
      search: true,
      overHidden: true,
      disabled: true,
      rules: [
        {
          required: true,
          message: "请填写物料名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "图纸类型",
      prop: "drawingDictValue",
      search: true,
      filterable: true,
      type: "select",
      width: 100,
      overHidden: true,
      dicUrl: dictDataUtil.request_url + "drawing_type",
      props: { label: "dictLabel", value: "dictValue" },
      rules: [
        {
          required: true,
          message: "请选择物料属性",
          trigger: "blur",
        },
      ],
    },
    {
      label: "文件",
      prop: "drawingPath",
      span: 24,
      slot: true,
      hide: true,
      rules: [
        {
          required: true,
          message: "请选择文件",
          trigger: "blur",
        },
      ],
    },
    {
      label: "文件名称",
      prop: "fileName",
      span: 24,
      width: 120,
      overHidden: true,
      display: false,
    },
    {
      label: "文件数组",
      prop: "drawingPathList",
      span: 24,
      hide: true,
      display: false,
    },
    {
      label: "pdf数组",
      prop: "pdfPathList",
      span: 24,
      hide: true,
      display: false,
    },

    {
      label: "文件名称数组",
      prop: "fileNameList",
      span: 24,
      slot: true,
      hide: true,
      display: false,
    },
    /*{ label: "文件",
      prop: "drawing",
      type: 'img',
      span: 24,
      display:false,
      slot: true,
      formatter: (row, column, cellValue, index) => {
        return import.meta.env.VITE_APP_UPLOAD_URL + `${row.drawingPath}`;} },*/
    {
      label: "版本",
      prop: "drawingVersion",
      type: "number",
      precision: 1,
      rules: [
        {
          required: true,
          message: "版本号不能为空",
          trigger: "blur",
        },
      ],
    },
    {
      label: "创建时间",
      prop: "created",
      width: 120,
      overHidden: true,
      display: false,
    },
    {
      label: "启用状态",
      slot: true,
      headerAlign: "center",
      prop: "enable",
      width: 100,
      addDisplay: false,
    },
  ],
});

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