<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <!--插槽:table标题-->
    <template #tableTitle>
      <c-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</c-button>
      <c-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</c-button>
      <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <c-button>
          批量操作
          <Icon icon="ant-design:down-outlined" />
        </c-button>
      </a-dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <!--<TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"  />-->
      <TableAction :actions="getTableAction(record)" />
    </template>
    <template #fileSlot="{ text }">
      <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
      <c-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)"
        >下载</c-button
      >
    </template>
  </BasicTable>
  <!-- 表单区域 -->
  <IotDeviceAccessModbusItemDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <IotDeviceAccessModbusItemModal @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts" name="iot-iotDeviceAccessModbusItem" setup>
  import { ref, computed, watch } from 'vue';
  import { BasicTable, TableAction } from '@jdlinker/ui';
  import { useDrawer, useModal, useListPage, downloadFile, ColEx } from '@jdlinker/ui';
  import { useUserStore } from '@jdlinker/func';
  import IotDeviceAccessModbusItemDrawer from './components/IotDeviceAccessModbusItemDrawer.vue';
  import IotDeviceAccessModbusItemModal from './components/IotDeviceAccessModbusItemModal.vue';
  import { columns, searchFormSchema } from './IotDeviceAccessModbusItem_data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, release, cancel } from './IotDeviceAccessModbusItem_api';
  const userStore = useUserStore();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册Drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  const showFooter = ref(true);
  const props = defineProps({
    data: { require: true, type: Object }
  });
  // 当前选中的OPC通道，可能会为空，代表未选择部门
  const masterId = computed(() => props.data?.id);
  const masterName = computed(() => props.data?.name);

  // 自适应列配置
  const adaptiveColProps: Partial<ColEx> = {
    xs: 24, // <576px
    sm: 24, // ≥576px
    md: 24, // ≥768px
    lg: 12, // ≥992px
    xl: 8, // ≥1200px
    xxl: 8 // ≥1600px
  };

  //注册table数据
  const { tableContext, onExportXls, onImportXls, createMessage } = useListPage({
    tableProps: {
      api: list,
      columns,
      formConfig: {
        schemas: searchFormSchema,
        baseColProps: adaptiveColProps
      },
      // 请求之前对参数做处理
      beforeFetch(params) {
        params.masterId = masterId.value;
      }
    },
    exportConfig: {
      name: 'linker_device_access_modbus_item',
      url: getExportUrl
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess
    }
  });

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

  watch(
    () => props.data,
    () => reload()
  );

  /**
   * 新增事件
   */
  function handleAdd() {
    if (!masterId.value) {
      createMessage.warning('请先选择一个OPC通道');
    } else {
      openDrawer(true, {
        isUpdate: 0,
        showFooter: true,
        // 初始化负责部门
        // nextDepartOptions: { name: props.data?.id, state: props.data?.name },
        record: {
          activitiSync: 1,
          userIdentity: 1,
          masterId: masterId.value,
          masterName: masterName.value
        }
      });
    }
    // openDrawer(true, { isUpdate: 0, showFooter: true });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: any) {
    if (userStore.getSysOpenModal) {
      openModal(true, { record, isUpdate: true, showFooter: true });
    } else {
      openDrawer(true, { record, isUpdate: 1, showFooter: true });
    }
  }
  /**
   * 详情
   */
  function handleDetail(record: any) {
    if (userStore.getSysOpenModal) {
      openModal(true, { record, isUpdate: 2 });
    } else {
      openDrawer(true, { record, isUpdate: 2 });
    }
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 启用
   */
  async function handleRelease(record) {
    await release(record, handleSuccess);
  }
  /**
   * 禁用
   */
  async function handleCancel(record) {
    await cancel(record, handleSuccess);
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    if (record.state === '0') {
      return [
        // {
        //   icon: 'clarity:info-standard-line',
        //   tooltip: '详情',
        //   onClick: handleDetail.bind(null, record),
        // },
        {
          icon: 'clarity:note-edit-line',
          tooltip: '编辑',
          onClick: handleEdit.bind(null, record)
        },
        {
          icon: 'ant-design:play-circle-outlined',
          tooltip: '启用',
          onClick: handleRelease.bind(null, record)
        },
        {
          icon: 'ant-design:delete-outlined',
          color: 'error',
          tooltip: '删除',
          popConfirm: {
            title: '是否确认删除',
            placement: 'left',
            confirm: handleDelete.bind(null, record)
          }
        }
      ];
    } else {
      return [
        // {
        //   icon: 'clarity:info-standard-line',
        //   tooltip: '详情',
        //   onClick: handleDetail.bind(null, record),
        // },
        {
          icon: 'clarity:note-edit-line',
          tooltip: '编辑',
          onClick: handleEdit.bind(null, record)
        },
        {
          icon: 'ant-design:stop-outlined',
          tooltip: '禁用',
          onClick: handleCancel.bind(null, record)
        },
        {
          icon: 'ant-design:delete-outlined',
          color: 'error',
          tooltip: '删除',
          popConfirm: {
            title: '是否确认删除',
            placement: 'left',
            confirm: handleDelete.bind(null, record)
          }
        }
      ];
    }
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record)
      },
      {
        label: '删除',
        popConfirm: {
          title: '确定删除吗?',
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }
</script>
<style scoped></style>
