<script setup lang="ts">
import { reactive, onMounted, toRaw } from 'vue';
import { PageContainer } from '@ant-design-vue/pro-layout';
import { Modal } from 'ant-design-vue/lib';
import { SizeType } from 'ant-design-vue/lib/config-provider';
import { MenuInfo } from 'ant-design-vue/lib/menu/src/interface';
import { ColumnsType } from 'ant-design-vue/lib/table';
import { parseDateToStr } from '@/utils/date-utils';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { saveAs } from 'file-saver';
import useI18n from '@/hooks/useI18n';
import { getTraceRawInfo, listTraceData } from '@/api/traceManage/analysis';
const { t } = useI18n();

/**查询参数 */
let queryParams = reactive({
  /**移动号 */
  imsi: '',
  /**移动号 */
  msisdn: '',
  /**当前页数 */
  pageNum: 1,
  /**每页条数 */
  pageSize: 20,
});

/**查询参数重置 */
function fnQueryReset() {
  queryParams = Object.assign(queryParams, {
    imsi: '',
    pageNum: 1,
    pageSize: 20,
  });
  tablePagination.current = 1;
  tablePagination.pageSize = 20;
  fnGetList();
}

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

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

/**表格字段列 */
let tableColumns: ColumnsType = [
  {
    title: t('views.traceManage.analysis.trackTaskId'),
    dataIndex: 'taskId',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.imsi'),
    dataIndex: 'imsi',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.msisdn'),
    dataIndex: 'msisdn',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.srcIp'),
    dataIndex: 'srcAddr',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.dstIp'),
    dataIndex: 'dstAddr',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.signalType'),
    dataIndex: 'ifType',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.msgType'),
    dataIndex: 'msgType',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.msgDirect'),
    dataIndex: 'msgDirect',
    align: 'center',
  },
  {
    title: t('views.traceManage.analysis.rowTime'),
    dataIndex: 'timestamp',
    align: 'center',
    customRender(opt) {
      if (!opt.value) return '';
      return parseDateToStr(opt.value);
    },
  },
  {
    title: t('common.operate'),
    key: 'id',
    align: 'center',
  },
];

/**表格分页器参数 */
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();
  },
});

/**表格紧凑型变更操作 */
function fnTableSize({ key }: MenuInfo) {
  tableState.size = key as SizeType;
}

/**查询备份信息列表, pageNum初始页数 */
function fnGetList(pageNum?: number) {
  if (tableState.loading) return;
  tableState.loading = true;
  if (pageNum) {
    queryParams.pageNum = pageNum;
  }
  listTraceData(toRaw(queryParams)).then(res => {
    if (res.code === RESULT_CODE_SUCCESS && Array.isArray(res.rows)) {
      tablePagination.total = res.total;
      tableState.data = res.rows;
    }
    tableState.loading = false;
  });
}

/**抽屉对象信息状态类型 */
type ModalStateType = {
  /**抽屉框是否显示 */
  visible: boolean;
  /**标题 */
  title: string;
  /**表单数据 */
  from: Record<string, any>;
};

/**抽屉对象信息状态 */
let modalState: ModalStateType = reactive({
  visible: false,
  title: '',
  from: {
    rawData: '',
    rawDataHTML: '',
    downBtn: false,
  },
});

/**
 * 对话框弹出显示
 * @param row 记录信息
 */
function fnModalVisible(row: Record<string, any>) {
  // 进制转数据
  const hexString = parseBase64Data(row.rawMsg);
  const rawData = convertToReadableFormat(hexString);
  modalState.from.rawData = rawData;
  // RAW解析HTML
  getTraceRawInfo(row.id).then(res => {
    if (res.code === RESULT_CODE_SUCCESS) {
      const htmlString = rawDataHTMLScript(res.msg);
      modalState.from.rawDataHTML = htmlString;
      modalState.from.downBtn = true;
    } else {
      modalState.from.rawDataHTML = t('views.traceManage.analysis.noData');
    }
  });
  modalState.title = t('views.traceManage.analysis.taskTitle', {
    num: row.imsi,
  });
  modalState.visible = true;
}

/**
 * 对话框弹出关闭
 */
function fnModalVisibleClose() {
  modalState.visible = false;
  modalState.from.downBtn = false;
  modalState.from.rawDataHTML = '';
  modalState.from.rawData = '';
}

