<script setup lang="ts">
import { reactive, ref, onMounted, toRaw } from 'vue';
import { PageContainer } from 'antdv-pro-layout';
import { SizeType } from 'ant-design-vue/es/config-provider';
import { ColumnsType } from 'ant-design-vue/es/table';
import { Modal, message } from 'ant-design-vue/es';
import { parseDateToStr } from '@/utils/date-utils';
import { getNeFile, listNeFiles } from '@/api/tool/neFile';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import ViewDrawer from '@/views/logManage/neFile/components/ViewDrawer.vue';
import useNeInfoStore from '@/store/modules/neinfo';
import useTabsStore from '@/store/modules/tabs';
import useI18n from '@/hooks/useI18n';
import saveAs from 'file-saver';
import { useRoute, useRouter } from 'vue-router';
const tabsStore = useTabsStore();
const neInfoStore = useNeInfoStore();
const { t } = useI18n();
const route = useRoute();
const router = useRouter();

// 获取地址栏参数
const routeParams = route.query as Record<string, any>;

/**网元参数 */
let neTypeSelect = ref<string[]>([]);

/**查询参数 */
let queryParams = reactive({
  /**网元类型 */
  neType: '',
  neId: '',
  /**读取路径 */
  path: '',
  /**前缀过滤 */
  search: '',
  /**当前页数 */
  pageNum: 1,
  /**每页条数 */
  pageSize: 20,
});

/**表格状态类型 */
type TabeStateType = {
  /**加载等待 */
  loading: boolean;
  /**紧凑型 */
  size: SizeType;
  /**记录数据 */
  data: object[];
};

/**表格状态 */
let tableState: TabeStateType = reactive({
  loading: false,
  size: 'small',
  data: [],
});

/**表格字段列 */
let tableColumns: ColumnsType = reactive([
  {
    title: t('views.logManage.neFile.fileMode'),
    dataIndex: 'fileMode',
    align: 'center',
    width: 150,
  },
  {
    title: t('views.logManage.neFile.size'),
    dataIndex: 'size',
    align: 'left',
    width: 100,
  },
  {
    title: t('views.logManage.neFile.modifiedTime'),
    dataIndex: 'modifiedTime',
    align: 'left',
    customRender(opt) {
      if (!opt.value) return '';
      return parseDateToStr(opt.value * 1000);
    },
    width: 150,
  },
  {
    title: t('views.logManage.neFile.fileName'),
    dataIndex: 'fileName',
    align: 'left',
    resizable: true,
    width: 200,
    minWidth: 100,
    maxWidth: 350,
  },
  {
    title: t('common.operate'),
    key: 'fileName',
    align: 'left',
  },
]);

/**表格分页器参数 */
let tablePagination = reactive({
  /**当前页数 */
  current: 1,
  /**每页条数 */
  pageSize: 20,
  /**默认的每页条数 */
  defaultPageSize: 20,
  /**指定每页可以显示多少条 */
  pageSizeOptions: ['10', '20', '50', '100'],
  /**只有一页时是否隐藏分页器 */
  hideOnSinglePage: false,
  /**是否可以快速跳转至某页 */
  showQuickJumper: true,
  /**是否可以改变 pageSize */
  showSizeChanger: true,
  /**数据总数 */
  total: 0,
  showTotal: (total: number) => t('common.tablePaginationTotal', { total }),
  onChange: (page: number, pageSize: number) => {
    tablePagination.current = page;
    tablePagination.pageSize = pageSize;
    queryParams.pageNum = page;
    queryParams.pageSize = pageSize;
    fnGetList();
  },
});

/**下载触发等待 */
let downLoading = ref<boolean>(false);

/**信息文件下载 */
function fnDownloadFile(row: Record<string, any>) {
  if (downLoading.value) return;
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.logManage.neFile.downTip', { fileName: row.fileName }),
    onOk() {
      downLoading.value = true;
      const hide = message.loading(t('common.loading'), 0);
      getNeFile({
        neType: queryParams.neType,
        neId: queryParams.neId,
        path: queryParams.path,
        fileName: row.fileName,
        delTemp: true,
      })
        .then(res => {
          if (res.code === RESULT_CODE_SUCCESS) {
            message.success({
              content: t('common.msgSuccess', {
                msg: t('common.downloadText'),
              }),
              duration: 2,
            });
            saveAs(res.data, `${row.fileName}`);
          } else {
            message.error({
              content: t('views.logManage.neFile.downTipErr'),
              duration: 2,
            });
          }
        })
        .finally(() => {
          hide();
          downLoading.value = false;
        });
    },
  });
}

