<!-- 已采物料 -->
<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef2"
      v-model:search="search"
      v-model="form"
      @row-update="updateRow"
      :data="data"
      :option="option"
      v-model:page="page"
    >
      <template #menu="{ size, row, index }">
        <el-button
          v-if="row.itemCode != undefined && row.itemCode != null"
          type="primary"
          link
          size="small"
          @click="doEdit(row, index)"
          ><i-ep-edit />编辑
        </el-button>
      </template>
    </avue-crud>
    <CommonTable
      ref="ctableRef"
      tableTitle="物料选择"
      tableType="MARTERIAL"
      @selected-sure="onSelectedFinish"
    />
  </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 {
  useCommonStoreHook,
  useDictionaryStore,
  useUserStoreHook,
} from "@/store";

// 数据字典相关
const { dicts } = useDictionaryStore();
const crudRef2 = ref({});
// 传入一个url，后面不带/
const {
  form,
  data,
  option,
  search,
  page,
  toDeleteIds,
  Methords,
  Utils,
  commonConfig,
} = useCrud({
  src: "/api/v1/process/web/traceability/materialsRecordInfo",
});
const ctableRef = ref(null);
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const userStore = useUserStoreHook();
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const doEdit = (row, index) => {
  crudRef2.value && crudRef2.value.rowEdit(row, index);
};
const refreshTra = (row) => {
  commonConfig.value.params = {
    seqNo: row.seqNo,
    workOrderCode: row.workOrderCode,
  };
  dataList();
};
defineExpose({ refreshTra });
onMounted(() => {
  if (userStore.user.userId === 10000) {
    option.value.menu = true;
  }
});
const onSelectedFinish = (selectedValue) => {
  form.value.itemCode = selectedValue.materialCode;
  form.value.itemName = selectedValue.materialName;
  form.value.itemModel = selectedValue.spec;
};
option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: false,
  border: true,
  index: false,
  expandLevel: 3,
  headerAlign: "center",
  align: "center",
  labelWidth: 100,
  addBtn: false,
  delBtn: false,
  viewBtn: false,
  menu: false,
  header: false,
  editBtn: false,
  rowKey: "opId",
  column: [
    {
      label: "工序Id",
      prop: "opId",
      display: false,
      hide: true,
    },
    {
      label: "工序名称",
      prop: "opName",
      display: false,
    },

    {
      label: "物料名称",
      prop: "itemName",
      disabled: "true",
    },
    {
      label: "物料规格",
      prop: "itemModel",
      disabled: "true",
      search: true,
      type: "input",
    },
    {
      label: "物料编号",
      prop: "itemCode",
      search: false,
      click: ({ value, column }) => {
        ctableRef.value.startSelect();
      },
      change: ({ value, column }) => {
        if (value != form.value.itemCode) {
          ctableRef.value.startSelect();
        }
      },
      rules: [
        {
          required: true,
          message: "请选择物料编码",
          trigger: "blur",
        },
      ],
    },
    {
      label: "采集编码",
      prop: "itemSeq",
      display: false,
      search: false,
    },
    {
      label: "批次号/管号",
      prop: "batchNo",
      display: false,
    },
    {
      label: "替换批次号",
      prop: "replaceBatchNo",
      display: false,
    },
    {
      label: "已采集数量",
      prop: "num",
      search: false,
    },
  ],
});
</script>
