<script setup lang="ts">
import { reactive, onMounted, toRaw, defineAsyncComponent, ref } from 'vue';
import { PageContainer } from 'antdv-pro-layout';
import { message, Modal } from 'ant-design-vue/es';
import { SizeType } from 'ant-design-vue/es/config-provider';
import { MenuInfo } from 'ant-design-vue/es/menu/src/interface';
import { ColumnsType } from 'ant-design-vue/es/table';
import useI18n from '@/hooks/useI18n';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import useNeInfoStore from '@/store/modules/neinfo';
import { listNeInfo, delNeInfo, stateNeInfo } from '@/api/ne/neInfo';
import { NE_TYPE_LIST } from '@/constants/ne-constants';
import useDictStore from '@/store/modules/dict';
import useNeOptions from './hooks/useNeOptions';
const { getDict } = useDictStore();
const { t } = useI18n();
const { fnNeStart, fnNeRestart, fnNeStop, fnNeReload, fnNeLogFile } =
  useNeOptions();
// 异步加载组件
const EditModal = defineAsyncComponent(
  () => import('./components/EditModal.vue')
);
const OAMModal = defineAsyncComponent(
  () => import('./components/OAMModal.vue')
);
// 配置备份文件导入
const BackConfModal = defineAsyncComponent(
  () => import('./components/BackConfModal.vue')
);
const backConf = ref(); // 引用句柄，取导出函数

/**字典数据 */
let dict: {
  /**网元信息状态 */
  neInfoStatus: DictType[];
} = reactive({
  neInfoStatus: [],
});

/**查询参数 */
let queryParams = reactive({
  /**网元类型 */
  neType: '',
  /**带状态信息 */
  bandStatus: true,
  /**当前页数 */
  pageNum: 1,
  /**每页条数 */
  pageSize: 20,
});

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

/**表格状态类型 */
type TabeStateType = {
  /**加载等待 */
  loading: boolean;
  /**紧凑型 */
  size: SizeType;
  /**搜索栏 */
  seached: boolean;
  /**记录数据 */
  data: Record<string, any>[];
  /**勾选记录 */
  selectedRowKeys: (string | number)[];
};

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

/**表格字段列 */
let tableColumns: ColumnsType = [
  {
    title: t('views.ne.common.neType'),
    dataIndex: 'neType',
    align: 'left',
    width: 100,
  },
  {
    title: t('views.ne.common.neId'),
    dataIndex: 'neId',
    align: 'left',
    width: 100,
  },
  {
    title: t('views.ne.common.rmUid'),
    dataIndex: 'rmUid',
    align: 'left',
    width: 150,
  },
  {
    title: t('views.ne.common.neName'),
    dataIndex: 'neName',
    align: 'left',
    width: 150,
  },
  {
    title: t('views.ne.common.ipAddr'),
    dataIndex: 'ip',
    align: 'left',
    width: 150,
  },
  {
    title: t('views.ne.common.port'),
    dataIndex: 'port',
    align: 'left',
    width: 100,
  },
  {
    title: t('views.ne.neInfo.state'),
    dataIndex: 'status',
    key: 'status',
    align: 'left',
    width: 100,
  },
  {
    title: t('common.operate'),
    key: 'id',
    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();
  },
});

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

/**表格多选 */
function fnTableSelectedRowKeys(keys: (string | number)[]) {
  tableState.selectedRowKeys = keys;
}

/**对话框对象信息状态类型 */
type ModalStateType = {
  /**配置备份框是否显示 */
  openByBackConf: boolean;
  /**OAM文件配置框是否显示 */
  openByOAM: boolean;
  /**新增框或修改框是否显示 */
  openByEdit: boolean;
  /**新增框或修改框ID */
  editId: string;
  /**OAM框网元类型ID */
  neId: string;
  neType: string;
  /**确定按钮 loading */
  confirmLoading: boolean;
};

/**对话框对象信息状态 */
let modalState: ModalStateType = reactive({
  openByBackConf: false,
  openByOAM: false,
  openByEdit: false,
  editId: '',
  neId: '',
  neType: '',
  confirmLoading: false,
});

/**
 * 对话框弹出显示为 新增或者修改
 * @param noticeId 网元id, 不传为新增
 */
function fnModalVisibleByEdit(row?: Record<string, any>) {
  if (!row) {
    modalState.editId = '';
  } else {
    modalState.editId = row.id;
  }
  modalState.openByEdit = !modalState.openByEdit;
}

/**
 * 对话框弹出确认执行函数
 * 进行表达规则校验
 */
