<template>
  <div class="edit-text">
    <el-form
      ref="modalFormComp"
      :key="modalForm.id"
      :model="modalForm"
      :rules="rules"
      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 label="对齐：">
        <el-radio-group v-model="modalForm.textAlign">
          <el-radio-button label="left">左</el-radio-button>
          <el-radio-button label="center">居中</el-radio-button>
          <el-radio-button label="right">右</el-radio-button>
          <!-- <el-radio-button label="justify">两端</el-radio-button> -->
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="field" label="字段：">
        <el-select
          v-model="modalForm.field"
          placeholder="请选择"
          @change="fieldChange"
        >
          <el-option
            v-for="item in FIELD_LIST"
            :key="item.field"
            :label="item.name"
            :value="item.field"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="modalForm.showLabel">显示字段名称</el-checkbox>
        <el-checkbox v-model="modalForm.showUnderline"
          >显示字段内容下划线</el-checkbox
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import SizeSelect from "../../../card/components/common/SizeSelect";
import ColorSelect from "../../../card/components/common/ColorSelect";
import FontFamilySelect from "../../../card/components/common/FontFamilySelect";
import { FIELD_LIST } from "./model";

const initModalForm = {
  id: "",
  fontSize: "14px",
  color: "",
  fontFamily: "",
  fontWeight: 400,
  rotation: 0,
  textAlign: null,
  content: [],
  contentStr: "",
  showLabel: false,
  showUnderline: false,
};

export default {
  name: "EditText",
  components: {
    SizeSelect,
    ColorSelect,
    FontFamilySelect,
  },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      isBold: false,
      FIELD_LIST,
      rules: {
        field: [
          {
            required: true,
            message: "请选择字段",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      this.modalForm = { ...val };
      this.isBold = val.fontWeight > 400;
    },
    boldChange(isBold) {
      this.modalForm.fontWeight = isBold ? 700 : 400;
    },
    fieldChange(val) {
      const item = FIELD_LIST.find((elem) => elem.field === val);
      this.modalForm.fieldName = item ? item.name : "";
      this.modalForm.content = "$" + this.modalForm.fieldName;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;
      this.$emit("modified", this.modalForm);
    },
  },
};
</script>
