<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-right="{}">
        <el-dropdown split-button
          >入库单导入同步到WMS
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                @click="downloadTemplate('/api/v1/warehousing/entry/template')"
              >
                <i-ep-download />下载模板
              </el-dropdown-item>
              <el-dropdown-item @click="importExcelData">
                <i-ep-top />导入数据
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </avue-crud>

    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import { syncAps } from "@/api/aps";
import { useCommonStoreHook, useDictionaryStore } from "@/store";
import { ElMessage, ElMessageBox } from "element-plus";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const { dicts } = useDictionaryStore();
const apsSync = () => {
  ElMessageBox.confirm("是否同步所选中数据？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    let param = { type: 2, codes: toDeleteIds.value };
    syncAps(param).then((data) => {
      if (data.code === "200") {
        ElMessage.success(data.msg);
      } else {
        ElMessage.error(data.msg);
      }
    });
  });
};



// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/warehousing/entry",
  });
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,
// });

const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const router = useRouter();
const binding = (row) => {
  router.push(`/base/bom/${row.materialCode}`);
};
onMounted(() => {
  // 设置表格列或者其他自定义的option

  dataList();
});

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

option.value = Object.assign(option.value, {
  selection: true,
  addBtn: false,
  editBtn: false,
  viewBtn: false,
  column: [
    {
      label: "操作人",
      prop: "creator",
    },
    {
      label: "操作时间",
      prop: "created",
    },

  ],
});
</script>
