<template>
  <div class="set-field-option-container">
    <el-form class="set-field-option-form" ref="customDesignFormRef" :model="comActiveFieldData">
      <!-- 字段名称 -->
      <el-form-item label="字段名称" class="set-field-option-item" prop="formAttribute.fieldName" v-if="comActiveFieldData.formAttribute"
        :rules="setFieldOptionRules.fieldName">
        <el-input v-model="comActiveFieldData.formAttribute.fieldName" placeholder="请输入字段名称" clearable></el-input>
      </el-form-item>
      <!-- label展示名称 -->
      <el-form-item label="字段label" class="set-field-option-item" prop="formAttribute.label" v-if="comActiveFieldData.formAttribute">
        <el-input v-model="comActiveFieldData.formAttribute.label" placeholder="请输入字段label" clearable></el-input>
      </el-form-item>
      <!-- placeholder文字 -->
      <el-form-item label="提示文字" class="set-field-option-item" prop="componentAttribute.placeholder" v-if="comActiveFieldData.componentAttribute">
        <el-input v-model="comActiveFieldData.componentAttribute.placeholder" placeholder="请输入提示文字" clearable></el-input>
      </el-form-item>
      <!-- 默认值 -->
      <el-form-item label="默认值" class="set-field-option-item" prop="componentAttribute.value" v-if="comActiveFieldData.componentAttribute">
        <el-input v-model="comActiveFieldData.componentAttribute.value" placeholder="请输入默认值" clearable></el-input>
      </el-form-item>

      <!-- 表单样式 -->
      <div class="set-field-option-styles-item" v-if="comActiveFieldData.formAttribute">
        <span class="set-field-option-label">字段宽度</span>
        <div class="set-field-option-block">
          <el-radio-group v-model="comActiveFieldData.formAttribute.width">
            <el-radio-button :label="100">100%</el-radio-button>
            <el-radio-button :label="50">50%</el-radio-button>
            <el-radio-button :label="33">33%</el-radio-button>
            <el-radio-button :label="25">25%</el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <div class="set-field-option-styles-item" v-if="comActiveFieldData.formAttribute">
        <span class="set-field-option-label">字段控制</span>
        <div class="set-field-option-block">
          <el-radio-group v-model="comActiveFieldData.formAttribute.required">
            <el-radio :label="false">选填</el-radio>
            <el-radio :label="true">必填</el-radio>
          </el-radio-group>
        </div>
      </div>
    </el-form>
    <!-- 确定编辑 -->
    <div class="set-field-option-footer">
      <div class="set-field-option-inner" v-if="this.comActiveFieldData && this.comActiveFieldData.uid">
        <el-button @click="cancelSet">取消</el-button>
        <el-button type="primary" @click="formCheck">确定</el-button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "setFieldOption",
  props: {
    // 当前命中的表单组件数据
    activeFieldData: {
      type: Object,
      default: () => ({
        formAttribute: {},
        componentAttribute: {},
        componentOptions: {}
      })
    },
    parentUid: String,
    activeUid: String
  },
  watch: {
    "activeFieldData.uid": {
      handler(newValue) {
        this.$refs["customDesignFormRef"].clearValidate();
      }
    },
    activeFieldData: {
      handler(newValue) {
        // 通过保存更新源数据按钮
        this.comActiveFieldData = JSON.parse(JSON.stringify(newValue));
      },
      deep: true
    },
    comActiveFieldData: {
      handler(newValue) {
        // 通过保存更新源数据按钮
        this.$mitt.emit("fieldOptionChange", newValue);
      },
      deep: true
    }
  },
  data() {
    const fieldNameValidor = (rule, value, callback) => {
      const reg = /^[a-zA-Z0-9_]+$/;
      const numReg = /^[0-9]/;
      if (!value) {
        return callback(new Error("请输入字段名称"));
      }
      if (numReg.test(value)) {
        return callback(new Error("字段名称不能以数字开头"));
      }
      if (!reg.test(value)) {
        return callback(new Error("字段名称只能为数字、字母(大小写)、下划线组成"));
      }
      callback();
    };
    return {
      // 通过保存更新源数据按钮
      comActiveFieldData: JSON.parse(JSON.stringify(this.activeFieldData)),
      setFieldOptionRules: {
        fieldName: [{ required: true, trigger: ["blur", "change"], validator: fieldNameValidor }]
      }
    };
  },
  //可访问data属性
  created() {
    this.$mitt.on("subfieldItemChange", this.setOptions);
  },
  beforeDestroy() {
    this.$mitt.off("subfieldItemChange");
  },
  //计算集
  computed: {

  },
  //方法集
  methods: {
    setOptions(value) {
      this.comActiveFieldData = value;
    },
    cancelSet() {
      this.$emit("cancelSetOptions");
    },
    async formCheck() {
      try {
        const result = await this.$refs["customDesignFormRef"].validate();
        if (result) {
          this.$emit("setOptionsFinish", this.comActiveFieldData, this.parentUid);
        }
      } catch (error) {
        console.log(error);
      }
    }
  },
}
</script>
<style lang="scss" scoped>
.set-field-option-container {
  width: 100%;
  height: 100%;

  .set-field-option-form {
    width: 100%;
    height: calc(100% - 56px);
    /* height: 100%; */
    padding: 16px;
    overflow-y: auto;
    overflow-x: hidden;
    box-sizing: border-box;
  }

  .set-field-option-footer {
    height: 56px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    border-top: 1px solid #eeeeee;
    box-sizing: border-box;
    padding: 0px 12px;

    .set-field-option-inner {
      button {
        height: 32px;
        line-height: 32px;
        padding: 0px;
        width: 60px;
        border-radius: 4px;
      }
    }
  }

  ::v-deep .set-field-option-item {
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;

    .el-form-item__label {
      color: rgba(35, 35, 35, 0.8);
      font-size: 14px;
      font-weight: 400;
      line-height: 22px;
      float: unset;
      text-align: left;
      margin-bottom: 8px;
    }

    .el-form-item__content {
      .el-input {
        .el-input__inner {
          height: 32px;
          line-height: 32px;
          border-radius: 2px;
          color: #232323;
        }
      }
    }
  }

  ::v-deep .set-field-option-styles-item {
    display: flex;
    flex-direction: column;
    border-top: 1px solid #eeeeee;
    padding-top: 20px;
    box-sizing: border-box;
    .set-field-option-label {
      font-size: 14px;
      font-weight: 400;
      color: rgba(35, 35, 35, 0.8);
      line-height: 22px;
      margin-bottom: 8px;
    }
    .set-field-option-block {
      display: flex;
      align-items: center;

      .el-radio-group {
        display: flex;
        align-items: center;

        .el-radio-button__orig-radio:checked + .el-radio-button__inner {
          background-color: #0081ff;
          border-color: #0081ff;

          &:hover {
            color: #fff;
          }
        }

        .el-radio__input.is-checked .el-radio__inner {
          background: #0081ff;
          border-color: #0081ff;
        }

        .el-radio-button__inner {
          width: 70px;
          height: 38px;
          line-height: 38px;
          padding: 0px;
          display: flex;
          justify-content: center;
          box-sizing: border-box;

          &:hover {
            color: #0081ff;
          }
        }
      }
    }
  }
}
</style>
