<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 #collect="scope">
        <el-switch
            active-value="1"
            inactive-value="0"
            inline-prompt
            active-text="是"
            inactive-text="否"
            v-model="scope.row.collect"
            @click="changeItem($event,scope.row)"
            class="ml-2"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
        />
      </template>
      <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 {configSave,configList,deviceUpdateCollect} from "@/api/device";
import { useCommonStoreHook } from "@/store";
import dictDataUtil from "@/common/configs/dictDataUtil";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const pageData = ref([]);
const loading = ref(false); //  加载状态
const dialog = reactive({
  title: "参数设置",
  visible: false,
});
const handleSubmit = () => {
  //
};
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/pac",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } = Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const changeItem =(obj,row) => {
  deviceUpdateCollect({id: row.id,collect: row.collect,deviceNo: row.deviceNo}).then((data)=>{
    if(data.code === '200'){
      ElMessage.success(data.msg);
    }else{
      row.collect = row.collect === '0' ? '1' : '0'
      ElMessage.error(data.msg);
    }
  })
}
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  column: [
    {
      label: "设备编号",
      prop: "deviceNo",
      search: true,
      width: 160,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "设备编号不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "设备名称",
      prop: "deviceName",
      search: true,
      width: 160,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "设备名称不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "设备类型",
      prop: "deviceType",
      type: "select",
      width: 130,
      editDisabled: true,
      overHidden: true,
      dicUrl:
        dictDataUtil.request_url +
        dictDataUtil.TYPE_CODE.device_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "设备类型不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "设备状态",
      prop: "state",
      type: "select",
      width: 160,
      overHidden: true,
      search: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.device_status,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "设备状态不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "在线状态",
      prop: "onlineState",
      width: 100,
      display: false,
      html: true,
      formatter: (val) => {
        if (val.onlineState === "1") {
          return '<b class="el-tag el-tag--success el-tag--light">在线</b>';
        } else {
          return '<b class="el-tag el-tag--warning el-tag--light">离线</b>';
        }
      },
    },
    {
      label: "是否采集",
      prop: "collect",
      editDisplay: false,
      addDisplay: false,
      slot: true,
      width: 100,
    },
    {
      label: "负责人",
      prop: "head",
      width: 160,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "负责人不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "供应厂商",
      prop: "manufacturer",
      width: 160,
      overHidden: true,
    },
    {
      label: "所属部门",
      prop: "deptId",
      width: 160,
      overHidden: true,
      type: "tree",
      dicUrl: dictDataUtil.dept_tree_url,
      props: {
        label: "deptName",
        value: "id",
      }
    },
    {
      label: "设备位置",
      prop: "devicePosition",
      width: 160,
      overHidden: true,
    },
    {
      label: "规格",
      prop: "specifications",
      width: 160,
      overHidden: true,
    },
    {
      label: "品牌",
      width: 160,
      overHidden: true,
      prop: "brand",
    },
  ],
});

onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
</script>
