<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 v-if="props.flowState == '0'||props.flowState == '2'||props.flowState == '-1'"
          type="primary"
          link
          size="small"
          @click="handleEdit(row, index)"
          ><i-ep-edit />编辑
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { updateRework } from "@/api/rework";
import {
  useCommonStoreHook,
  useDictionaryStore,
  useDictionaryStoreHook,
} from "@/store";
import { updateItemRecord } from "@/api/process/index";
// 数据字典相关
const { dicts } = useDictionaryStore();

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/processReplace/getItemRecord",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const repeatObj = ref({});
// 公共弹窗相关
const ctableRef = ref(null);

const props = defineProps({
  replaceId: {
    type: String,
    default: () => {
      return 0;
    },
  },
  flowState: {
    type: String,
    default: () => {
      return 0;
    },
  },
  dialog: {
    type: Object,
    default: () => {
      return {};
    },
  },
});
const startCreat = () => {
  if (ctableRef.value) {
    ctableRef.value.startSelect();
  }
};
defineExpose({ startCreat });
/**
 * 上传excel相关
 */
const uploadRef = ref(null);
const uploadFinished = () => {
  // 上传完成后的刷新操作
  page.currentPage = 1;
  dataList();
};
const importExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/plan/order/import");
  }
};
const handleEdit = (row, index) => {
  row.replaceId = props.replaceId;
  crudRef.value && crudRef.value.rowEdit(row, index);
};
const onSelectedFinish = (selectedValue) => {
  form.value.materialCode = selectedValue.materialCode;
  repeatObj.value.showCreate = false;
};

option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: false,
  labelWidth: 110,
  searchLabelWidth: 110,
  addBtn: false,
  delBtn: false,
  editBtn: false,
  column: [
    {
      label: "物料编码",
      prop: "itemCode",
      search: true,
      disabled: true,
    },
    {
      label: "物料规格",
      prop: "itemModel",
      search: true,
      disabled: true,
    },
    {
      label: "物料名称",
      prop: "itemName",
      disabled: true,
    },
    {
      label: "批次号/管号",
      prop: "batchNo",
      disabled: true,
    },
    {
      label: "替换批次号",
      prop: "replaceBatchNo",
    },
  ],
});
onMounted(() => {
  search.value.replaceId = props.replaceId;
  dataList();
});
</script>
