<template>
  <div class="edit-text">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <el-form-item label="字号：">
        <size-select
          v-model="modalForm.fontSize"
          style="width:100%;"
        ></size-select>
      </el-form-item>
      <el-form-item label="字体：">
        <font-family-select
          v-model="modalForm.fontFamily"
          style="width:100%;"
        ></font-family-select>
      </el-form-item>
      <el-form-item label="颜色：">
        <color-select v-model="modalForm.color"></color-select>
      </el-form-item>
      <el-form-item label="加粗：">
        <el-checkbox v-model="isBold" @change="boldChange"
          >是否加粗</el-checkbox
        >
      </el-form-item>
      <el-form-item prop="contentStr" label="内容：">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="modalForm.contentStr"
          @change="contentChange"
          ref="contentTextarea"
        >
        </el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import SizeSelect from "../../components/common/SizeSelect";
import ColorSelect from "../../components/common/ColorSelect";
import FontFamilySelect from "../../components/common/FontFamilySelect";

const initModalForm = {
  id: "",
  fontSize: "10.5pt",
  color: "",
  fontFamily: "",
  fontWeight: 400,
  rotation: 0,
  content: [],
  contentStr: ""
};

export default {
  name: "edit-text",
  components: {
    SizeSelect,
    ColorSelect,
    FontFamilySelect
  },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      isBold: false,
      rules: {
        contentStr: [
          {
            required: true,
            message: "请输入文本内容",
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      const contentStr = val.content
        .map(item => {
          return item.type === "text"
            ? item.content
            : "${" + item.content + "}";
        })
        .join("");
      this.modalForm = { ...val, contentStr };
      this.isBold = val.fontWeight > 400;
    },
    boldChange(isBold) {
      this.modalForm.fontWeight = isBold ? 700 : 400;
    },
    contentChange() {
      const constentStr = this.modalForm.contentStr;
      const rexp = new RegExp(/\$\{.+?\}/, "g");
      const variates = constentStr.match(rexp);
      const texts = constentStr.split(rexp);
      let contents = [];

      texts.forEach((text, index) => {
        if (text)
          contents.push({
            type: "text",
            content: text
          });

        if (variates && variates[index])
          contents.push({
            type: "variate",
            content: variates[index].replace("${", "").replace("}", "")
          });
      });
      this.modalForm.content = contents;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;
      this.$emit("modified", this.modalForm);
    }
  }
};
</script>
