<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
        >
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="refresh"
          icon="el-icon-refresh"
          :size="size"
          @click="apsSync"
          >APS同步</el-button
        >
      </template>
      <template #menu="{ row, index, type }">
        <el-button @click="binding(row)" text type="primary"
          >BOM
          <template #icon> <svg-icon icon-class="BOM"></svg-icon> </template
        ></el-button>
        <el-button @click="drawing(row)" text type="primary"
          >图纸<template #icon>
            <svg-icon icon-class="tuzhi"></svg-icon> </template
        ></el-button>
      </template>

      <template #menu-right="{}">
        <el-dropdown split-button
          >物料导入
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                @click="downloadTemplate('/api/v1/base/material/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-dropdown split-button style="margin-left: 10px"
          >BOM导入
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                @click="
                  downloadTemplate('/api/v1/base/materialBom/bom/template')
                "
              >
                <i-ep-download />下载模板
              </el-dropdown-item>
              <el-dropdown-item @click="importBomExcelData">
                <i-ep-top />导入数据
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-button
          class="ml-3"
          @click="exportData('/api/v1/base/material/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 { 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);
      }
    });
  });
};
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/material",
  });
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/material/import");
  }
};

const importBomExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/base/materialBom/bom/import");
  }
};
option.value = Object.assign(option.value, {
  selection: true,
  column: [
    {
      label: "物料编码",
      prop: "materialCode",
      width: 130,
      overHidden: true,
      search: true,
      editDisabled: true,
      rules: [
        {
          required: true,
          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: "stageDictValue",
      search: true,
      filterable: true,
      width: 100,
      overHidden: true,
      type: "select",
      dicData: dicts.stage,
      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: "frock",
      search: true,
      width: 90,
      overHidden: true,
      filterable: true,
      type: "radio", //类型为单选框
      dicData: [
        {
          label: "是",
          value: "1",
        },
        {
          label: "否",
          value: "0",
        },
      ],
      value: "1",
      rules: [
        {
          required: true,
          message: "是否工装",
          trigger: "blur",
        },
      ],
    },
    {
      label: "筛选规范",
      prop: "selectionSpec",
      width: 150,
      overHidden: true,
      type: "textarea",
      span: 18,
    },

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