<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="{row}">
        <el-button
            icon="el-icon-setting"
            text
            @click="openDialog(row.deviceType)"
            type="primary"
            size="small">模型</el-button>
      </template>
      <template #protocolState="{row}">
        <el-switch
            active-value= '1'
            inactive-value= '0'
            width="60"
            inline-prompt
            active-text="启用"
            inactive-text="禁用"
            v-model="row.protocolState"
            @click="changeItem(row)"
            class="ml-2"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
        />
      </template>
    </avue-crud>
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="60%"
        @close="dialog.visible = false"
    >
      <model-page :deviceType="choiceDeviceType"></model-page>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import {updateProtocol} from "@/api/device";
import { useCommonStoreHook } from "@/store";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ModelPage from "./components/model-page.vue";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const loading = ref(false); //  加载状态
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    dataListUrl: "/api/v1/device/protocolPage",
  });
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 =(row) => {
  updateProtocol({state: row.protocolState,deviceType: row.deviceType}).then((data)=>{
    if(data.code === '200'){
      ElMessage.success(data.msg);
      dataList()
    }else{
      ElMessage.error(data.msg);
    }
  })
}
const choiceDeviceType = ref(null)
const dialog = reactive({
  title: "模型",
  visible: false,
});
const openDialog = (type) =>{
  dialog.visible = true
  choiceDeviceType.value = type
}
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  addBtn: false,
  editBtn: false,
  viewBtn: false,
  searchBtn: false,
  menuTitle: "模型设置",
  column: [
    {
      label: "设备类型",
      prop: "deviceType",
      type: "select",
      overHidden: true,
      dicUrl:
        dictDataUtil.request_url +
        dictDataUtil.TYPE_CODE.device_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "设备类型不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "协议状态",
      prop: "protocolState",
      slot: true,
    },
    {
      label: "更新时间",
      prop: "updated",
      overHidden: true,
    },
    {
      label: "更新人",
      prop: "updator",
      overHidden: true,
    },
  ],
});

onMounted(() => {
  search.value.protocol = "1"
  // console.log("crudRef", crudRef)
  dataList();

});
</script>
