<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="appId"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          :scroll="{ x: 1200 }"
          class="sys-org-table"
          :striped="true"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
              @advanced="openAdvanced"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'appName'">
              <div class="app-box">
                <a-image
                  :height="45"
                  :width="45"
                  :preview="false"
                  :src="record.appIcon"
                  fallback="https://file.wsdns.cn/20230218/550e610d43334dd2a7f66d5b20bd58eb.svg"
                />
                <div class="app-info">
                  <a class="ele-text-heading" @click="openEdit(record)">
                    {{ record.appName }}
                  </a>
                  <span class="ele-text-placeholder">
                    {{ record.companyName }}
                  </span>
                </div>
              </div>
            </template>
            <template v-if="column.key === 'appCode'">
              <span class="ele-text-placeholder">{{ record.appCode }}</span>
            </template>
            <template v-if="column.key === 'appStatus'">
              <a-tag v-if="record.appStatus === '开发中'" color="red">{{
                record.appStatus
              }}</a-tag>
              <a-tag v-if="record.appStatus === '运行中'" color="success">{{
                record.appStatus
              }}</a-tag>
              <a-tag v-if="record.appStatus === '维护中'" color="orange">{{
                record.appStatus
              }}</a-tag>
              <a-tag v-if="record.appStatus === '已下架'">{{
                record.appStatus
              }}</a-tag>
            </template>
            <template v-if="column.key === 'appUrl'">
              <a-tooltip v-if="record.appUrl" :title="record.appUrl">
                <a @click="openUrl(record.appUrl)">前往</a>
              </a-tooltip>
            </template>
            <template v-if="column.key === 'appTypeMultiple'">
              <a-space
                :size="2"
                direction="vertical"
                v-if="record.appTypeMultiple"
              >
                <a-tag
                  v-for="(item, index) in JSON.parse(record.appTypeMultiple)"
                  :key="index"
                  class="ele-text-placeholder"
                  >{{ item }}</a-tag
                >
              </a-space>
            </template>
            <template v-if="column.key === 'appQrcode'">
              <a-image
                v-if="record.appQrcode"
                :height="45"
                :width="45"
                :src="record.appQrcode"
              />
            </template>
            <template v-if="column.key === 'createTime'">
              {{ record.createTime }}
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button @click="openEdit(record)">管理</a-button>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <AppEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import { openUrl } from '@/utils/common';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import Search from './components/search.vue';
  import AppEdit from './components/app-edit.vue';
  import { pageApp, removeBatchApp, removeApp } from '@/api/oa/app';
  import { App, AppParam } from '@/api/oa/app/model';

  defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

  // 表格实例
  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: '应用ID',
      dataIndex: 'appId',
      width: 100,
      key: 'appId'
    },
    {
      title: '应用名称',
      dataIndex: 'appName',
      width: 360,
      key: 'appName'
    },
    {
      title: '应用标识',
      dataIndex: 'appCode',
      key: 'appCode'
    },
    {
      title: '应用类型',
      dataIndex: 'appTypeMultiple',
      key: 'appTypeMultiple'
    },
    {
      title: '应用状态',
      dataIndex: 'appStatus',
      key: 'appStatus'
    },
    {
      title: '应用地址',
      dataIndex: 'appUrl',
      key: 'appUrl'
    },
    {
      title: '二维码',
      dataIndex: 'appQrcode',
      key: 'appQrcode'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime'
    },
    {
      title: '操作',
      key: 'action',
      width: 120,
      align: 'center',
      fixed: 'right',
      hideInSetting: true
    }
  ]);

  // 表格选中数据
  const selection = ref<App[]>([]);
  // 当前编辑数据
  const current = ref<App | null>(null);
  // 是否显示资产详情
  const showInfo = ref(false);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 是否显示高级搜索
  const showAdvancedSearch = ref(false);

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

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

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

  /* 打开用户详情弹窗 */
  const openInfo = (row?: App) => {
    current.value = row ?? null;
    showInfo.value = true;
  };

  /* 打开高级搜索 */
  const openAdvanced = () => {
    showAdvancedSearch.value = !showAdvancedSearch.value;
  };

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

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

  /* 自定义行属性 */
  const customRow = (record: App) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        openEdit(record);
      }
    };
  };

  reload();
</script>

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

<style lang="less" scoped>
  p {
    line-height: 0.8;
  }
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
  .price-edit {
    padding-right: 5px;
  }
  .comments {
    max-width: 200px;
  }
  .app-box {
    display: flex;
    .app-info {
      display: flex;
      margin-left: 15px;
      margin-right: 15px;
      flex-direction: column;
    }
  }
</style>
