<template>
  <div>
    <slot></slot>
  </div>
</template>
<script>
  import utils from '@/utils.js'
  import emitter from '@/mixins/emitter.js'
  import { Notification } from 'element-ui'
  const { Base64 } = require('js-base64')
  export default {
    name: 'HtGlobalValidate',
    componentName: 'HtGlobalValidate',
    mixins: [emitter],
    props: {
      verificationJson: String,
      data: Object,
    },
    data() {
      return {
        verificationList: [],
      }
    },
    created() {
      let _this = this
      this.$on('global-validate', () => {
        _this.validate()
      })
    },
    mounted() {
      if (this.verificationJson) {
        this.verificationList = JSON.parse(Base64.decode(this.verificationJson))
      }
    },
    methods: {
      validate() {
        let result = true
        if (this.verificationList && this.verificationList.length > 0) {
          let pInst = utils.getOnlineFormInstance(this)
          let _this = this
          let messages = []
          this.verificationList.forEach((item) => {
            _this.execFun(pInst, item, messages)
          })
          if (messages.length > 0) {
            result = false
            // 判断是移动端还是pc
            if (utils.isMobile()) {
              this.$notify({
                message: messages[0],
                background: '#f25130',
              })
            } else {
              Notification.error({
                title: this.$t('ht.common.warmTips'),
                dangerouslyUseHTMLString: true,
                message: messages.join('<br/>'),
              })
            }
          }
        }
        if (result) {
          result = this.validateTable()
        }
        this.dispatch('HtOnlineForm', 'global-validate-result', [result])
      },
      execFun(pInst, item, messages) {
        let formulasDiyJs = item.formulasDiyJs
        if (formulasDiyJs.indexOf('return') === -1) {
          formulasDiyJs = 'return ' + formulasDiyJs
        }
        let Fn = Function('data', '_me', '$Formulas', formulasDiyJs) //一个变量指向Function，防止有些前端编译工具报错
        try {
          let result = Fn(pInst.data, pInst, pInst.$Formulas)
          if (result.constructor === Array) {
            result = result.filter((it) => !it.valid)
            if (result.length > 0) {
              const tableName = result[0].tableName
              const fieldName = result[0].fieldName
              const sunIndexList = Array.from(
                new Set(result.map((it) => it.sunIndex + 1))
              )
              const sunTableName = result[0].sunTableName
              const indexList = Array.from(
                new Set(result.map((it) => it.index + 1))
              )
              if (sunTableName) {
                const tips = `${tableName}：第${indexList.join(
                  ','
                )}行,【${sunTableName}】第${sunIndexList.join(
                  ','
                )}行,字段【${fieldName}】${item.tips}`
                messages.push(tips)
              } else if (tableName) {
                messages.push(
                  `${tableName}：第${indexList.join(
                    ','
                  )}行,字段【${fieldName}】${item.tips}`
                )
              } else {
                messages.push(item.tips)
              }
            }
          } else {
            if (!result) {
              messages.push(item.tips)
            }
          }
        } catch (err) {
          messages.push(`全局校验【${item.name}】脚本错误`)
        }
      },
      validateTable() {
        const tables = utils.getOnlineFormInstance(this).permission.table //获取table权限
        const boData = utils.getOnlineFormInstance(this).data //获取表单bo对象
        let newBoData = {}
        //获取所有表单字段对象
        for (let k in boData) {
          newBoData = boData[k]
        }
        for (let key in tables) {
          const subData = newBoData['sub_' + key]
          //如果有导入属性，则表明不是子表而是数据报表，不做必填校验
          if (tables[key] && tables[key].hasOwnProperty('export')) {
            continue
          }
          if ([true, 'true'].includes(tables[key].required) && subData) {
            if (subData.length == 0) {
              if (utils.isMobile()) {
                this.$notify({
                  message: '子表必填一条记录',
                  type: 'warning',
                })
              } else {
                this.$message({
                  message: '子表必填一条记录',
                  type: 'warning',
                })
              }
              return false
            } else if (
              subData.length > 0 &&
              this.validateSunTable(tables, subData).length > 0
            ) {
              if (utils.isMobile()) {
                this.$notify({
                  message: '孙表必填一条记录',
                  type: 'warning',
                })
              } else {
                this.$message({
                  message: '孙表必填一条记录',
                  type: 'warning',
                })
              }
              return false
            }
          }
        }
        return true
      },
      validateSunTable(tables, subData) {
        const requiredTableKeyList = []
        const notPassingSunTableKey = []
        //获取必填子表/孙表字段名
        for (let key in tables) {
          if ([true, 'true'].includes(tables[key].required)) {
            requiredTableKeyList.push(`sub_${key}`)
          }
        }
        subData.map((item) => {
          for (let sunKey in item) {
            if (
              requiredTableKeyList.includes(sunKey) &&
              item[sunKey].length <= 0
            ) {
              notPassingSunTableKey.push(sunKey)
            }
          }
        })
        return notPassingSunTableKey
      },
    },
  }
</script>
<style lang="scss" scoped></style>
