<template>
  <div class="subfield-item-container" :class="{'is-active-subfield-item' : activeUid == comChildModuleInfo.uid}" :style="styles"
    @click="activeSubfieldItem">
    <img src="@/assets/images/consultingAgencyManagement/customForm/icon_drag@2x.png" alt="" class="subfield-module-item-dragg-target-icon">
    <el-form-item class="subfield-module-form-item" :class="{'is-required' : comChildModuleInfo.formAttribute.required}"
      :label="comChildModuleInfo.formAttribute.label">
      <!-- 输入框类型 -->
      <el-input v-model="comChildModuleInfo.componentAttribute.value" :placeholder="comChildModuleInfo.componentAttribute.placeholder" clearable
        v-if="comChildModuleInfo.comType == 'el-input'"></el-input>
    </el-form-item>
    <img src="@/assets/images/consultingAgencyManagement/customForm/icon_delete@2x.png" alt="" class="remove-subfield-item-icon"
      @click.stop="removeModuleItem">
  </div>
</template>
<script>
export default {
  name: "subfieldItem",
  props: {
    childModuleInfo: Object,
    parentUid: String,
    activeUid: String
  },
  watch: {
    childModuleInfo: {
      handler(newValue) {
        this.comChildModuleInfo = JSON.parse(JSON.stringify(newValue));
      },
      deep: true
    },
    comChildModuleInfo: {
      handler(newValue) {
        this.$mitt.emit("subfieldItemChange", newValue);
      },
      deep: true
    }
  },
  data() {
    return {
      comChildModuleInfo: JSON.parse(JSON.stringify(this.childModuleInfo)),
    };
  },
  //可访问data属性
  created() {
    this.$mitt.on("fieldOptionChange", this.setFieldOption);
  },
  beforeDestroy() {
    this.$mitt.off("fieldOptionChange");
  },
  //计算集
  computed: {
    styles() {
      return {
        width: this.comChildModuleInfo.formAttribute ? `${this.comChildModuleInfo.formAttribute.width}%` : "100%"
      };
    }
  },
  //方法集
  methods: {
    setFieldOption(value) {
      this.comChildModuleInfo = value;
    },
    removeModuleItem() {
      this.$emit("removeModuleItem", this.comChildModuleInfo, this.parentUid);
    },
    activeSubfieldItem() {
      if (this.activeUid == this.comChildModuleInfo?.uid) return;
      this.$emit("activeSubfieldItem", this.comChildModuleInfo, this.parentUid);
    }
  },
}
</script>
<style lang="scss" scoped>
.subfield-item-container {
  width: 100%;
  min-height: 56px;
  padding: 0px 16px 0px 32px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  background: #dcebff;

  transition: width 0.3s linear;

  &.is-active-subfield-item {
    border: 1px solid #0081ff;
  }

  .subfield-module-item-dragg-target-icon {
    width: 14px;
    height: 14px;
    cursor: move;
  }

  ::v-deep .subfield-module-form-item {
    display: flex;

    .el-form-item__label {
      line-height: 32px;
      font-size: 14px;
      font-weight: 400;
      color: rgba(35, 35, 35, 0.8);
      white-space: nowrap;
    }
    .el-form-item__content {
      flex: 1;
    }
  }

  .remove-subfield-item-icon {
    width: 16px;
    height: 16px;
    margin-left: 12px;
    cursor: pointer;
  }
}
</style>
