<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">
        在变量
        <el-input v-model="stageData.variable" class="ml-5 mr-5" style="width: 200px;"></el-input>
        <span
          class="isrequired">*</span>
        <span class="ml-5 mr-5">中<b>插入</b></span>
        <expression
          :exprType="exprType"
          messageExprType="ContentExpr"
          :messageExpr="stageData.contentExpr"
          @confirm="handleExpression" />
        <span class="isrequired">*</span>
        <el-select v-model="stageData.position" placeholder="请选择" style="width: 170px;">
          <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
        <span class="isrequired">*</span>
        <expression
          class="ml-5"
          :exprType="exprType2"
          :messageExpr="stageData.locationExpr"
          @confirm="handleLocationExpr" />
      </el-row>
    </div>
  </div>
</template>

<script>
import icon from '@/assets/img/insert.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: [],
      options: [
        {
          name: '之前',
          value: 'BEFORE'
        },
        {
          name: '之后',
          value: 'AFTER'
        },
        {
          name: '作为以下项的第一个子级',
          value: 'FIRST'
        },
        {
          name: '作为以下项的最后一个子级',
          value: 'LAST'
        }
      ]
    }
  },
  props: {
    index: {
      type: Number,
      default: 0
    },
    stageData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  computed: {
    ...mapGetters(['payloadType'])
  },
  watch: {
    stageData: {
      handler: function(val) {
        if (!val.position) {
          this.$set(val, 'position', 'FIRST')
        }
        if (!val.contentExpr) {
          this.$set(val, 'contentExpr', {})
        }
        if (!val.locationExpr) {
          this.$set(val, 'locationExpr', {})
        }
      },
      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'] : val === 'json' ? ['jsonpath'] : []
      },
      immediate: true
    }
  },
  methods: {
    handleExpression(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)
    },
    handleLocationExpr(data) {
      this.$set(this.stageData.locationExpr, 'exprType', data.exprType)
      this.$set(this.stageData.locationExpr, 'valueExpr', data.valueExpr)
    },
    addPerate(e) {
      this.$emit('confirm', e, '0', this.index)
    }
  }
}
</script>

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