<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <!--插槽:table标题-->
    <template #tableTitle>
      <c-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined">新增</c-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="mdi:chevron-down" />
        </c-button>
      </a-dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <Save v-if="visible" :data="current" @change="saveChange" />
</template>

<script lang="ts" setup>
  import { ref, toRaw } from 'vue';
  import { BasicTable, TableAction, useListPage } from '@jdlinker/ui';
  import { columns, searchFormSchema } from './data';
  import { list, deleteOne, batchDelete } from './service';
  import Save from './Save/index.vue';
  import { onlyMessage } from '@jdlinker/utils';
  import { useMenuStore } from '@jdlinker/func';

  const menuStory = useMenuStore();
  const visible = ref(false);
  const current = ref({});
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '固件升级任务',
      api: list,
      columns,
      formConfig: {
        schemas: searchFormSchema
      }
    }
  });

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

  const saveChange = (value: boolean) => {
    visible.value = false;
    current.value = {};
    if (value) {
      onlyMessage('操作成功', 'success');
      handleSuccess();
    }
  };

  const handleAdd = () => {
    current.value = {};
    visible.value = true;
  };
  const handleEdit = (record: any) => {
    current.value = toRaw({ ...record, view: false });
    visible.value = true;
  };
  const handleDetail = (record: any) => {
    menuStory.jumpPage('/device/Firmware/Task/Detail', { id: record.id });
  };
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    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:delete-outlined',
        color: 'error',
        tooltip: '删除',
        popConfirm: {
          title: '是否确认删除',
          placement: 'left',
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }
</script>
<style scoped></style>
