<template>
  <el-select
    v-model="selected"
    class="place-select"
    :placeholder="placeholder"
    filterable
    :clearable="clearable"
    :disabled="disabled"
    @change="select"
  >
    <el-option
      v-for="(item, index) in optionList"
      :key="index"
      :value="item"
      :label="item"
    >
    </el-option>
  </el-select>
</template>

<script>
import { placeQuery } from "../../modules/print/api";

export default {
  name: "place-select",
  props: {
    disabled: { type: Boolean, default: false },
    placeholder: { type: String, default: "请选择" },
    value: { type: [Number, String], default: "" },
    clearable: { type: Boolean, default: true },
    printPlanId: { type: [String, Array], default: "" }
  },
  data() {
    return {
      optionList: [],
      selected: ""
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      }
    },
    printPlanId(val, oldval) {
      if (val !== oldval) {
        this.search("");
        this.$emit("input", "");
        this.$emit("change", {});
      }
    }
  },
  async created() {
    this.search();
  },
  methods: {
    async search(query) {
      const res = await placeQuery({
        param: query,
        printPlanId: this.printPlanId
      });
      this.optionList = res;
    },
    select() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    }
  }
};
</script>
