<template>
  <div class="app-task">
    <!-- 表格 -->
    <ele-pro-table
      ref="tableRef"
      row-key="taskId"
      :columns="columns"
      :datasource="datasource"
      tool-class="ele-toolbar-form"
      class="sys-org-table"
    >
      <template #toolbar>
        <AppUrlSearch @add="openEdit" @remove="removeBatch" />
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'domain'">
          <a :href="record.domain" target="_blank">{{ record.domain }}</a>
        </template>
        <template v-if="column.key === 'action'">
          <a @click="moveUp(record)">上移<ArrowUpOutlined /></a>
          <a-divider type="vertical" />
          <a @click="openEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
            <a class="ele-text-danger">删除</a>
          </a-popconfirm>
        </template>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <AppUrlEdit
      v-model:visible="showEdit"
      :app-id="data.appId"
      :data="current"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref, watch } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type { DatasourceFunction } from 'ele-admin-pro/es/ele-pro-table/types';
  import AppUrlSearch from './app-url-search.vue';
  import { App } from '@/api/oa/app/model';
  import AppUrlEdit from './app-url-edit.vue';
  import { AppUrl, AppUrlParam } from '@/api/oa/app/url/model';
  import {
    pageAppUrl,
    removeAppUrl,
    removeBatchAppUrl,
    updateAppUrl
  } from '@/api/oa/app/url';
  import { ArrowUpOutlined } from '@ant-design/icons-vue';

  const props = defineProps<{
    appId: any;
    data: App;
  }>();

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
  const selection = ref<any[]>();
  // 当前编辑数据
  const current = ref<AppUrl | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    // 搜索条件
    where.appId = props.appId;
    return pageAppUrl({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表格列配置
  const columns = ref<any[]>([
    {
      title: '客户端',
      dataIndex: 'name',
      width: 180
    },
    {
      title: '访问域名',
      dataIndex: 'domain',
      key: 'domain',
      width: 280
    },
    {
      title: '账号',
      dataIndex: 'account',
      width: 280
    },
    {
      title: '密码',
      dataIndex: 'password',
      width: 280
    },
    {
      title: '备注',
      dataIndex: 'comments',
      key: 'comments',
      ellipsis: true
    },
    {
      title: '操作',
      key: 'action',
      width: 180,
      align: 'center',
      hideInSetting: true
    }
  ]);

  const moveUp = (row?: AppUrl) => {
    updateAppUrl({
      appUrlId: row?.appUrlId,
      sortNumber: Number(row?.sortNumber) - 1
    }).then((msg) => {
      message.success(msg);
      reload();
    });
  };

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

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

  /* 删除单个 */
  const remove = (row: AppUrl) => {
    const hide = message.loading('请求中..', 0);
    removeAppUrl(row.appUrlId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value?.length) {
      message.error('请至少选择一条数据');
      return;
    }
    if (selection.value?.length) {
      Modal.confirm({
        title: '提示',
        content: '确定要删除选中的记录吗?',
        icon: createVNode(ExclamationCircleOutlined),
        maskClosable: true,
        onOk: () => {
          const hide = message.loading('请求中..', 0);
          removeBatchAppUrl(selection.value.map((d) => d.appUrlId))
            .then((msg) => {
              hide();
              message.success(msg);
              reload();
            })
            .catch((e) => {
              hide();
              message.error(e.message);
            });
        }
      });
    }
  };

  watch(
    () => props.appId,
    (appId) => {
      if (appId) {
        reload();
      }
    },
    { immediate: true }
  );
</script>

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