<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 #menu="{ size, row, index }">
        <el-button
          icon="el-icon-edit"
          text
          v-if="(props.status == 0 || props.status == 1)&&props.flowState!=='1'&&props.flowState!=='3'"
          @click="doEdit(row, index)"
          type="primary"
          :size="size"
          >编辑</el-button
        >
      </template>
      <!--        <single-upload v-model="form.drawingPath" :generatePdf="true"/>-->
      <template #drawingPath-form="scope">
        <FilesUpload
          v-model="form.drawingPath"
          :src-list="srcList"
          :file-name-list="fileNameList"
          :limit="10"
          @finished="testFiles"
        />
      </template>
<!--      <template #checkName-form="scope">
        <el-tree-select
            v-model="form.checkName"
            :data="userList"
            filterable
        />
      </template>-->
    </avue-crud>
  </div>
</template>
<script setup>
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook, useDictionaryStore } from "@/store";
import SingleUpload from "@/components/Upload/SingleUpload.vue";
import {getUserList, getUserTree} from "@/api/system/user";
import { ref } from "vue";
import FilesUpload from "@/components/Upload/FilesUpload.vue";
import {signatureList} from "@/api/signature";

// 数据字典相关
const { dicts } = useDictionaryStore();
const userList = ref([]);

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/rework/item",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const filePath = import.meta.env.VITE_APP_UPLOAD_URL;
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const props = defineProps({
  recordId: {
    type: String,
    default: () => {
      return 0;
    },
  },
  flowState: {
    type: String,
    default: () => {
      return 0;
    },
  },
  dialog: {
    type: Object,
    default: () => {
      return {};
    },
  },
  status: {
    type: Number,
    default: () => {
      return 0;
    },
  },
});
const doEdit = (row, index) => {
  srcList.value = [];
  fileNameList.value = [];
  crudRef.value && crudRef.value.rowEdit(row, index);
};
const srcList = ref([]);
const fileNameList = ref([]);
const testFiles = () => {
  form.value.drawingPathList = srcList.value;
  form.value.fileNameList = fileNameList.value;
};

onMounted(() => {
  search.value.recordId = props.recordId;
  dataList();
    signatureList({signatureType:"3"}).then((data) => {
    userList.value = data.data;
  });
});

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: false,
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: true,
  column: [
    {
      label: "工序",
      prop: "operationName",
      search: true,
      width: 100,
      overHidden: true,
      editDisabled: true,
    },
    {
      label: "工艺条件",
      prop: "processAsk",
      width: 160,
      overHidden: true,
      editDisabled: true,
    },
    {
      label: "接收数",
      prop: "num",
      rules: [
        {
          required: true,
          message: "请填写接收数量",
          trigger: "blur",
        },
      ],
    },
    {
      label: "合格数",
      prop: "qualifiedNum",
      rules: [
        {
          required: true,
          message: "请填写合格数量",
          trigger: "blur",
        },
      ],
    },
    {
      label: "合格率",
      prop: "qualifiedRate",
      editDisplay: false,
    },
    {
      label: "检验人",
      prop: "checkName",
      type: "tree",
      dicData: userList,
        props: {
            label: 'signatureName',
            value: 'id'
        },

    },
    {
      label: "操作者",
      prop: "operateName",
      editDisplay: false,
    },
    {
      label: "日期",
      prop: "operateDate",
      width: 120,
      editDisplay: false,
    },
    {
      label: "附件",
      prop: "drawingPath",
      viewDisplay: false,
      slot: true,
      hide: true,
    },
    {
      label: "附件",
      prop: "drawingPathList",
      display: false,
      hide: true,
    },
    {
      label: "附件",
      prop: "fileNameList",
      display: false,
      hide: true,
    },
    {
      label: "文件",
      prop: "filePath",
      display: false,
      width: 160,
      type: "textarea",
      html: true,
      formatter: (val) => {
        if (val.filePath != "" && null != val.filePath) {
          let array = JSON.parse(val.filePath);
          let file = "";
          array.forEach((item) => {
            file =
              file +
              '<a class="el-tag el-tag--info el-tag--light" href="' +
              filePath +
              item.filePath +
              '" >' +
              item.fileName +
              "</a>";
          });
          return file;
          /* return (
            '<a class="el-tag el-tag--info el-tag--light" href="' +
            filePath +
            val.filePath +
            '" >文件下载</a>'
          );*/
        } else {
          return val.filePath;
        }
      },
    },
    /* {
      label: "附件",
      prop: "operateDate",
      display: false,
    },
    {
      label: "附件下载",
      prop: "filePath",
      width: 120,
      html: true,
      formatter: (val) => {
        if (val.filePath != "") {
          return (
            '<a class="el-tag el-tag--info el-tag--light" href="' +
            filePath +
            val.filePath +
            '" >文件下载</a>'
          );
        } else {
          return val.filePath;
        }
      },
    },*/
  ],
});
</script>
