<template>
  <div class="edit-barcode">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :key="modalForm.id"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="背景颜色：">
        <color-select
          v-model="modalForm.bgColor"
          show-empty
          :predefine="predefineColors"
        ></color-select>
      </el-form-item>
      <el-form-item label="边框颜色：">
        <color-select v-model="modalForm.color"></color-select>
      </el-form-item>
      <el-form-item label="边框粗细：">
        <line-width-select v-model="modalForm.bold"></line-width-select>
      </el-form-item>
      <el-form-item label="边框形状：">
        <line-style-select v-model="modalForm.style"></line-style-select>
      </el-form-item>
      <el-form-item label="方向：">
        <el-input-number
          v-model="modalForm.rotation"
          :max="180"
          :min="-180"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="field" label="变量：">
        <el-select
          v-model="modalForm.field"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in fieldList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ColorSelect from "../../components/common/ColorSelect";
import LineStyleSelect from "../../components/common/LineStyleSelect";
import LineWidthSelect from "../../components/common/LineWidthSelect";
import { mapState } from "vuex";
import { objAssign } from "../../plugins/utils";

const initModalForm = {
  id: "",
  rotation: 0,
  bold: "1px",
  color: "#ffffff",
  bgColor: "#ffffff",
  style: "solid",
  field: ""
};

export default {
  name: "edit-barcode",
  components: { ColorSelect, LineStyleSelect, LineWidthSelect },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      predefineColors: ["#000000", "#666666", "#999999", "#ffffff"],
      fieldList: [],
      rules: {
        field: [
          {
            required: true,
            message: "请选择变量",
            trigger: "change"
          }
        ]
      }
    };
  },
  computed: {
    ...mapState("free", ["cardConfig"])
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      this.fieldList = [
        ...this.cardConfig.requiredFields,
        ...this.cardConfig.extendFields
      ].filter(item => item.enable);
      this.modalForm = objAssign(initModalForm, val);
      this.modalForm.field = val.fields[0] && val.fields[0].code;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      const data = { ...this.modalForm };
      data.fields = this.fieldList.filter(
        item => item.code === this.modalForm.field
      );
      const model = objAssign(this.instance, data);
      this.$emit("modified", model);
    }
  }
};
</script>