/**tmp目录下，UPF标准版内部输出目录 */
let tmp = ref<boolean>(false);

/**UPF标准版内部抓包的输出目录 */
function fnUPFTmp() {
  fnDirCD('/tmp', 0);
}

/**关闭跳转 */
function fnClose() {
  const to = tabsStore.tabClose(route.path);
  if (to) {
    router.push(to);
  } else {
    router.back();
  }
}

/**访问路径 */
let nePathArr = ref<string[]>([]);

/**进入目录 */
function fnDirCD(dir: string, index?: number) {
  if (index === undefined) {
    nePathArr.value.push(dir);
    queryParams.search = '';
    fnGetList(1);
    return;
  }
  if (index === 0) {
    const neType = queryParams.neType;
    if (neType === 'UPF' && tmp.value) {
      nePathArr.value = ['/tmp'];
      queryParams.search = `${neType}_${queryParams.neId}`;
    } else {
      nePathArr.value = [
        `/tmp/omc/tcpdump/${neType.toLowerCase()}/${queryParams.neId}`,
      ];
      queryParams.search = '';
    }

    fnGetList(1);
  } else {
    nePathArr.value = nePathArr.value.slice(0, index + 1);
    queryParams.search = '';
    fnGetList(1);
  }
}

/**网元类型选择对应修改 */
function fnNeChange(keys: any, _: any) {
  if (!Array.isArray(keys)) return;
  const neType = keys[0];
  const neId = keys[1];
  // 不是同类型时需要重新加载
  if (queryParams.neType !== neType || queryParams.neId !== neId) {
    queryParams.neType = neType;
    queryParams.neId = neId;
    if (neType === 'UPF' && tmp.value) {
      nePathArr.value = ['/tmp'];
      queryParams.search = `${neType}_${neId}`;
    } else {
      nePathArr.value = [`/tmp/omc/tcpdump/${neType.toLowerCase()}/${neId}`];
      queryParams.search = '';
    }
    fnGetList(1);
  }
}

/**查询列表, pageNum初始页数 */
function fnGetList(pageNum?: number) {
  if (queryParams.neId === '') {
    message.warning({
      content: t('views.logManage.neFile.neTypePlease'),
      duration: 2,
    });
    return;
  }
  if (tableState.loading) return;
  tableState.loading = true;
  if (pageNum) {
    queryParams.pageNum = pageNum;
  }
  queryParams.path = nePathArr.value.join('/');
  listNeFiles(toRaw(queryParams)).then(res => {
    if (res.code === RESULT_CODE_SUCCESS) {
      const { total, rows } = res.data;
      tablePagination.total = total;
      tableState.data = rows;
      if (
        tablePagination.total <=
          (queryParams.pageNum - 1) * tablePagination.pageSize &&
        queryParams.pageNum !== 1
      ) {
        tableState.loading = false;
        fnGetList(queryParams.pageNum - 1);
      }
    } else {
      message.error(res.msg, 3);
      tablePagination.total = 0;
      tableState.data = [];
    }
    tableState.loading = false;
  });
}

/**抽屉状态 */
const viewDrawerState = reactive({
  open: false,
  /**文件路径 /var/log/amf.log */
  filePath: '',
  /**网元类型 */
  neType: '',
  /**网元ID */
  neId: '',
});

/**打开抽屉查看 */
function fnDrawerOpen(row: Record<string, any>) {
  viewDrawerState.filePath = [...nePathArr.value, row.fileName].join('/');
  viewDrawerState.neType = neTypeSelect.value[0];
  viewDrawerState.neId = neTypeSelect.value[1];
  viewDrawerState.open = !viewDrawerState.open;
}

