<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>

      <!-- 图片和文件展示 -->
      <template v-if="limitContain.includes(comActiveFieldData.comType)">
        <el-form-item label="数量限制" class="set-field-option-item set-field-option-limit" prop="formAttribute.limit"
          :rules="comActiveFieldData.formAttribute.astrict ? setFieldOptionRules.limit : {required: false}" v-if="comActiveFieldData.formAttribute">
          <el-radio-group v-model="comActiveFieldData.formAttribute.astrict" @input="astrictChange">
            <el-radio :label="false">不限制</el-radio>
            <el-radio :label="true">限制</el-radio>
          </el-radio-group>
          <!-- 限制展示数字框 -->
          <el-input-number class="limit-astrict" size="small" v-model.number="comActiveFieldData.formAttribute.limit" :min="1"
            v-if="comActiveFieldData.formAttribute.astrict"></el-input-number>
        </el-form-item>
      </template>

      <!-- label展示名称 -->
      <el-form-item label="字段label" class="set-field-option-item" prop="formAttribute.label" :rules="setFieldOptionRules.labelName"
        v-if="comActiveFieldData.formAttribute">
        <el-input v-model="comActiveFieldData.formAttribute.label" placeholder="请输入字段label" clearable></el-input>
      </el-form-item>

      <!-- placeholder文字 -->
      <template v-if="placeholderContain.includes(comActiveFieldData.comType)">
        <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>
      </template>

      <!-- 默认值 -->
      <template v-if="comActiveFieldData.formAttribute.allowDefaultValue">
        <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>
      </template>

      <!-- 下拉框配置 是否多选 -->
      <template v-if="comActiveFieldData.comType == 'select'">
        <el-form-item label="类型" class="set-field-option-item set-field-option-radio" prop="formAttribute.isMultiple"
          v-if="comActiveFieldData.formAttribute">
          <el-radio-group v-model="comActiveFieldData.formAttribute.isMultiple" @input="modeChange">
            <el-radio :label="false">单选</el-radio>
            <el-radio :label="true">多选</el-radio>
          </el-radio-group>
        </el-form-item>
      </template>

      <!-- 选项内容 -->
      <template v-if="comActiveFieldData.comType == 'select'">
        <div class="set-field-option-select-container" v-if="comActiveFieldData.formAttribute">
          <div class="option-select-title">选项内容</div>
          <!-- 选项容器可以拖动区域 -->
          <div class="option-select-content">
            <vuedraggable :list="comActiveFieldData.formAttribute.selectOptions" group="optionSelectContent" draggable=".select-option-list-item"
              ghostClass="subfield-origin-dragClass" :sort="true" handle=".select-option-list-item-drag-icon" :animation="340">
              <transition-group name="fade" tag="div" class="select-option-list">
                <div class="select-option-list-item" v-for="(item,index) of comActiveFieldData.formAttribute.selectOptions" :key="item.id">
                  <img src="@/assets/images/consultingAgencyManagement/customForm/icon_drag@2x.png" alt="" class="select-option-list-item-drag-icon">
                  <el-form-item label="" :prop="`formAttribute.selectOptions.${index}.value`" :rules="setFieldOptionRules.selectOptionsRules"
                    :show-message="false">
                    <el-input v-model="comActiveFieldData.formAttribute.selectOptions[index].value" placeholder="请输入" clearable></el-input>
                  </el-form-item>
                  <img src="@/assets/images/consultingAgencyManagement/customForm/icon_delete@2x.png" alt=""
                    class="select-option-list-item-remove-icon" @click="removeOption(item,index)">
                </div>
              </transition-group>
            </vuedraggable>
          </div>
          <!-- 添加选项容器 -->
          <div class="add-option-select-btn">
            <div class="add-option-select-btn-item" @click="addOption">
              <img src="@/assets/images/consultingAgencyManagement/customForm/icon_add@2x.png" alt="">
              <span>添加选项</span>
            </div>
          </div>
        </div>
      </template>

      <!-- 表单样式 -->
      <el-form-item label="字段宽度" class="set-field-option-item set-field-option-radio need-top-line" prop="formAttribute.width"
        v-if="comActiveFieldData.formAttribute">
        <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>
      </el-form-item>

      <el-form-item label="字段控制" class="set-field-option-item set-field-option-limit need-top-line" v-if="comActiveFieldData.formAttribute">
        <el-radio-group v-model="comActiveFieldData.formAttribute.required">
          <el-radio :label="false">选填</el-radio>
          <el-radio :label="true">必填</el-radio>
        </el-radio-group>
      </el-form-item>
    </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>
