<template>
  <div class="subfield-item-container" :class="classCreate(comChildModuleInfo.comType)" :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 == 'text'"></el-input>
      <!-- 多行文本类型 -->
      <el-input type="textarea" v-model="comChildModuleInfo.componentAttribute.value" :placeholder="comChildModuleInfo.componentAttribute.placeholder"
        :show-word-limit="false" clearable v-if="comChildModuleInfo.comType == 'textarea'"></el-input>
      <!-- 下拉框类型 -->
      <el-select v-model="comChildModuleInfo.componentAttribute.value" :multiple="comChildModuleInfo.formAttribute.isMultiple"
        :collapse-tags="comChildModuleInfo.formAttribute.isMultiple" :placeholder="comChildModuleInfo.componentAttribute.placeholder"
        v-if="comChildModuleInfo.comType == 'select'">
        <el-option v-for="(item,index) in comChildModuleInfo.formAttribute.selectOptions" :key="item.value" :label="item.value" :value="index">
        </el-option>
      </el-select>
    </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>
import { cloneDeep } from "lodash-es";
export default {
  name: "subfieldItem",
  props: {
    childModuleInfo: Object,
    parentUid: String,
    activeUid: String
  },
  watch: {
    childModuleInfo: {
      handler(newValue) {
        this.comChildModuleInfo = cloneDeep(newValue);
      },
      deep: true
    },
    comChildModuleInfo: {
      handler(newValue) {
        this.$mitt.emit("subfieldItemChange", newValue);
      },
      deep: true
    }
  },
  data() {
    return {
      comChildModuleInfo: cloneDeep(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: {
    classCreate(comType) {
      const classParams = {
        'is-active-subfield-item': this.activeUid == this.comChildModuleInfo.uid,
        [`custom-subfielditem-${comType}`]: true
      };
      return classParams;
    },
    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: 12px 16px 12px 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;
  }

  /* 多行文本样式 */
  &.custom-subfielditem-textarea {
    .subfield-module-item-dragg-target-icon {
      align-self: flex-start;
      margin-top: 4px;
    }
    .remove-subfield-item-icon {
      align-self: flex-start;
      margin-top: 4px;
    }
    ::v-deep .subfield-module-form-item {
      .el-form-item__label {
        line-height: 22px;
      }
      .el-textarea {
        .el-textarea__inner {
          resize: unset;
          min-height: 76px !important;
        }
      }
    }
  }

  /* 下拉选择框 */
  &.custom-subfielditem-select {
    ::v-deep .el-form-item {
      .el-select {
        width: 100%;
        .el-input__inner {
          border-radius: 2px;
        }
      }
    }
  }
}
</style>
