<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        :scroll="{ x: 800 }"
        cache-key="proSystemLinkTable"
      >
        <template #toolbar>
          <LinkSearch @add="openEdit" @search="reload" />
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'link'">
            <a-avatar
              shape="square"
              :width="40"
              :src="record.icon"
              style="margin-right: 5px"
            />
            <a @click="openUrl(record.url)">{{ record.name }}</a>
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click.stop="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="topRight"
                title="确定要删除此模块吗？"
                @confirm.stop="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <link-edit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { AntDesignOutlined, PlusOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading } from 'ele-admin-pro/es';
  import LinkEdit from './components/link-edit.vue';
  import { pageLink, removeLink } from '@/api/oa/link';
  import type { Link, LinkParam } from '@/api/oa/link/model';
  import { Menu } from '@/api/system/menu/model';
  import { openNew, openUrl } from '@/utils/common';
  import LinkSearch from './components/link-search.vue';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    // {
    //   key: 'index',
    //   width: 48,
    //   align: 'center',
    //   fixed: 'left',
    //   hideInSetting: true,
    //   customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    // },
    {
      title: '名称',
      dataIndex: 'link',
      key: 'link'
    },
    {
      title: '备注',
      dataIndex: 'comments'
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
      width: 120,
      showSorterTooltip: false,
      customRender: ({ text }) => ['显示', '隐藏'][text]
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center'
    }
  ]);

  // 表格选中数据
  const selection = ref<Link[]>([]);

  // 当前编辑数据
  const current = ref<Link | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    return pageLink({ ...where, ...orders, limit, page });
  };

  /* 搜索 */
  const reload = (where?: LinkParam) => {
    selection.value = [];
    tableRef?.value?.reload({ page: 1, where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Link) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 自定义行属性 */
  const customRow = (record: Link) => {
    return {
      // 行点击事件
      onClick: () => {
        // openUrl(`${record.url}`);
      },
      // 行双击事件
      onDblclick: () => {
        openUrl(`${record.url}`);
      }
    };
  };

  /* 删除单个 */
  const remove = (row: Link) => {
    const hide = messageLoading('请求中..', 0);
    removeLink(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
</script>

<script lang="ts">
  export default {
    name: 'SystemLink'
  };
</script>
