<template>
  <el-dropdown
    trigger="click"
    class="line-style-select custom-select"
    placement="bottom-start"
    @command="select"
  >
    <div class="select-preview">
      <div class="select-preview-main">
        <div
          class="style-item"
          :style="{ borderBottomStyle: selected }"
          v-if="selected"
        ></div>
      </div>
      <div class="select-preview-icon">
        <i class="el-icon-arrow-down"></i>
      </div>
    </div>
    <el-dropdown-menu slot="dropdown" class="line-style-menu">
      <el-dropdown-item command="none" v-if="showEmpty">无</el-dropdown-item>
      <el-dropdown-item
        v-for="(option, index) in optionList"
        :key="index"
        :command="option"
      >
        <div class="style-item" :style="{ borderBottomStyle: option }"></div>
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
const PREDEFINE_OPTIONS = ["dotted", "dashed", "solid"];

export default {
  name: "line-style-select",
  props: {
    value: String,
    predefine: Array,
    showEmpty: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
  },
  created() {
    this.optionList =
      this.predefine && this.predefine.length
        ? this.predefine
        : PREDEFINE_OPTIONS;
  },
  methods: {
    select(option) {
      this.selected = option;
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>