import { cloneDeep } from "lodash-es";
import { v4 } from 'uuid';
import vuedraggable from "vuedraggable";
export default {
  name: "setFieldOption",
  components: {
    vuedraggable
  },
  props: {
    // 当前命中的表单组件数据
    activeFieldData: {
      type: Object,
      default: () => ({
        formAttribute: {},
        componentAttribute: {},
        componentOptions: {}
      })
    },
    activeFieldOrigin: Object,
    parentUid: String,
    activeUid: String
  },
  watch: {
    "activeFieldData.uid": {
      handler(newValue) {
        this.$refs["customDesignFormRef"].clearValidate();
      }
    },
    activeFieldData: {
      handler(newValue) {
        // 通过保存更新源数据按钮
        this.comActiveFieldData = cloneDeep(newValue);
      },
      deep: true
    },
    comActiveFieldData: {
      handler(newValue) {
        // 通过保存更新源数据按钮
        this.$mitt.emit("fieldOptionChange", newValue);
      },
      deep: true
    },
    activeFieldOrigin: {
      handler(newValue) {
        this.comActiveFieldOrigin = cloneDeep(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();
    };
    const limitValidor = (rule, value, callback) => {
      const reg = /(^[1-9]\d*$)/;
      if (!reg.test(value)) return callback(new Error("请输入正确的数量"));
      callback();
    };
    return {
      // 通过保存更新源数据按钮
      comActiveFieldData: cloneDeep(this.activeFieldData),
      // 源数据
      comActiveFieldOrigin: cloneDeep(this.activeFieldOrigin),
      setFieldOptionRules: {
        fieldName: [{ required: true, trigger: ["blur", "change"], validator: fieldNameValidor }],
        limit: [{ required: true, type: "number", trigger: ["blur", "change"], validator: limitValidor }],
        selectOptionsRules: {
          required: true,
          message: "请输入选项值",
          trigger: ["change", "blur"]
        },
        labelName: [{ required: true, trigger: ["change", "blur"], message: "字段label不能为空" }]
      },
      placeholderContain: ["text", "textarea", "select", "date", "phone", "email"],
      limitContain: ["photo", "file"]
    };
  },
  //可访问data属性
  created() {
    this.$mitt.on("subfieldItemChange", this.setOptions);
  },
  beforeDestroy() {
    this.$mitt.off("subfieldItemChange");
  },
  //计算集
  computed: {

  },
  //方法集
  methods: {
    setOptions(value) {
      // 不是当前命中的id 不进行修改 (避免极端情况，正常不会触发 一次仅能修改一个元素)
      // console.log(this.activeUid, value.uid);
      // 不是更改的当前命中元素 
      if (this.activeUid !== value.uid) {
        return;
      };
      this.comActiveFieldData = value;
    },
    cancelSet() {
      this.$mitt.emit("fieldOptionChange", cloneDeep(this.comActiveFieldOrigin));
      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);
      }
    },
    addOption() {
      this.comActiveFieldData.formAttribute.selectOptions.push({
        value: "",
        id: v4()
      });
    },
    removeOption(item, index) {
      this.comActiveFieldData.formAttribute.selectOptions.splice(index, 1);
    },
    astrictChange(value) {
      if (value) {
        this.$set(this.comActiveFieldData.formAttribute, "limit", 1);
        return;
      }
      this.$refs["customDesignFormRef"].clearValidate("formAttribute.limit");
      this.$set(this.comActiveFieldData.formAttribute, "limit", -1);
    },
    modeChange(value) {
      if (value) {
        this.$set(this.comActiveFieldData.componentAttribute, "value", []);
        return;
      }
      this.$set(this.comActiveFieldData.componentAttribute, "value", "");
    }
  },
}
</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 {
      line-height: 32px;
      .el-input {
        .el-input__inner {
          height: 32px;
          line-height: 32px;
          border-radius: 2px;
          color: #232323;
        }
      }
      .el-input__suffix {
        display: flex;
        align-items: center;
      }
    }

    /* 下拉框样式重置 */
    &.set-field-option-limit,
    &.set-field-option-select {
      .el-form-item__label {
        padding-right: 0px;
      }
      .el-form-item__content {
        display: flex;
        align-items: center;

        .el-radio-group {
          display: flex;
          align-items: center;
          line-height: 22px;
          height: 22px;

          & > .el-radio:first-of-type {
            margin-left: 0px;
          }

          .el-radio {
            display: flex;
            align-items: center;
            margin-left: 24px;
            margin-right: 0px;

            .el-radio__input {
              width: 14px;
              height: 14px;

              .el-radio__inner {
                &::after {
                  width: 6px;
                  height: 6px;
                }
                &:hover {
                  border-color: #0081ff;
                }
              }
            }
            .el-radio__input.is-checked .el-radio__inner {
              background: #0081ff;
              border-color: #0081ff;
            }
            .el-radio__label {
              padding-left: 8px;
              color: #232323;
            }
          }
        }

        .limit-astrict {
          width: 115px;
          margin-left: 8px;
          .el-input-number__decrease,
          .el-input-number__increase {
            width: 32px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: center;

            &:hover {
              color: #0081ff;
            }
          }
          .el-input__inner {
            height: 34px;
            line-height: 34px;

            &:focus {
              border-color: #0081ff;
            }
          }
        }
      }
    }

    /* radio样式重置 */
    &.set-field-option-radio {
      .el-form-item__content {
        line-height: 22px;

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

            .el-radio__input {
              width: 14px;
              height: 14px;

              .el-radio__inner {
                &::after {
                  width: 6px;
                  height: 6px;
                }
                &:hover {
                  border-color: #0081ff;
                }
              }
            }
            .el-radio__input.is-checked .el-radio__inner {
              background: #0081ff;
              border-color: #0081ff;
            }
            .el-radio__label {
              padding-left: 8px;
              color: #232323;
              padding-right: 0px;
            }
          }

          .el-radio-button {
            display: flex;
            align-items: center;
            flex: 1;
            .el-radio-button__inner {
              flex: 1;
              height: 38px;
              display: flex;
              align-items: center;
              justify-content: center;
              border-radius: 0px;
              padding: 0px 16px;
              box-sizing: border-box;
            }
          }
        }
      }
    }

    &.need-top-line {
      padding-top: 20px;
      border-top: 1px solid #eeeeee;
      box-sizing: border-box;
    }
  }

  .set-field-option-select-container {
    width: 100%;
    background: #f5f5f5;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    padding: 12px;
    box-sizing: border-box;
    margin-bottom: 20px;

    .option-select-title {
      color: rgba(35, 35, 35, 0.8);
      font-size: 12px;
      font-weight: 400;
      margin-bottom: 8px;
    }

    .option-select-content {
      .select-option-list {
        display: flex;
        flex-direction: column;

        ::v-deep .select-option-list-item {
          display: flex;
          align-items: center;
          margin-bottom: 8px;
          height: 32px;
          width: 100%;

          .select-option-list-item-drag-icon {
            width: 14px;
            height: 14px;
            min-width: 14px;
            cursor: pointer;
          }

          .select-option-list-item-remove-icon {
            width: 16px;
            height: 16px;
            min-width: 16px;
            cursor: pointer;
            margin-left: 8px;
          }

          .el-form-item {
            margin-bottom: 0px;
            margin-left: 8px;

            &.is-error {
              .el-form-item__content {
                .el-input {
                  .el-input__inner {
                    &:focus {
                      border-color: #ff4949;
                    }
                  }
                }
              }
            }

            .el-form-item__content {
              line-height: 32px;

              .el-input {
                .el-input__inner {
                  height: 32px;
                  line-height: 32px;
                  border-radius: 2px;

                  &:focus {
                    border-color: #0081ff;
                  }
                }
                .el-input__suffix {
                  display: flex;
                  align-items: center;
                  .el-input__icon {
                    line-height: 32px;
                  }
                }
              }
            }
          }
        }
      }
    }

    .add-option-select-btn {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;

      .add-option-select-btn-item {
        width: 191px;
        height: 32px;
        border-radius: 4px;
        color: #0081ff;
        border: 1px solid #0081ff;
        cursor: pointer;

        display: flex;
        justify-content: center;
        align-items: center;

        & > img {
          width: 16px;
          height: 16px;
        }

        & > span {
          font-size: 14px;
          font-weight: 400;
          margin-left: 4px;
        }
      }
    }
  }
}
</style>
