<template>
  <div class="module-cont">
    <div class="add-perate-icon" @click.stop="addPerate">
      <img :src="icon" alt="" title="替换">
      <img
        v-if="typeof(stageData.enable) != 'undefined' && stageData.enable === false"
        class="perate-enable"
        :src="enable"
        alt=""
        title="禁用">
    </div>
    <div class="row-content">
      <el-row class="mb-10">
        使用
        <expression
          :exprType="exprType"
          messageExprType="ContentExpr"
          :messageExpr="stageData.contentExpr"
          @confirm="handleExpression" />
        <span class="isrequired">*</span>
        <span class="ml-5"><b>替换</b>变量</span>
        <el-input v-model="stageData.variable"></el-input>
        <span class="isrequired">*</span>
        <span class="ml-5">中的</span>
        <expression
          class="ml-5"
          :exprType="exprType2"
          :messageExpr="stageData.locationExpr"
          @confirm="handleLocationExpr" />
      </el-row>
      <el-row class="mb-10">
        <el-radio v-model="stageData.replaceContent" :label="false">替换整个节点</el-radio>
      </el-row>
      <el-row class="mb-10">
        <el-radio v-model="stageData.replaceContent" :label="true">替换节点内容</el-radio>
      </el-row>
    </div>
  </div>
</template>

<script>
import icon from '@/assets/img/replace.gif'
import enable from '@/assets/img/enable.gif'
import Expression from '../expression'
import { createNamespacedHelpers } from 'vuex'

const { mapGetters } = createNamespacedHelpers('app')
export default {
  name: 'Report',
  components: {
    Expression
  },
  data() {
    return {
      icon: icon,
      enable: enable,
      exprType: [],
      exprType2: []
    }
  },
  props: {
    index: {
      type: Number,
      default: 0
    },
    stageData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  computed: {
    ...mapGetters(['payloadType'])
  },
  watch: {
    stageData: {
      handler: function(val) {
        if (!val.contentExpr) {
          this.$set(val, 'contentExpr', {})
        }
        if (!val.locationExpr) {
          this.$set(val, 'locationExpr', {})
        }
        if (!val.replaceContent) {
          this.$set(val, 'replaceContent', false)
        }
      },
      deep: true,
      immediate: true
    },
    payloadType: {
      handler: function(val) {
        this.exprType = val === 'xml' ? ['simple', 'xquery', 'jsonpath'] : val === 'json' ? ['simple', 'xquery', 'jsonpath'] : ['simple']
        this.exprType2 = val === 'xml' ? ['xpath', 'xpath', 'jsonpath'] : val === 'json' ? ['xpath', 'jsonpath'] : []
      },
      immediate: true
    }
  },
  methods: {
    handleExpression(data) {
      console.log(data)
      this.$set(this.stageData.contentExpr, 'exprType', data.exprType)
      this.$set(this.stageData.contentExpr, 'valueExpr', data.valueExpr)
      this.$set(this.stageData.contentExpr, 'resourcePath', data.resourcePath)
      this.$set(this.stageData.contentExpr, 'resourceId', data.resourceId)
      this.$set(this.stageData.contentExpr, 'resourceType', data.resourceType)
    },
    handleLocationExpr(data) {
      this.$set(this.stageData.locationExpr, 'exprType', data.exprType)
      this.$set(this.stageData.locationExpr, 'valueExpr', data.valueExpr)
    },
    addPerate(e) {
      console.log(this.stageData)
      this.$emit('confirm', e, '0', this.index)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
