<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="mt-5">
        <span class="service">
          <i class="el-icon-arrow-left"></i>
          <el-link type="primary" class="dis-inline" @click="targetVisible = true">{{stageData.name ? stageData.name : '服务'}}</el-link>
          <i class="el-icon-arrow-right"></i>
        </span>
        <span class="isrequired">*</span>
        <b class="ml-5">的服务标注</b>
        <el-select
          v-if="isNames"
          v-model="names"
          clearable
          placeholder="请选择"
          style="width: 40%;"
          >
          <el-option v-for="item in namesList" :key="item.operationName" :label="item.operationName" :value="item.operationName">
          </el-option>
        </el-select>
      </el-row>
    </div>
    <choose-target-dialog :flowId="flowId" v-model="targetVisible" @confirm="handleTarget" />
  </div>
</template>

<script>
import icon from '@/assets/img/webservice_callout.gif'
import enable from '@/assets/img/enable.gif'
import ChooseTargetDialog from '@/components/target/ChooseTargetDialog'
export default {
  name: 'Report',
  components: {
    ChooseTargetDialog
  },
  data() {
    return {
      icon: icon,
      enable: enable,
      namesList: [],
      names: null,
      isNames: false,
      targetVisible: false
    }
  },
  props: {
    index: {
      type: Number,
      default: 0
    },
    stageData: {
      type: Object,
      default() {
        return {}
      }
    },
    flowId: {
      type: String,
      default() {
        return ''
      }
    }
  },
  watch: {
    names: {
      handler: function(val) {
        this.namesList.forEach(item => {
          if (item.operationName === val) {
            const items = {
              operationNamespace: item.nameSpace,
              operationName: item.operationName
            }
            this.$set(this.stageData, 'operation', items)
          }
        })
      },
      deep: true,
      immediate: true
    },
    stageData: {
      handler: function(val) {
        console.log(val)
        if (val.operation) {
          this.isNames = true
          if (this.namesList.length === 0) {
            this.namesList = val.refService.serviceParamsMap.operationList
          }
          this.names = val.operation.operationName
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleTarget(data, name) {
      console.log(data, name)
      if (data.messageType && data.messageType.toLowerCase() === 'soap' && name === 'business') {
        this.isNames = true
      } else {
        this.isNames = false
      }
      if (data.serviceParamsMap.operationList) {
        this.namesList = data.serviceParamsMap.operationList
        this.names = data.serviceParamsMap.operationList[0].operationName
        console.log(this.namesList)
      }
      this.$set(this.stageData, 'name', data.name)
      this.$set(this.stageData, 'refId', data.id)
      this.$set(this.stageData, 'serviceType', data.type)
    },
    addPerate(e) {
      this.$emit('confirm', e, '0', this.index)
    }
  }
}
</script>
)
<style lang="scss" scoped>
  .service {
    display: inline-block;
    line-height: 20px;
  }
  .el-icon-arrow-left, .el-icon-arrow-right {
    vertical-align: middle;
  }
  .mt-5{
  width: 200px;
}
</style>
