<template>
  <div>{{ form }}</div>
  <div>{{ search }}</div>
  <el-button @click="test">测试公共弹窗</el-button>
  <el-button
    v-debounce:click="
      () => {
        testDebunce('这是传递的参数');
      }
    "
    >测试防抖截流</el-button
  >
  <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 #fileUrl-form="scope">
        <single-upload v-model="form.fileUrl" />
        {{ form }}
      </template>
      <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/plan/order/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/plan/order/export')"
        >
          <template #icon> <i-ep-download /> </template>导出
        </el-button>
      </template>
    </avue-crud>
    <CommonTable
      ref="ctableRef"
      tableTitle="测试title"
      tableType="MARTERIAL"
      @selected-sure="onSelectedFinish"
    />
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
    <FilesUpload
      v-model:src="fileUrl"
      v-model:src-list="fileUrlList"
      v-model:pdf-list="pdfUrlList"
      :generate-pdf="true"
      @finished="testFiles"
    />
    <el-button @click="testFiles">测试上传文件的值</el-button>
    <div style="height: 100px; width: 100px; overflow: hidden">
      <PDFView
        :need-to-show-pdf="true"
        content-type="button"
        :is-link="true"
        :show-pdf-number="3"
        pdf-source="http://192.168.101.4:9000/jgfile/2024/04/20/%E7%AC%AC08%E7%AB%A0_%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0.pdf"
      />
    </div>
  </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 } from "@/store";
import SingleUpload from "@/components/Upload/SingleUpload.vue";
import PDFView from "@/components/PDFView/index.vue";
// 测试上传文件相关
const fileUrl = ref(""); //单文件
const fileUrlList = ref([]); //多文件
const pdfUrlList = ref([]); //pdf多文件
const testFiles = () => {
  console.log(import.meta.env.VITE_APP_UPLOAD_URL);
  console.log("fileUrl", fileUrl.value);
  console.log("fileUrlList", fileUrlList.value);
  console.log("pdfUrlList", pdfUrlList.value);
};

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

// 公共弹窗相关
const ctableRef = ref(null);
const test = () => {
  if (ctableRef.value) {
    ctableRef.value.startSelect();
  }
};
const onSelectedFinish = (selectedValue) => {
  console.log("onSelectedFinish", selectedValue);
};

const testDebunce = (args) => {
  console.log("执行了事件", args);
};

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

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/plan/order/import");
  }
};

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: true,
  column: [
    {
      label: "产品",
      prop: "materialCode",
      search: true,
    },
    {
      label: "年龄",
      prop: "materialModel",
      search: true,
    },
    {
      label: "性别",
      prop: "orderCode",
      search: true,
    },
    {
      label: "网络字典",
      prop: "province",
      type: "select",
      dicUrl: dictDataUtil.request_url + dictDataUtil.TYPE_CODE.routing_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "全局字典",
      prop: "province",
      type: "select",
      dicData: dicts.station_operate_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "身高",
      prop: "projectCode",
      search: true,
    },
    {
      label: "是否xxxx",
      prop: "xxxxx",
      search: false,
      filterable: true,
      type: "radio", //类型为单选框
      dicData: [
        {
          label: "否",
          value: 0,
        },
        {
          label: "是",
          value: 1,
        },
      ],
      value: 0,
    },
    {
      label: "附件上传",
      prop: "fileUrl",
      span: 24,
      slot: true,
    },
  ],
});
</script>
