<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="rowSave"
      @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="BOM添加"
      tableType="MARTERIAL"
      @selected-sure="onSelectedFinish"
    />
  </div>
</template>
<script setup="ts">
  import { ref, getCurrentInstance } from "vue";
  import { useCrud } from "@/hooks/userCrud";
  import ButtonPermKeys from "@/common/configs/buttonPermission";
  import { useCommonStoreHook } from "@/store";

  const { isShowTable, tableType } = toRefs(useCommonStoreHook());
  import { useDictionaryStore } from "../../../store/index";
  const { dicts } = useDictionaryStore();
  const test = () => {
    isShowTable.value = true;
    tableType.value = tableType.value == 1 ? 2 : 1;
  };
  const ctableRef = ref(null);
  // 传入一个url，后面不带/
  const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
    useCrud({
      src: "/api/v1/wms/vehicleMaterial",
    });
  const { dataList, createRow, updateRow, deleteRow, searchChange, 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,
  // });
  function rowSave(form,done,loading) {
    console.info(form);
    createRow(form,done,loading);

  }
  const crudRef = ref(null); //crudRef.value 获取avue-crud对象

  onMounted(() => {
    // console.log("crudRef", crudRef)
    dataList();
  });

  /**
   * 上传excel相关
   */
  const uploadRef = ref(null);
  const uploadFinished = () => {
    // 上传完成后的刷新操作
    page.currentPage = 1;
    dataList();
  };
  const importExcelData = () => {
    if (uploadRef.value) {
      uploadRef.value.show("/api/v1/base/station");
    }
  };

  // 设置表格列或者其他自定义的option
  option.value = Object.assign(option.value, {
    selection: true,
    column: [

      {
        label: "料箱等级",
        prop: "vehicleLevel",
        search: true,
        rules: [{
          required: true,
          message: "请选择工位类型",
          trigger: "blur"
        }],
        type: 'select',
        dicData:dicts.vehicle_level,
        filterable: true, //添加filterable属性即可启用搜索功能
        props: {
          label: "dictLabel", // 下拉菜单显示的字段
          value: "dictValue" // 下拉菜单值的字段
        },
      },

      {
        label: "物料编号",
        prop: "materialCode",
        display:false,
        search:true,
        rules: [{
          required: true,
          message: "请填选择物料编码",
          trigger: "blur",
        }],
      },
      {
        label: "物料编号",
        prop: "materialCode",
        hide:true,
        click:({ value, column }) => {
          ctableRef.value.startSelect();
        },
        rules: [{
          required: true,
          message: "请填选择物料编码",
          trigger: "blur",
        }],
      },
      {
        label: "物料名称",
        prop: "materialName",
        search:true,
        disabled:true,

      },
      {
        label: "规格",
        prop: "spec",
        disabled:true,
      },
      {
        label: "描述",
        prop: "remark",
        type: 'textarea',
        span: 24,
      },
      {
        label: "创建人",
        prop: "creator",
        display:false
      },
      {
        label: "创建时间",
        prop: "created",
        display:false
      },
    ],
  });
  const onSelectedFinish = (selectedValue) => {
    form.value.materialCode=selectedValue.materialCode;
    form.value.code=selectedValue.materialCode;
    form.value.materialName=selectedValue.materialName;
    form.value.spec=selectedValue.spec;
  }
</script>