function fnModalEditOk(from: Record<string, any>) {
  // 新增时刷新列表
  if (!from.id) {
    fnGetList();
    return;
  }
  // 编辑时局部更新信息
  stateNeInfo(from.neType, from.neId)
    .then(res => {
      // 找到编辑更新的网元
      const item = tableState.data.find(s => s.id === from.id);
      if (item && res.code === RESULT_CODE_SUCCESS) {
        item.neType = from.neType;
        item.neId = from.neId;
        item.rmUid = from.rmUid;
        item.neName = from.neName;
        item.ip = from.ip;
        item.port = from.port;
        if (res.data.online) {
          item.status = '1';
          if (res.data.standby) {
            item.status = '3';
          }
        } else {
          item.status = '0';
        }
        Object.assign(item.serverState, res.data);
        const resouresUsage = parseResouresUsage(item.serverState);
        Reflect.set(item, 'resoures', resouresUsage);
      }
    })
    .finally(() => {
      useNeInfoStore().fnRefreshNelist();
    });
}

/**
 * 对话框弹出关闭执行函数
 * 进行表达规则校验
 */
function fnModalEditCancel() {
  modalState.editId = '';
  modalState.openByEdit = false;
  modalState.openByOAM = false;
  modalState.openByBackConf = false;
}

/**
 * 记录删除
 * @param id  编号
 */
function fnRecordDelete(id: string) {
  if (!id || modalState.confirmLoading) return;
  let msg = t('views.ne.neInfo.delTip');
  if (id === '0') {
    msg = `${msg} ...${tableState.selectedRowKeys.length}`;
    id = tableState.selectedRowKeys.join(',');
  }

  Modal.confirm({
    title: t('common.tipTitle'),
    content: msg,
    onOk() {
      modalState.confirmLoading = true;
      const hide = message.loading(t('common.loading'), 0);
      delNeInfo(id)
        .then(res => {
          if (res.code === RESULT_CODE_SUCCESS) {
            message.success(t('common.operateOk'), 3);
            // 过滤掉删除的id
            tableState.data = tableState.data.filter(item => {
              if (id.indexOf(',') > -1) {
                return !tableState.selectedRowKeys.includes(item.id);
              } else {
                return item.id !== id;
              }
            });
            // 刷新缓存
            useNeInfoStore().fnRefreshNelist();
          } else {
            message.error({
              content: `${res.msg}`,
              duration: 3,
            });
          }
        })
        .finally(() => {
          hide();
          modalState.confirmLoading = false;
        });
    },
  });
}

/**
 * 记录多项选择
 */
function fnRecordMore(type: string | number, row: Record<string, any>) {
  switch (type) {
    case 'delete':
      fnRecordDelete(row.id);
      break;
    case 'start':
      fnNeStart(row);
      break;
    case 'restart':
      fnNeRestart(row);
      break;
    case 'stop':
      fnNeStop(row);
      break;
    case 'reload':
      fnNeReload(row);
      break;
    case 'log':
      fnNeLogFile(row);
      break;
    case 'oam':
      modalState.neId = row.neId;
      modalState.neType = row.neType;
      modalState.openByOAM = !modalState.openByOAM;
      break;
    case 'backConfExport':
      backConf.value.exportConf(row.neType, row.neId);
      break;
    case 'backConfImport':
      modalState.neId = row.neId;
      modalState.neType = row.neType;
      modalState.openByBackConf = !modalState.openByBackConf;
      break;
    default:
      console.warn(type);
      break;
  }
}

/**查询列表, pageNum初始页数 */
function fnGetList(pageNum?: number) {
  if (tableState.loading) return;
  tableState.loading = true;
  if (pageNum) {
    queryParams.pageNum = pageNum;
  }
  listNeInfo(toRaw(queryParams))
    .then(res => {
      if (res.code === RESULT_CODE_SUCCESS && Array.isArray(res.rows)) {
        // 取消勾选
        if (tableState.selectedRowKeys.length > 0) {
          tableState.selectedRowKeys = [];
        }
        tablePagination.total = res.total;
        // 遍历处理资源情况数值
        tableState.data = res.rows.map(item => {
          let resouresUsage = {
            sysDiskUsage: 0,
            sysMemUsage: 0,
            sysCpuUsage: 0,
            nfCpuUsage: 0,
          };
          const neState = item.serverState;
          if (neState) {
            resouresUsage = parseResouresUsage(neState);
          } else {
            item.serverState = { online: false };
          }
          Reflect.set(item, 'resoures', resouresUsage);
          return item;
        });
      }
      tableState.loading = false;
    })
    .finally(() => {
      // 刷新缓存的网元信息
      useNeInfoStore().fnRefreshNelist();
    });
}

