<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleInsert"> 添加设备</a-button>
      </template>
      <template #bodyCell="{ column, record, index, text }">
        <template v-if="column.dataIndex === 'nuName'">
          <span v-if="!record.nuName"><a @click="handlePzhldy(record)"> 未配置 </a></span>
          <span v-else><a @click="handlePzhldy(record)"> {{record.nuName}} </a></span>
        </template>
        <template v-if="column.dataIndex === 'status'">
          <span v-if="record.status ==='0'" style="color:green">
            在线
          </span>
          <span v-else style="color:red">
            离线
          </span>
        </template>
      </template>
        <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
    </BasicTable>
    <DeviceInfoDrawer @register="registerDrawer" @success="handleSuccess" />
    <ApiLogAlarmModal ref="apiLogAlarmModal"></ApiLogAlarmModal>
    <HldyUtilsModal ref="hldyUtilsModal" @success="handleHldyParams" ></HldyUtilsModal>
    <HumidDeviceSyncLogListModal ref="syncLogModal"></HumidDeviceSyncLogListModal>
  </div>
</template>

<script lang="ts" name="iot-nuIotRegionInfo" setup>
  import {reactive, ref,h} from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useUserStore } from '/@/store/modules/user';
  import {Modal} from "ant-design-vue";
  import {list, deleteDevice, updateDeviceRealTime} from './humid.api';
  import { columns, searchFormSchema } from './humid.data';
  import {useModal} from "@/components/Modal";
  import {useDrawer} from "@/components/Drawer";
  import ApiLogAlarmModal from './components/ApiLogAlarmModal.vue'
  import HldyUtilsModal from "@/views/utils/nuUtils/HldyUtilsModal.vue";
  import HumidDeviceSyncLogListModal from "/@/views/iot/tq/nuIotTqElectricitySyncLog/HumidDeviceSyncLogListModal.vue";
  import DeviceInfoDrawer from "./components/DeviceInfoDrawer.vue";
  import { defHttp } from '/@/utils/http/axios';
  const apiLogAlarmModal = ref();
  const hldyUtilsModal = ref();
  const syncLogModal = ref();
  //注册drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  const queryParam = reactive<any>({});
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
    tableProps:{
      title: '温湿度',
      api: list,
      columns,
      canResize:false,
      showIndexColumn: true,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter:true,
        showAdvancedButton:true,
        fieldMapToNumber: [
        ],
        fieldMapToTime: [
        ],
      },
      actionColumn: {
        width: 290,
        fixed:'right'
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record){
    return [
      {
        label: '抄表',
        onClick: handleRead.bind(null, record),
      },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
      },
      {
        label: '日志',
          onClick: handleApiLogAlarm.bind(null, record),
      },
      {
        label: '同步',
        onClick: handleSync.bind(null, record),
      },
    ]
  }

  // 抄电表
  async function handleRead(record: Recordable) {
    const params = {
      'sn' : record.sn,
    };
    await updateDeviceRealTime(params);
    handleSuccess()
  }

  function handleInsert(){
    let record = {};
    record["isUpdate"] = false;
    openDrawer(true, {
      isUpdate: false,
      showFooter: true,
      tenantSaas: false,
    });
  }

  // 编辑
  async function handleEdit(record: Recordable) {
    record["isUpdate"] = true;
    console.log(record);
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true,
      tenantSaas: false,
    });
  }

  // 删除
  async function handleDelete(record: Recordable) {
    const params = {
      'sn' : record.sn,
    };
    await deleteDevice(params);
    handleSuccess();
  }

  function handleApiLogAlarm(record){
    apiLogAlarmModal.value.disableSubmit = true;
    apiLogAlarmModal.value.showLogAlarm(record);
  }

  
  //护理单元回调
  function handleHldyParams(params){
    defHttp.post({
      url: "/iot/yiweilian/humidDevice/editHldy",
      params:params
    }).then(res=>{
      console.log("🚀 ~ getTableAction ~ res:", res)
      reload();
    })
  }
  /**
   * 配置护理单元
   */
  function handlePzhldy(record){
    hldyUtilsModal.value.disableSubmit = true;
    hldyUtilsModal.value.edit(record);
  }
  //同步
  function handleSync(record: Recordable){
    syncLogModal.value.disableSubmit = true;
    syncLogModal.value.init(record);
  }
</script>
