<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <tenant-search @search="reload" />
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="tenantId"
        :columns="columns"
        :datasource="datasource"
        :scroll="{ x: 800 }"
      >
        <!--        <template #toolbar>-->
        <!--          <a-space>-->
        <!--            <a-button type="primary" class="ele-btn-icon" @click="openEdit()">-->
        <!--              <template #icon>-->
        <!--                <plus-outlined />-->
        <!--              </template>-->
        <!--              <span>创建租户</span>-->
        <!--            </a-button>-->
        <!--          </a-space>-->
        <!--        </template>-->
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'tenantName'">
            {{ record.tenantName }}
          </template>
          <template v-if="column.key === 'status'">
            <block @click="updateStatus(record)">
              <a-tag v-if="record.status === 1" color="green">运营中</a-tag>
              <a-tag v-if="record.status === 0" color="red">未上线</a-tag>
            </block>
          </template>
          <template v-if="column.key === 'comments'">
            <a-tooltip :title="record.comments">
              <FormOutlined
                @click="onEditContent('comments', record.comments, record)"
              />
              {{ record.comments }}
            </a-tooltip>
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm
                title="确定要删除此记录吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <tenant-edit v-model:visible="showEdit" :data="current" @done="reload" />
    <!-- 用户详情 -->
    <tenant-info v-model:visible="showInfo" :data="current" @done="reload" />
    <!-- 管理弹窗 -->
    <tenant-manage
      v-model:visible="showManage"
      :data="current"
      @done="reload"
    />
    <Markdown
      v-model:visible="showMarkdown"
      :data="data"
      :field="field"
      :tenantId="tenantId"
      :content="markdown"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue';
  import {
    PlusOutlined,
    ExclamationCircleOutlined,
    FormOutlined
  } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import TenantSearch from './components/tenant-search.vue';
  import TenantEdit from './components/tenant-edit.vue';
  import TenantInfo from './components/tenant-info.vue';
  import TenantManage from './components/tenant-manage.vue';
  import Markdown from './components/markdown.vue';
  import {
    pageTenant,
    removeTenant,
    updateTenant
    // removeBatchTenant,
    // updateTenantPassword
  } from '@/api/oa/tennat/index';
  import type { Tenant, TenantParam } from '@/api/oa/tennat/model';

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

  // 是否显示资产详情
  const showInfo = ref(false);
  const showManage = ref(false);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '租户编号(AppId)',
      dataIndex: 'tenantId',
      key: 'tenantId',
      sorter: true
    },
    {
      title: '租户名称',
      dataIndex: 'tenantName',
      key: 'tenantName',
      sorter: true
    },
    {
      title: '备注',
      dataIndex: 'comments',
      key: 'comments'
    },
    {
      title: '状态',
      dataIndex: 'status',
      sorter: true,
      key: 'status'
    },
    {
      title: '所属客户',
      dataIndex: 'customerName',
      key: 'customerName',
      ellipsis: true,
      hideInTable: true,
      sorter: true
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    }
    // {
    //   title: '操作',
    //   key: 'action',
    //   width: 200,
    //   align: 'center',
    //   hideInSetting: true
    // }
  ]);

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

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

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

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

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

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

  /* 编辑备注信息 */
  const tenantId = ref(undefined);
  const markdown = ref('请输入备注内容');
  const showMarkdown = ref(false);
  const field = ref('comments');
  const onEditContent = (name, text, item) => {
    tenantId.value = item.tenantId;
    field.value = name;
    markdown.value = text;
    showMarkdown.value = true;
  };

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

  /* 更新状态 */
  const updateStatus = (item) => {
    let index = 0;
    if (item.status === 0) {
      index = 1;
    }
    updateTenant({
      tenantId: item.tenantId,
      status: index
    }).then(() => {
      message.success('操作成功');
      reload();
    });
  };
</script>

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