<script src="../../host.js"></script>
<template>
  <iframe
      :src="resutl_url"
      :height="height"
      border="0"
      allowtransparency="yes"
      frameborder="no"
      marginheight="0"
      marginwidth="0"
      scrolling="no"
      width="100%">
  </iframe>
</template>
<script>
import { pre, dev, prod, local, hostSuffix, path_create, path_update } from './config'

export default {
  name: 'y-design-answer-form',
  data () {
    return {
      setting: {},
      resutl_url: ''
    }
  },
  watch: {
    option: {
      handler (opt) {
        this.init(opt)
      },
      deep: true,
      immediate: true
    },
  },
  props: {
    option: {
      type: Object,
      required: true
    },
    height: {
      type: String,
      default: '99%'
    },
    token: [Function]
  },
  methods: {
    // 初始化配置
    init (opt) {
      // 初始化参数
      let { set } = new this.$tool.dataSetting.Config(this, 'setting')
      set('action', opt['action'], [
        [/dev/, 'dev'],
        [/pre/, 'pre'],
        [/prod/, 'prod'],
        [/local/, 'local'],
        [/stage/, 'stage']
      ], 'dev')

      set('host', this.setting['action'], [
        [/dev|stage/, dev()],
        [/prod/, prod()],
        [/pre/, pre()],
        [/local/, opt.host || local()]
      ])

      set('method', opt, [
        [/get/, 'get'],
        [/post/, 'post']
      ], 'get')

      set('hostSuffix', opt['action'], [
        [/prod/, '/prod-api'],
        [/stage/, '/stage-api'],
        [/dev/, '/dev-api'],
        [/pre/, '/pre-api'],
        [/local/, opt.hostSuffix || '']
      ], '/dev-api')

      set('path_create', opt, null, path_create())
      set('path_update', opt, null, path_update())

      this.setting = Object.assign(this.option, this.setting)

      if (opt.gaugeName) {
        this.add(opt.gaugeName)
      } else if (opt.shortId && opt.attach) {
        this.edit(opt.shortId, opt.attach)
      }


      // console.log('init-end', this.setting)
    },
    async add (gaugeName) {
      if (gaugeName) {
        const { host, hostSuffix, path_create } = this.setting
        this.$tool.request({
          url: host + hostSuffix + path_create,
          headers: {
            Authorization: await this.token(),
            way: 1
          },
          method: 'get',
          params: {
            gaugeName: gaugeName
          }
        }).then(res => {
          if (res.code === 200) {
            this.resutl_url = res.data
          } else {
            this.$emit('error', res)
          }
        }).catch(err => {
          this.$emit('error', err)
        })
      }
    },
    async edit (shortId, attach) {
      console.log('打开edit', (shortId && /^\w{8}$/.test(shortId)), this.$tool.valid.isJSON(attach))
      if ((shortId && /^\w{8}$/.test(shortId)) && this.$tool.valid.isJSON(attach)) {
        const { host, hostSuffix, path_update } = this.setting
        this.$tool.request({
          url: host + hostSuffix + path_update + shortId,
          method: 'get',
          headers: {
            Authorization: await this.token(),
            way: 1
          },
          params: {
            attach: attach
          }
        }).then(res => {
          if (res.code === 200) {
            this.resutl_url = res.data
          } else {
            this.$emit('error', res)
          }
        }).catch(err => {
          this.$emit('error', err)
        })
      } else {
        this.$emit('error', {
          msg: '传输参数问题'
        })
      }
    },
  }
}
</script>
<style scoped>

</style>
