<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>
    </avue-crud>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";

import { useCommonStoreHook } from "@/store";
import dictDataUtil from "@/common/configs/dictDataUtil";
import { queryTypeDataList } from "@/api/device";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/device/fieldAlias",
  });
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 typeDataList = ref([]);
const typeDataValues = ref({});
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  delBtn: false,
  selection: true,
  column: [
    {
      label: "设备类型",
      prop: "deviceType",
      type: "select",
      editDisplay: false,
      search: true,
      dicUrl: dictDataUtil.request_url + dictDataUtil.TYPE_CODE.device_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      change: ({ value, index }) => {
        if (value) {
          form.value.originalField = "";
          typeDataList.value = typeDataValues.value[value];
          if (typeDataList.value === undefined) {
            typeDataList.value = [];
          }
        }
      },
      rules: [
        {
          required: true,
          message: "设备类型不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "原始字段",
      prop: "originalField",
      editDisplay: false,
      type: "select",
      dicData: typeDataList,
      rules: [
        {
          required: true,
          message: "原始字段不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "字段类型",
      prop: "fieldType",
      type: "select",
      dicUrl:
        dictDataUtil.request_url +
        dictDataUtil.EXPAND_FIELD_TABLE.expand_field_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "字段类型不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "页面显示",
      prop: "filedLabel",
      rules: [
        {
          required: true,
          message: "页面显示不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "字段解释",
      prop: "filedExplain",
    },
    {
      label: "字段排序",
      prop: "sort",
    },
  ],
});
onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
  queryTypeDataList().then((data) => {
    typeDataValues.value = data.data;
  });
});
</script>
