<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-left="{ size }">
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="danger"
          icon="el-icon-delete"
          :size="size"
          @click="multipleDelete"
          >删除</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,
  useDictionaryStoreHook,
} from "@/store";

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

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/base/accessories/material",
  });
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 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 onSelectedFinish = (selectedValue) => {
  form.value.materialCode = selectedValue.materialCode;
  repeatObj.value.showCreate = false;
};

// 设置表格列或者其他自定义的option
const switchOp = [
  {
    label: "",
    value: 0,
  },
  {
    label: "",
    value: 1,
  },
];
option.value = Object.assign(option.value, {
  selection: true,
  labelWidth: 110,
  searchLabelWidth: 110,
  column: [
    {
      label: "辅料类型",
      prop: "accessoriesType",
      search: true,
      type: "select",
      dicData: dicts.accessories_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "辅料类型不能为空",
          trigger: "blur",
        },
      ],
    },
    {
      label: "物料编码",
      prop: "materialCode",
      clearable: false,
      rules: [
        {
          required: true,
          message: "生产物料编码不能为空",
          trigger: "change",
        },
      ],
      click: ({ value, column }) => {
        repeatObj.value.showCreate = true;
        startCreat();
      },
      change: ({ value, column }) => {
        if (value != form.value.materialCode) {
          repeatObj.value.showCreate = true;
          startCreat();
        }
      },
    },
    {
      label: "物料名称",
      prop: "materialName",
      addDisplay: false,
      editDisplay: false,
    },
    { label: "物料规格", prop: "spec", addDisplay: false, editDisplay: false },
  ],
});
onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
</script>
