<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model="form"
      v-model:page="page"
      v-model:search="search"
      :data="data"
      :option="option"
      @row-save="deviceList"
      @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 icon="el-icon-plus" type="primary" @click="deviceList"
          >新增
        </el-button>
        <el-button
          :disabled="toDeleteIds.length < 1"
          :size="size"
          icon="el-icon-delete"
          type="danger"
          @click="multipleDelete"
          >删除
        </el-button>
      </template>
    </avue-crud>
    <CommonTable
      ref="ctableRef"
      :multiple="true"
      multipleKey="deviceNo"
      tableTitle="设备列表"
      tableType="DEVICE"
      @select-multiple-sure="onSelectedFinish"
    />
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook } from "@/store";

const { isShowTable, tableType } = toRefs(useCommonStoreHook());
import { useDictionaryStore } from "@/store";
import { columns } from "./columns";
import { addStationDevice } from "@/api/station";

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/base/stationDevice",
  });
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,
// });
function rowSave(form, done, loading) {
  createRow(form, done, loading);
}

const crudRef = ref(null); //crudRef.value 获取avue-crud对象

onMounted(() => {
  // console.log("crudRef", crudRef)
  search.value.stationId = props.stationId;
  dataList();
});

const ctableRef = ref(null);
const deviceList = () => {
  ctableRef.value.startSelect();
};

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: true,
  column: columns,
  saveBtn: false,
  delBtn: false,
  editBtn: false,
  addBtn: false,
  viewBtn: false,
  menu: false,
});
const props = defineProps({
  stationId: {
    type: String,
    default: "",
  },
});

watch?.(
  () => props.stationId,
  (newVal: string) => {
    search.value.stationId = newVal;
    dataList();
  }
);
const onSelectedFinish = (selectedValue: string[]) => {
  for (let i = 0; i < data.value.length; i++) {
    if (selectedValue.includes(data.value[i].deviceNo)) {
      ElMessage({
        message: "设备已绑定过，无需再次绑定",
        type: "error",
      });
      return false;
    }
  }
  if (selectedValue.length < 1) {
    ElMessage({
      message: "没有选择设备",
      type: "error",
    });
    return false;
  }
  const selectDevice = ref({
    deviceNos: selectedValue,
    stationId: props.stationId,
  });

  addStationDevice(selectDevice.value).then((data: any) => {
    if (data.code === "200") {
      ElMessage({
        message: data.msg,
        type: "success",
      });
      search.value.stationId = props.stationId;
      dataList();
    } else {
      ElMessage({
        message: data.msg,
        type: "error",
      });
    }
  });
};
</script>
