export function CrossTemplate () {
  return {
    id: '',
    tunnelId: '',
    type: '',
    name: '',
    rightHole: '',
    leftHole: '',
    width: null,
    height: null
  }
}

export function SectionTemplate (holeId = '') {
  return {
    id: '',
    type: '',
    name: '',
    holeId,
    markerlineType: '',
    mileList: [{
      mileStart: null,
      mileEnd: null
    }]
  }
}

export function RoadTemplate (tunnelId = '') {
  return {
    id: '',
    tunnelId,
    xlwTunnelId: null,
    holeName: '',
    rodaName: '',
    directionUpdown: '',
    directionName: '',
    mileStart: null,
    mileEnd: null,
    length: null,
    width: null,
    height: null,
    limitHeight: null,
    laneNum: null,
    emergencyNum: null,
    orderNo: null,
    makerlines: [
      new SectionTemplate()
    ]
  }
}

export function FacilityTemplate () {
  return {
    tunnelCode: '',
    tunnelName: '',
    deptId: '',
    road: '',
    latitude: '',
    longitude: '',
    tunnelType: '',
    tunnelDesignType: '0',
    length: null,
    mileStart: null,
    mileEnd: null,
    crossNum: null,
    trafficFlow: null,
    sort: null,
    modules: ''
  }
}

export function EquipmentTemplate () {
  return {
   id: '',
   provincecenter: '',
   areacenter: '',
   tunnelgroup: '',
   tunnelname: '',
   holePosition: '',
   directionUpdown: '',
   deviceId: '',
   deviceCode: '',
   deviceType: '',
   module: '',
   deviceName: '',
   mileStart: '',
   mileEnd: '',
   mileStartOffset: '',
   line: '',
   axisX: '',
   axisY: '',
   length: null,
   plcCode: '',
   modelVersion: '',
   road: '',
   trafficEventDeviceId: '',
   deviceIp: '',
   deviceUuid: ''
  }
}

export function noEmpty (_, value, callback) {
  const ptn1 = /[^\w_\-\+\u4e00-\u9fa5]+/g
  const ptn2 = /(^\s*)|(\s*$)/g
  if (ptn1.test(value) || (value || '').replace(ptn2, '').length === 0) {
    callback(false)
  } else {
    callback()
  }
}

export function textAble (_, value, callback) {
  const ptn = /(^\s*)|(\s*$)/g
  if (value.replace(ptn, '').length === 0) {
    callback(false)
  } else {
    callback()
  }
}

const isEmpty = (str) => {
  return str === null || str === undefined || str === ''
}

export function tunnelCheck (rows) {

  return rows.some(item => {

    const { type, name, markerlineType, mileList } = item
    const ptn = /(^\s*)|(\s*$)/g

    const unStatus = mileList.some(xterm => {
      const { mileStart, mileEnd } = xterm
      return isEmpty(mileStart) || isEmpty(mileEnd)
    })

    const unfirm = Object.keys(item).some(key => {

      if (key === 'type' && item.type === 'type_01' && isEmpty(markerlineType)) {
        return true
      }

      return isEmpty(type) || isEmpty(name)

    })

    return unfirm || unStatus

  })

}

export function facilityCheck (rows) {

  const freekeys = ['id', 'tunnelId']

  return rows.some(item => {
    return Object.keys(item).some(key => {
      return !freekeys.includes(key) && isEmpty(item[key])
    })
  })

}