/*
 * @Description: 公共方法
 * @Author: zhoupeng
 * @Date: 2020-04-21 10:26:10
 * @LastEditTime: 2020-07-22 19:30:38
 */
import router from 'umi/router';
import { getQueryVariableBy, getQueryParamUrl } from '../../../utils/helper';

const routeArr = [];
/**
 * @description: 记录路由
 * @param {type}
 * @return:
 * @author: zhoupeng
 */
export const replace = url => {
  router.replace(url);
};
export const goBack = url => {
  if (url) {
  } else {
    router.goBack();
  }

  // var routeNum = window.sessionStorage.getItem('router');
  // if (!routeNum) {
  //   window.sessionStorage.setItem('router', 1);
  // } else {
  //   window.sessionStorage.setItem('router', routeNum + 1);
  // }
};

export const go = num => {
  router.go(num);
};

export const push = url => {
  routeArr.push(url);
  router.push(url);
};

//跳转到首页
/*
 *原理，先回退到第一次进入的页面，然后跳转到首页
 */

export const goIndex = () => {
  const length = window.history && window.history.length ? window.history.length : 0;
  const prvLength = window.sessionStorage.getItem('historyLength');
  console.log('window.history', window.history);

  const fromPage = window.sessionStorage.getItem('fromPage');
  if (fromPage === 'scanning') {
    console.log('==============router.go', length, prvLength);
    router.go(-(length - prvLength - 1));
    router.push('/ckuh5/index');
  } else if (length) {
    router.go(-(length - prvLength));
    router.push('/ckuh5/index');
  }
};

//跳转到首页
/*
 *原理，先回退到第一次进入的页面，然后跳转到首页
 */

export const goScannerPage = () => {
  const length = window.history && window.history.length ? window.history.length : 0;
  const prvLength = window.sessionStorage.getItem('historyLength');

  const saveUrl = window.sessionStorage.getItem('queryUrl');
  var page = getQueryVariableBy(saveUrl, 'page');
  var params = getQueryParamUrl(saveUrl);

  const fromPage = window.sessionStorage.getItem('fromPage');
  console.info('-------------------/ckuh5/index', page);
  if (fromPage === 'scanning' && page) {
    console.info('-------------------2', page);
    router.push('/ckuh5/index');
    router.push(`/ckuh5/${page}?fromPage=scanning&${params}`);
  } else if (length && page) {
    console.info('-------------------3', length, page);
    router.push('/ckuh5/index');
    router.push(`/ckuh5/${page}?fromPage=scanning&${params}`);
  } else {
    console.info('-------------------4');
    router.push('/ckuh5/index');
  }
};
