import BigNumber from "bignumber.js"
import {convertLockMsg,syncMsgbox} from "./index";

export function findCodeLabel(codes,val)
{
    if(!codes)
        return val;
    let item = codes.find(item=>item.value==val)
    if(!item)
        return val;
    return item.label
} 

export function dateFormat (date, format) {
    if(!date)
      return date
    if(typeof date == 'string')
      date = new Date(date)  

    let _format = format || 'yyyy-MM-dd';
  
    const d = date;
    const o = {
      'M+' : d.getMonth() + 1, // month
      'd+' : d.getDate(), // day
      'h+' : d.getHours(), // hour
      'm+' : d.getMinutes(), // minute
      's+' : d.getSeconds(), // second
      'q+' : Math.floor((d.getMonth() + 3) / 3), // quarter
      'S' : d.getMilliseconds() // millisecond
    };
    if (/(y+)/.test(_format)) {
        _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
    
      for (const k in o) {
        if (o.hasOwnProperty(k) && new RegExp('(' + k + ')').test(_format)) {
          _format = _format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
      }
    
      return _format;
}
// 数字展示成金额
export function moneyFormat(value, lanType) {
	let num = value
	let decimals = 2
	if (lanType !== undefined && ['KRW', 'JPY'].includes(lanType)) {
		decimals = 0
	}
	if (value === null || isNaN(value)) {
		num = '0'
	}
	return new BigNumber(num).toFormat(decimals)
}

export function routerPush(route, isClickMenu) {
	let tagsArry = this.$store.state.rulebpmn.tagsArry
	// console.log('tagsArry', tagsArry)
	// console.log('route', route)
	let routerPath = typeof route === 'string' ? route : route.path
	if (isClickMenu) {
		this.$router.push(route)
		return
	}
	if (!routerPath) {
		this.$router.push(route)
		return
	}
	if (routerPath.startsWith('/business') || routerPath.startsWith('/review')) {
		let isInRouteList = false
		isInRouteList = tagsArry.some((item) => {
			return item.routePath === routerPath
		})
		if (isInRouteList) {
			this.$notify({
				title: '提示',
				message: '该类型页面已经打开,请先关闭再操作!',
				type: 'warning',
			});
		} else {
			this.$router.push(route)
		}
	} else {
		this.$router.push(route)
	}
}
export function moduleRouter(defaultFlag){
  if (defaultFlag) {
    return this.$route.meta.module || 'Business';
  } else {
    return this.$route.meta.module || 'business';
  }
}

export function trnNameRouter(){
    return this.$route.meta.trnName || '';
}

//押汇中心经办+分行经办
export function isPubJinB(){
	let role=JSON.parse(window.sessionStorage.curRole)
	let roleId=role.id+"";
	let parentRoleId=role.parentRoleId+"";
	console.log("aaaaaa",window.sessionStorage.curRole,"bbbbbbb",role,"ccccccccccc",roleId,"ddddddddd",parentRoleId);
	if (roleId==="10265" || roleId==="1502" || roleId==="10231" || parentRoleId==="10265" ){
		return true;
	}
	return false;
}

export function isPubBranchJinB(){
	let curRole=JSON.parse(window.sessionStorage.curRole);
	if(curRole.parentRoleId+""==="10265"|| curRole.id+""==="10265"){
		return true;
	}
	return false;
}
export function isPubHeadJinB() {
	if(JSON.parse(window.sessionStorage.curRole).id+""==="10255"){
		return true;
	}
	return false;
}

export function  isPubHeadCenter() {//单证中心
	let roleId=JSON.parse(window.sessionStorage.curRole).id;
	if ("-10253-".indexOf(roleId)>0 ){
		return true;
	}
	return false;
}

export function isPubFuhe(){
	let roleId=JSON.parse(window.sessionStorage.curRole).id;
	if ("-10256-10232-10253-".indexOf(roleId)>0 ){
		return true;
	}
	return false;
}

export function isPubHeadFuhe(){
	let roleId=JSON.parse(window.sessionStorage.curRole).id;
	if ("-10256-10253-".indexOf(roleId)>0 ){
		return true;
	}
	return false;
}

export function isPubBranchFuhe(){
	if(JSON.parse(window.sessionStorage.curRole).id+""==="10232"){
		return true;
	}
	return false;
}

export function isPubAdmin() {
	let roleName = JSON.parse(window.sessionStorage.curRole).name;
	if (roleName.indexOf("管理员")>0 ){
		return true;
	}
	return false;
}

export function isPubJinBRerouting() {
	let role=JSON.parse(window.sessionStorage.curRole);
	let parentRoleId=role.parentRoleId+"";
	let headId=JSON.parse(window.sessionStorage.currentOrg).departmentNumber;
	if (parentRoleId==="10265" && headId!=="1000" ){
		return true;
	}
	return false;
}

export default {
	install(Vue){
		Vue.prototype.findCodeLabel = findCodeLabel
		// Vue.prototype.dateFormat = dateFormat
		Vue.prototype.moneyFormat = moneyFormat
		Vue.prototype.routerPush = routerPush
		Vue.prototype.moduleRouter = moduleRouter
		Vue.prototype.trnNameRouter = trnNameRouter
		Vue.prototype.convertLockMsg = convertLockMsg
		Vue.prototype.$smsgbox=syncMsgbox
		Vue.prototype.isPubJinB=isPubJinB;
		Vue.prototype.isPubFuhe=isPubFuhe;
		Vue.prototype.isPubBranchJinB=isPubBranchJinB;
		Vue.prototype.isPubHeadJinB=isPubHeadJinB;
		Vue.prototype.isPubHeadFuhe=isPubHeadFuhe;
		Vue.prototype.isPubHeadCenter=isPubHeadCenter;
		Vue.prototype.isPubBranchFuhe=isPubBranchFuhe;
		Vue.prototype.isPubAdmin=isPubAdmin;
		Vue.prototype.isPubJinBRerouting=isPubJinBRerouting;
	}
}