// 将Base64编码解码为字节数组
function parseBase64Data(hexData: string) {
  // 将Base64编码解码为字节数组
  const byteString = atob(hexData);
  const byteArray = new Uint8Array(byteString.length);
  for (let i = 0; i < byteString.length; i++) {
    byteArray[i] = byteString.charCodeAt(i);
  }

  // 将每一个字节转换为2位16进制数表示，并拼接起来
  let hexString = '';
  for (let i = 0; i < byteArray.length; i++) {
    const hex = byteArray[i].toString(16);
    hexString += hex.length === 1 ? '0' + hex : hex;
  }
  return hexString;
}

// 转换十六进制字节流为可读格式和ASCII码表示
function convertToReadableFormat(hexString: string) {
  let result = '';
  let asciiResult = '';
  let arr = [];
  let row = 100;
  for (let i = 0; i < hexString.length; i += 2) {
    const hexChars = hexString.substring(i, i + 2);
    const decimal = parseInt(hexChars, 16);
    const asciiChar =
      decimal >= 32 && decimal <= 126 ? String.fromCharCode(decimal) : '.';

    result += hexChars + ' ';
    asciiResult += asciiChar;

    if ((i + 2) % 32 === 0) {
      arr.push({
        row: row,
        code: result,
        asciiText: asciiResult,
      });
      result = '';
      asciiResult = '';
      row += 10;
    }
    if (2 + i == hexString.length) {
      arr.push({
        row: row,
        code: result,
        asciiText: asciiResult,
      });
      result = '';
      asciiResult = '';
      row += 10;
    }
  }
  return arr;
}

// 信息详情HTMl内容处理
function rawDataHTMLScript(htmlString: string) {
  // 删除所有 <a> 标签
  // const withoutATags = htmlString.replace(/<a\b[^>]*>(.*?)<\/a>/gi, '');
  // 删除所有 <script> 标签
  let withoutScriptTags = htmlString.replace(
    /<script\b[^>]*>([\s\S]*?)<\/script>/gi,
    ''
  );
  // 默认全展开
  // const withoutHiddenElements = withoutScriptTags.replace(
  //   /style="display:none"/gi,
  //   'style="background:#ffffff"'
  // );

  function set_node(node: any, str: string) {
    if (!node) return;
    node.style.display = str;
    node.style.background = '#ffffff';
  }
  Reflect.set(window, 'set_node', set_node);
  function toggle_node(node: any) {
    node = document.getElementById(node);
    if (!node) return;
    set_node(node, node.style.display != 'none' ? 'none' : 'block');
  }
  Reflect.set(window, 'toggle_node', toggle_node);
  function hide_node(node: any) {
    node = document.getElementById(node);
    if (!node) return;
    set_node(node, 'none');
  }
  Reflect.set(window, 'hide_node', hide_node);

  // 展开第一个
  withoutScriptTags = withoutScriptTags.replace(
    'id="f1c" style="display:none"',
    'id="f1c" style="display:block"'
  );
  return withoutScriptTags;
}

/**信息文件下载 */
function fnDownloadFile() {
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.traceManage.analysis.taskDownTip'),
    onOk() {
      const blob = new Blob([modalState.from.rawDataHTML], {
        type: 'text/plain',
      });
      saveAs(blob, `${modalState.title}_${Date.now()}.html`);
    },
  });
}

onMounted(() => {
  // 获取列表数据
  fnGetList();
});
</script>

