<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'iotlink:add'">{{
          t('新增')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              auth: 'iotlink:edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'iotlink:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <IotLinkModal @register="registerModal" @success="handleSuccess" :width="800" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useModal } from '/@/components/Modal';
  import IotLinkModal from './components/IotLinkModal.vue';
  import { deleteIotLink, getIotLinkPageList } from '/@/api/iot';
  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: t('关键字'),
      component: 'Input',
      colProps: { xs: 16, md: 8 },
      componentProps: {
        placeholder: t('请输入关键字'),
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      title: t('链接名称'),
      dataIndex: 'name',
      width: '10%',
      sorter: true,
    },

    {
      title: t('协议类型'),
      dataIndex: 'protocolType',
      width: '10%',
      sorter: true,
      customRender: ({ value }) => {
        return value === 0 ? 'MQTT' : 'OPC.UA';
      },
    },

    {
      title: t('物联网数据源地址'),
      dataIndex: 'hostUrl',
      sorter: true,
    },
    {
      title: t('客户端id'),
      dataIndex: 'clientId',
      sorter: true,
    },
    {
      title: t('订阅主题'),
      dataIndex: 'topic',
      sorter: true,
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      customRender: ({ value }) => {
        return value ? '在线' : '离线';
      },
    },
    {
      title: t('备注'),
      dataIndex: 'remark',
      sorter: true,
    },
  ];

  const { notification } = useMessage();

  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: t('物联网链接'),
    api: getIotLinkPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    useSearchForm: true,
    striped: false,
    showTableSetting: true,
    actionColumn: {
      width: 120,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    tableSetting: {
      size: false,
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      id: record.id,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    deleteIotLink([record.id]).then((_) => {
      reload();
      notification.success({
        message: t('提示'),
        description: t('删除成功'),
      }); //提示消息
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
