<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
      >

      <template #menu-right="{}">
        <el-dropdown split-button
          >导入
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                @click="downloadTemplate('/api/v1/base/frock/template')"
              >
                <i-ep-download />下载模板
              </el-dropdown-item>
              <el-dropdown-item @click="importExcelData">
                <i-ep-top />导入数据
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-button
          class="ml-3"
          @click="exportData('/api/v1/base/frock/export')"
        >
          <template #icon> <i-ep-download /> </template>导出
        </el-button>
      </template>
    </avue-crud>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="900px"
      @close="dialog.visible = false"
      :destroy-on-close="true"
    >
      <drawing-page
        :materialCode="drawingDetail.materialCode"
        :materialName="drawingDetail.materialName"
        :dialog="dialog"
      />
    </el-dialog>
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import {
  useCommonStoreHook,
  useDictionaryStore,
  useDictionaryStoreHook,
} from "@/store";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const { dicts } = useDictionaryStore();

const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const dialog = reactive({
  title: "图纸",
  visible: false,
});
const drawingDetail = ref(null);
const drawing = (row) => {
  drawingDetail.value = row;
  dialog.visible = true;
};
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/base/frock",
  });
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/base/frock/import");
  }
};
option.value = Object.assign(option.value, {
  selection: true,
  column: [
    {
      label: "工装编码",
      prop: "materialCode",
      width: 130,
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,
          editDisabled: false,
          message: "请填写工装编码",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工装名称",
      prop: "materialName",
      width: 140,
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,
          message: "请填写工装名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工装属性",
      prop: "attributeDictValue",
      search: true,
      filterable: true,
      type: "select",
      width: 100,
      overHidden: true,
      dicData: dicts.material_properties,
      props: { label: "dictLabel", value: "dictValue" },
      rules: [
        {
          required: true,
          message: "请选择工装属性",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工装规格",
      prop: "spec",
      width: 100,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "请填写工装规格",
          trigger: "blur",
        },
      ],
    },
    {
      label: "单位",
      prop: "unitDictValue",
      filterable: true,
      type: "select",
      dicData: dicts.danwei_type,
      props: { label: "dictLabel", value: "dictValue" },
      rules: [
        {
          required: true,
          message: "请选择单位",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工装级别",
      prop: "levelDictValue",
      search: true,
      filterable: true,
      type: "select",
      width: 100,
      overHidden: true,
      dicData: dicts.material_level,
      props: { label: "dictLabel", value: "dictValue" },
      rules: [
        {
          required: true,
          message: "请选择工装级别",
          trigger: "blur",
        },
      ],
    },
    {
      label: "维修厂家",
      prop: "manufacturer",
      width: 100,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "请填写维修厂家",
          trigger: "blur",
        },
      ],
    },
    {
      label: "质检方案",
      prop: "inspectDictValue",
      search: true,
      filterable: true,
      type: "select",
      width: 100,
      overHidden: true,
      dicData: dicts.quality_testing_plan,
      props: { label: "dictLabel", value: "dictValue" },
    },
    {
      label: "适用平台",
      prop: "applicablePlatformsDictValue",
      search: true,
      filterable: true,
      type: "select",
      width: 100,
      overHidden: true,
      dicData: dicts.applicable_platforms,
      props: { label: "dictLabel", value: "dictValue" },
      rules: [
        {
          required: true,
          message: "请选择适用平台",
          trigger: "blur",
        },
      ],
    },
    {
      label: "质量等级",
      prop: "qualityLevelDictValue",
      search: true,
      width: 100,
      overHidden: true,
      filterable: true,
      type: "select",
      dicData: dicts.quality_grade,
      props: { label: "dictLabel", value: "dictValue" },
      rules: [
        {
          required: true,
          message: "请选择质量等级",
          trigger: "blur",
        },
      ],
    },
    {
      label: "选用类型",
      prop: "selectionDictValue",
      search: true,
      filterable: true,
      width: 100,
      overHidden: true,
      type: "select",
      dicData: dicts.selection_type,
      props: { label: "dictLabel", value: "dictValue" },
    },
    { label: "客户型号", prop: "customerModel", width: 100, overHidden: true },
    {
      label: "保质期(天)",
      prop: "qualityGuaranteePeriod",
      width: 100,
      overHidden: true,
      type: "number",
      min: 0,
    },
    {
      label: "封装方法",
      prop: "packageDictValue",
      search: true,
      width: 100,
      overHidden: true,
      filterable: true,
      type: "select",
      dicData: dicts.packaging_method,
      props: { label: "dictLabel", value: "dictValue" },
    },
    {
      label: "筛选规范",
      prop: "selectionSpec",
      width: 150,
      overHidden: true,
      type: "textarea",
      span: 18,
    },

    {
      label: "备注",
      prop: "remark",
      width: 150,
      overHidden: true,
      type: "textarea",
      span: 18,
    },
  ],
});
</script>