<template>
  <PageContainer>
    <a-card
      v-show="tableState.seached"
      :bordered="false"
      :body-style="{ marginBottom: '24px', paddingBottom: 0 }"
    >
      <!-- 表格搜索栏 -->
      <a-form :model="queryParams" name="queryParams" layout="horizontal">
        <a-row :gutter="16">
          <a-col :lg="6" :md="12" :xs="24">
            <a-form-item
              :label="t('views.traceManage.analysis.imsi')"
              name="imsi"
            >
              <a-input
                v-model:value="queryParams.imsi"
                :allow-clear="true"
                :placeholder="t('views.traceManage.analysis.imsiPlease')"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :xs="24">
            <a-form-item
              :label="t('views.traceManage.analysis.msisdn')"
              name="imsi"
            >
              <a-input
                v-model:value="queryParams.msisdn"
                :allow-clear="true"
                :placeholder="t('views.traceManage.analysis.msisdnPlease')"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :xs="24">
            <a-form-item>
              <a-space :size="8">
                <a-button type="primary" @click.prevent="fnGetList(1)">
                  <template #icon><SearchOutlined /></template>
                  {{ t('common.search') }}
                </a-button>
                <a-button type="default" @click.prevent="fnQueryReset">
                  <template #icon><ClearOutlined /></template>
                  {{ t('common.reset') }}
                </a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" :body-style="{ padding: '0px' }">
      <!-- 插槽-卡片左侧侧 -->
      <template #title> </template>

      <!-- 插槽-卡片右侧 -->
      <template #extra>
        <a-space :size="8" align="center">
          <a-tooltip>
            <template #title>{{ t('common.searchBarText') }}</template>
            <a-switch
              v-model:checked="tableState.seached"
              :checked-children="t('common.switch.show')"
              :un-checked-children="t('common.switch.hide')"
              size="small"
            />
          </a-tooltip>
          <a-tooltip>
            <template #title>{{ t('common.reloadText') }}</template>
            <a-button type="text" @click.prevent="fnGetList()">
              <template #icon><ReloadOutlined /></template>
            </a-button>
          </a-tooltip>
          <a-tooltip>
            <template #title>{{ t('common.sizeText') }}</template>
            <a-dropdown trigger="click">
              <a-button type="text">
                <template #icon><ColumnHeightOutlined /></template>
              </a-button>
              <template #overlay>
                <a-menu
                  :selected-keys="[tableState.size as string]"
                  @click="fnTableSize"
                >
                  <a-menu-item key="default">{{
                    t('common.size.default')
                  }}</a-menu-item>
                  <a-menu-item key="middle">{{
                    t('common.size.middle')
                  }}</a-menu-item>
                  <a-menu-item key="small">{{
                    t('common.size.small')
                  }}</a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </a-tooltip>
        </a-space>
      </template>

      <!-- 表格列表 -->
      <a-table
        class="table"
        row-key="id"
        :columns="tableColumns"
        :loading="tableState.loading"
        :data-source="tableState.data"
        :size="tableState.size"
        :pagination="tablePagination"
        :scroll="{ x: true }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'id'">
            <a-space :size="8" align="center">
              <a-tooltip>
                <template #title>查看详情</template>
                <a-button type="link" @click.prevent="fnModalVisible(record)">
                  <template #icon><ProfileOutlined /></template>
                </a-button>
              </a-tooltip>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>

    <!-- 详情框 -->
    <a-modal
      width="800px"
      :title="modalState.title"
      :visible="modalState.visible"
      @cancel="fnModalVisibleClose"
    >
      <div class="raw-title">
        {{ t('views.traceManage.analysis.signalData') }}
      </div>
      <a-row
        class="raw"
        :gutter="16"
        v-for="v in modalState.from.rawData"
        :key="v.row"
      >
        <a-col class="num" :span="2">{{ v.row }}</a-col>
        <a-col class="code" :span="12">{{ v.code }}</a-col>
        <a-col class="txt" :span="10">{{ v.asciiText }}</a-col>
      </a-row>
      <a-divider />
      <div class="raw-title">
        {{ t('views.traceManage.analysis.signalDetail') }}
        <a-button
          type="dashed"
          size="small"
          @click.prevent="fnDownloadFile"
          v-if="modalState.from.downBtn"
        >
          <template #icon>
            <DownloadOutlined />
          </template>
          {{ t('views.traceManage.analysis.taskDownText') }}
        </a-button>
      </div>
      <div class="raw-html" v-html="modalState.from.rawDataHTML"></div>
    </a-modal>
  </PageContainer>
</template>

<style lang="less" scoped>
.table :deep(.ant-pagination) {
  padding: 0 24px;
}

.raw {
  &-title {
    color: #000000d9;
    font-size: 24px;
    line-height: 1.8;
  }
  .num {
    background-color: #e5e5e5;
  }
  .code {
    background-color: #e7e6ff;
  }
  .txt {
    background-color: #ffe3e5;
  }

  &-html {
    max-height: 300px;
    overflow-y: scroll;
  }
}
</style>
