<template>
  <div class="p-2">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" :model="queryParam" :label-col="labelCol"
              :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="5">
            <a-form-item name="nuName">
              <template #label><span title="区域名称">区域名称</span></template>
              <a-input v-model:value="queryParam.nuName" placeholder="请输入区域名称" allow-clear/>
            </a-form-item>
          </a-col>
          <a-col :lg="5">
            <a-form-item name="dimension">
              <template #label><span title="设备维度">设备维度</span></template>
              <a-select v-model:value="queryParam.dimension" placeholder="请选择设备维度" allow-clear>
                <a-select-option value="区域维度">区域维度</a-select-option>
                <a-select-option value="机构维度">机构维度</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="5">
            <a-form-item name="deviceType">
              <template #label><span title="设备类型">设备类型</span></template>
              <a-select v-model:value="queryParam.deviceType" placeholder="请选择设备类型" allow-clear>
                <a-select-option value="摄像头">摄像头</a-select-option>
                <a-select-option value="录像机">录像机</a-select-option>
                <a-select-option value="交换机">交换机</a-select-option>
                <a-select-option value="路由器">路由器</a-select-option>
                <a-select-option value="无线AP">无线AP</a-select-option>
                <a-select-option value="智能电表">智能电表</a-select-option>
                <a-select-option value="智能水表">智能水表</a-select-option>
                <a-select-option value="温湿度计">温湿度计</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="5">
            <a-form-item name="deviceStatus">
              <template #label><span title="在线状态">在线状态</span></template>
              <a-select v-model:value="queryParam.deviceStatus" placeholder="请选择在线状态" allow-clear>
                <a-select-option value="在线">在线</a-select-option>
                <a-select-option value="离线">离线</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="4">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
                <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset"
                          style="margin-left: 8px">重置</a-button>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd"> 添加设备</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <DeviceAddModal ref="registerDrawer" @success="handleSuccess" />
    <LogModal ref="logDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="iotManager-deviceList" setup>
import {ref, reactive, defineExpose, nextTick} from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { deviceColumns } from '../manager.data';
import { deviceList } from '../manager.api';
import { useUserStore } from '/@/store/modules/user';
import DeviceAddModal from './DeviceAddModal.vue';
import LogModal from './LogModal.vue';

const formRef = ref();
const queryParam = reactive<any>({});
const registerDrawer = ref();
const logDrawer = ref();
const departId = ref<any>('');
const nuId = ref<any>('');
const orgCode = ref<any>('');
const userStore = useUserStore();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '设备管理',
    api: deviceList,
    columns: deviceColumns,
    canResize: false,
    useSearchForm: false,
    showIndexColumn: true,
    actionColumn: {
      width: 180,
      fixed: 'right',
    },
    beforeFetch: async (params) => {
      return Object.assign(params, queryParam);
    },
  },
});
const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;
const labelCol = reactive({
  xs:24,
  sm:4,
  xl:6,
  xxl:4
});
const wrapperCol = reactive({
  xs: 24,
  sm: 20,
});

/**
 * 成功回调
 */
function handleSuccess() {
  reload();
}

/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '设备日志',
      onClick: deviceLog.bind(null, record),
    },
  ];
}

/**
 * 设备管理
 */
function handleAdd(){
  let record = { "departId" : departId.value,"nuId":nuId.value,"orgCode":orgCode.value };
  registerDrawer.value.disableSubmit = true;
  registerDrawer.value.edit(record);
}

/**
 * 设备日志
 */
function deviceLog(record: Recordable){
  logDrawer.value.disableSubmit = true;
  logDrawer.value.edit(record);
}

/**
 * 查询
 */
function searchQuery() {
  queryParam.nuId = nuId.value;
  reload();
}

/**
 * 重置
 */
function searchReset() {
  formRef.value.resetFields();
  //刷新数据
  searchQuery();
}

//初始化
function init(record) {
  nuId.value = record.nuId;
  departId.value = record.departId;
  orgCode.value = record.sysOrgCode;
  searchQuery();
}

defineExpose({
  init
});

</script>

<style lang="less" scoped>

.jeecg-basic-table-form-container {
  padding: 0;
  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }
  .query-group-cust{
    min-width: 100px !important;
  }
  .query-group-split-cust{
    width: 30px;
    display: inline-block;
    text-align: center
  }
  .ant-form-item:not(.ant-form-item-with-help){
    margin-bottom: 16px;
    height: 32px;
  }
  :deep(.ant-picker),:deep(.ant-input-number){
    width: 100%;
  }
}

.p-2{
  height: calc(100vh - 120px);
}

</style>