/**解析网元状态携带的资源利用率 */
function parseResouresUsage(neState: Record<string, any>) {
  let sysCpuUsage = 0;
  let nfCpuUsage = 0;
  if (neState.cpu) {
    nfCpuUsage = neState.cpu.nfCpuUsage;
    const nfCpu = +(nfCpuUsage / 100);
    nfCpuUsage = +nfCpu.toFixed(2);
    if (nfCpuUsage > 100) {
      nfCpuUsage = 100;
    }

    sysCpuUsage = neState.cpu.sysCpuUsage;
    const sysCpu = +(sysCpuUsage / 100);
    sysCpuUsage = +sysCpu.toFixed(2);
    if (sysCpuUsage > 100) {
      sysCpuUsage = 100;
    }
  }

  let sysMemUsage = 0;
  if (neState.mem) {
    const men = neState.mem.sysMemUsage;
    sysMemUsage = +(men / 100).toFixed(2);
    if (sysMemUsage > 100) {
      sysMemUsage = 100;
    }
  }

  let sysDiskUsage = 0;
  if (neState.disk && Array.isArray(neState.disk.partitionInfo)) {
    let disks: any[] = neState.disk.partitionInfo;
    disks = disks.sort((a, b) => +b.used - +a.used);
    if (disks.length > 0) {
      const { total, used } = disks[0];
      sysDiskUsage = +((used / total) * 100).toFixed(2);
    }
  }

  return {
    sysDiskUsage,
    sysMemUsage,
    sysCpuUsage,
    nfCpuUsage,
  };
}