onMounted(() => {
  // 获取网元网元列表
  neInfoStore.fnNelist().then(res => {
    if (res.code === RESULT_CODE_SUCCESS) {
      if (res.data.length === 0) {
        message.warning({
          content: t('common.noData'),
          duration: 2,
        });
      } else if (routeParams.neType && routeParams.neId) {
        neTypeSelect.value = [routeParams.neType, routeParams.neId];
        fnNeChange(neTypeSelect.value, undefined);
      }
    }
  });
});
</script>

<template>
  <PageContainer>
    <a-card :bordered="false" :body-style="{ padding: '0px' }">
      <!-- 插槽-卡片左侧侧 -->
      <template #title>
        <a-row :gutter="16" :wrap="true" align="middle">
          <a-col>
            <a-button type="default" @click.prevent="fnClose()">
              <template #icon><CloseOutlined /></template>
              {{ t('common.close') }}
            </a-button>
          </a-col>
          <a-col>
            <span>{{ t('views.logManage.neFile.neType') }}:</span>&nbsp;
            <a-cascader
              v-model:value="neTypeSelect"
              :options="neInfoStore.getNeCascaderOptions"
              @change="fnNeChange"
              :allow-clear="false"
              :placeholder="t('views.logManage.neFile.neTypePlease')"
              :disabled="downLoading || tableState.loading"
            />
          </a-col>
          <template v-if="nePathArr.length > 0">
            <span>{{ t('views.logManage.neFile.nePath') }}:</span>&nbsp;
            <a-col>
              <a-breadcrumb>
                <a-breadcrumb-item
                  v-for="(path, index) in nePathArr"
                  :key="path"
                  @click="fnDirCD(path, index)"
                >
                  {{ path }}
                </a-breadcrumb-item>
              </a-breadcrumb>
            </a-col>
          </template>
        </a-row>
      </template>

      <!-- 插槽-卡片右侧 -->
      <template #extra>
        <a-space :size="8" align="center">
          <a-tooltip placement="topRight" v-if="neTypeSelect[0] === 'UPF'">
            <template #title>
              {{ t('views.traceManage.pcap.fileUPFTip') }}
            </template>
            <a-checkbox v-model:checked="tmp" @change="fnUPFTmp()">
              {{ t('views.traceManage.pcap.fileUPF') }}
            </a-checkbox>
          </a-tooltip>
          <a-tooltip placement="topRight">
            <template #title>{{ t('common.reloadText') }}</template>
            <a-button type="text" @click.prevent="fnGetList()">
              <template #icon><ReloadOutlined /></template>
            </a-button>
          </a-tooltip>
        </a-space>
      </template>

      <!-- 表格列表 -->
      <a-table
        class="table"
        row-key="fileName"
        :columns="tableColumns"
        :loading="tableState.loading"
        :data-source="tableState.data"
        :size="tableState.size"
        :pagination="tablePagination"
        :scroll="{ x: 800 }"
        @resizeColumn="(w:number, col:any) => (col.width = w)"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'fileName'">
            <a-space :size="8" align="center">
              <a-tooltip
                v-if="
                  record.fileType === 'file' && record.fileName.endsWith('.log')
                "
              >
                <template #title>{{ t('common.viewText') }}</template>
                <a-button type="link" @click.prevent="fnDrawerOpen(record)">
                  <template #icon><ProfileOutlined /></template>
                </a-button>
              </a-tooltip>
              <a-button
                type="link"
                :loading="downLoading"
                @click.prevent="fnDownloadFile(record)"
                v-if="record.fileType === 'file'"
              >
                <template #icon><DownloadOutlined /></template>
                {{ t('common.downloadText') }}
              </a-button>
              <a-button
                type="link"
                :loading="downLoading"
                @click.prevent="fnDirCD(record.fileName)"
                v-if="record.fileType === 'dir'"
              >
                <template #icon><FolderOutlined /></template>
                {{ t('views.logManage.neFile.dirCd') }}
              </a-button>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>

    <!-- 文件内容查看抽屉 -->
    <ViewDrawer
      v-model:open="viewDrawerState.open"
      :file-path="viewDrawerState.filePath"
      :ne-type="viewDrawerState.neType"
      :ne-id="viewDrawerState.neId"
    ></ViewDrawer>
  </PageContainer>
</template>

<style lang="less" scoped></style>
