import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { Modal, message } from 'ant-design-vue/es';
import useI18n from '@/hooks/useI18n';
import { useRouter } from 'vue-router';
import { updateNeConfigReload } from '@/api/configManage/configParam';
import { serviceNeAction } from '@/api/ne/neInfo';
import useMaskStore from '@/store/modules/mask';

export default function useNeOptions() {
  const router = useRouter();
  const { t } = useI18n();
  const maskStore = useMaskStore();

  /**
   * 网元启动
   * @param row {neName,neType,neId}
   */
  function fnNeStart(row: Record<string, any>) {
    Modal.confirm({
      title: t('common.tipTitle'),
      content: t('views.ne.common.startTip'),
      onOk() {
        const hide = message.loading(t('common.loading'), 0);
        serviceNeAction({
          neType: row.neType,
          neId: row.neId,
          action: 'start',
        })
          .then(res => {
            if (res.code === RESULT_CODE_SUCCESS) {
              message.success(t('common.operateOk'), 3);
            } else {
              message.error(`${res.msg}`, 3);
            }
          })
          .finally(() => {
            hide();
          });
      },
    });
  }

  /**
   * 网元重启
   * @param row {neName,neType,neId}
   */
  function fnNeRestart(row: Record<string, any>) {
    Modal.confirm({
      title: t('common.tipTitle'),
      content: t('views.ne.common.restartTip'),
      onOk() {
        const hide = message.loading(t('common.loading'), 0);
        serviceNeAction({
          neType: row.neType,
          neId: row.neId,
          action: 'restart',
        })
          .then(res => {
            if (res.code === RESULT_CODE_SUCCESS) {
              // OMC自升级
              if (row.neType.toUpperCase() === 'OMC') {
                if (res.code === RESULT_CODE_SUCCESS) {
                  maskStore.handleMaskType('reload');
                } else {
                  message.error({
                    content: `${res.msg}`,
                    duration: 3,
                  });
                }
                return;
              }
              message.success(t('common.operateOk'), 3);
            } else {
              message.error(`${res.msg}`, 3);
            }
          })
          .finally(() => {
            hide();
          });
      },
    });
  }

  /**
   * 网元停止
   * @param row {neName,neType,neId}
   */
  function fnNeStop(row: Record<string, any>) {
    Modal.confirm({
      title: t('common.tipTitle'),
      content: t('views.ne.common.stopTip'),
      onOk() {
        const hide = message.loading(t('common.loading'), 0);
        serviceNeAction({
          neType: row.neType,
          neId: row.neId,
          action: 'stop',
        })
          .then(res => {
            if (res.code === RESULT_CODE_SUCCESS) {
              message.success(t('common.operateOk'), 3);
            } else {
              message.error(`${res.msg}`, 3);
            }
          })
          .finally(() => {
            hide();
          });
      },
    });
  }

  /**
   * 网元重新加载
   * @param row {neName,neType,neId}
   */
  function fnNeReload(row: Record<string, any>) {
    Modal.confirm({
      title: t('common.tipTitle'),
      content: t('views.ne.common.reloadTip'),
      onOk() {
        const hide = message.loading(t('common.loading'), 0);
        updateNeConfigReload(row.neType, row.neId)
          .then(res => {
            if (res.code === RESULT_CODE_SUCCESS) {
              message.success(t('common.operateOk'), 3);
            } else {
              message.error(`${res.msg}`, 3);
            }
          })
          .finally(() => {
            hide();
          });
      },
    });
  }

  /**
   * 跳转网元日志文件页面
   * @param row {neType,neId}
   */
  function fnNeLogFile(row: Record<string, any>) {
    router.push({
      name: 'NeFile_2123',
      query: {
        neType: row.neType,
        neId: row.neId,
      },
    });
  }

  return { fnNeStart, fnNeRestart, fnNeStop, fnNeReload, fnNeLogFile };
}