onMounted(() => {
  // 初始字典数据
  Promise.allSettled([getDict('ne_info_status')]).then(resArr => {
    if (resArr[0].status === 'fulfilled') {
      dict.neInfoStatus = resArr[0].value;
    }
  });

  // 获取列表数据
  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.ne.common.neType')" name="neType ">
              <a-auto-complete
                v-model:value="queryParams.neType"
                :options="NE_TYPE_LIST.map(v => ({ value: v }))"
                allow-clear
                :placeholder="t('common.inputPlease')"
              />
            </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>
        <a-space :size="8" align="center">
          <a-button type="primary" @click.prevent="fnModalVisibleByEdit()">
            <template #icon><PlusOutlined /></template>
            {{ t('common.addText') }}
          </a-button>
          <a-button
            type="default"
            danger
            :disabled="tableState.selectedRowKeys.length <= 0"
            :loading="modalState.confirmLoading"
            @click.prevent="fnRecordDelete('0')"
          >
            <template #icon><DeleteOutlined /></template>
            {{ t('common.deleteText') }}
          </a-button>
        </a-space>
      </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" placement="bottomRight">
              <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: tableColumns.length * 120 }"
        :row-selection="{
          type: 'checkbox',
          columnWidth: '48px',
          selectedRowKeys: tableState.selectedRowKeys,
          onChange: fnTableSelectedRowKeys,
        }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'status'">
            <DictTag :options="dict.neInfoStatus" :value="record.status" />
          </template>
          <template v-if="column.key === 'id'">
            <a-space :size="8" align="center">
              <a-tooltip>
                <template #title>{{ t('common.editText') }}</template>
                <a-button
                  type="link"
                  @click.prevent="fnModalVisibleByEdit(record)"
                >
                  <template #icon><FormOutlined /></template>
                </a-button>
              </a-tooltip>
              <a-tooltip>
                <template #title>
                  {{ t('views.ne.common.restart') }}
                </template>
                <a-button
                  type="link"
                  @click.prevent="fnRecordMore('restart', record)"
                >
                  <template #icon><UndoOutlined /></template>
                </a-button>
              </a-tooltip>
              <a-tooltip placement="left">
                <template #title>{{ t('common.moreText') }}</template>
                <a-dropdown placement="bottomRight" trigger="click">
                  <a-button type="link">
                    <template #icon><EllipsisOutlined /> </template>
                  </a-button>
                  <template #overlay>
                    <a-menu @click="({ key }:any) => fnRecordMore(key, record)">
                      <a-menu-item key="log">
                        <FileTextOutlined />
                        {{ t('views.ne.common.log') }}
                      </a-menu-item>
                      <a-menu-item key="start">
                        <ThunderboltOutlined />
                        {{ t('views.ne.common.start') }}
                      </a-menu-item>
                      <a-menu-item key="stop">
                        <CloseSquareOutlined />
                        {{ t('views.ne.common.stop') }}
                      </a-menu-item>
                      <a-menu-item
                        key="reload"
                        v-if="
                          !['OMC', 'PCF', 'IMS', 'MME'].includes(record.neType)
                        "
                      >
                        <SyncOutlined />
                        {{ t('views.ne.common.reload') }}
                      </a-menu-item>
                      <a-menu-item key="delete">
                        <DeleteOutlined />
                        {{ t('common.deleteText') }}
                      </a-menu-item>
                      <a-menu-item
                        key="oam"
                        v-if="!['OMC'].includes(record.neType)"
                      >
                        <FileTextOutlined />
                        {{ t('views.ne.common.oam') }}
                      </a-menu-item>
                      <!-- 配置备份 -->
                      <a-menu-item key="backConfExport">
                        <ExportOutlined />
                        {{ t('views.ne.neInfo.backConf.export') }}
                      </a-menu-item>
                      <a-menu-item key="backConfImport">
                        <ImportOutlined />
                        {{ t('views.ne.neInfo.backConf.import') }}
                      </a-menu-item>
                    </a-menu>
                  </template>
                </a-dropdown>
              </a-tooltip>
            </a-space>
          </template>
        </template>
        <template #expandedRowRender="{ record }">
          <a-row>
            <a-col :offset="2" :lg="8" :md="8" :xs="8">
              <a-divider orientation="left">
                {{ t('views.ne.neInfo.info') }}
              </a-divider>
              <div>
                <span>{{ t('views.ne.neInfo.serviceState') }}：</span>
                <DictTag :options="dict.neInfoStatus" :value="record.status" />
              </div>
              <div>
                <span>{{ t('views.ne.neVersion.version') }}：</span>
                <span>{{ record.serverState.version }}</span>
              </div>
              <div>
                <span>{{ t('views.ne.common.serialNum') }}：</span>
                <span>{{ record.serverState.sn }}</span>
              </div>
              <div>
                <span>{{ t('views.ne.common.expiryDate') }}：</span>
                <span>{{ record.serverState.expire }}</span>
              </div>
            </a-col>
            <a-col :offset="2" :lg="8" :md="8" :xs="8">
              <a-divider orientation="left">
                {{ t('views.ne.neInfo.resourceInfo') }}
              </a-divider>
              <div>
                <span>{{ t('views.ne.neInfo.neCpu') }}：</span>
                <a-progress
                  status="normal"
                  :stroke-color="
                    record.resoures.nfCpuUsage < 30
                      ? '#52c41a'
                      : record.resoures.nfCpuUsage > 70
                      ? '#ff4d4f'
                      : '#1890ff'
                  "
                  :percent="record.resoures.nfCpuUsage"
                />
              </div>
              <div>
                <span>{{ t('views.ne.neInfo.sysCpu') }}：</span>
                <a-progress
                  status="normal"
                  :stroke-color="
                    record.resoures.sysCpuUsage < 30
                      ? '#52c41a'
                      : record.resoures.sysCpuUsage > 70
                      ? '#ff4d4f'
                      : '#1890ff'
                  "
                  :percent="record.resoures.sysCpuUsage"
                />
              </div>
              <div>
                <span>{{ t('views.ne.neInfo.sysMem') }}：</span>
                <a-progress
                  status="normal"
                  :stroke-color="
                    record.resoures.sysMemUsage < 30
                      ? '#52c41a'
                      : record.resoures.sysMemUsage > 70
                      ? '#ff4d4f'
                      : '#1890ff'
                  "
                  :percent="record.resoures.sysMemUsage"
                />
              </div>
              <div>
                <span>{{ t('views.ne.neInfo.sysDisk') }}：</span>
                <a-progress
                  status="normal"
                  :stroke-color="
                    record.resoures.sysDiskUsage < 30
                      ? '#52c41a'
                      : record.resoures.sysDiskUsage > 70
                      ? '#ff4d4f'
                      : '#1890ff'
                  "
                  :percent="record.resoures.sysDiskUsage"
                />
              </div>
            </a-col>
          </a-row>
        </template>
      </a-table>
    </a-card>

    <!-- 新增框或修改框 -->
    <EditModal
      v-model:open="modalState.openByEdit"
      :edit-id="modalState.editId"
      @ok="fnModalEditOk"
      @cancel="fnModalEditCancel"
    ></EditModal>

    <!-- OAM编辑框 -->
    <OAMModal
      v-model:open="modalState.openByOAM"
      :ne-id="modalState.neId"
      :ne-type="modalState.neType"
      @cancel="fnModalEditCancel"
    ></OAMModal>

    <!-- 配置文件备份框 -->
    <BackConfModal
      ref="backConf"
      v-model:open="modalState.openByBackConf"
      :ne-id="modalState.neId"
      :ne-type="modalState.neType"
      @cancel="fnModalEditCancel"
    ></BackConfModal>
  </PageContainer>
</template>

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