<template>
  <el-select v-model="value" placeholder="全部">
    <el-option label="全部" :value="''" v-if="showAll"></el-option>
    <el-option v-for="item in options" :key="item.id" :label="item.deptName" :value="item.id"></el-option>
  </el-select>
</template>

<script>
import selectComponentJs from "../select";
import { getCreateUserDept } from "@/request/config";
import axios from "axios";

const extObj = selectComponentJs();


export default {
  ...extObj,
  watch: {
    filter() {
      this.getDeptList()
    },
    ...extObj.watch
  },
  methods: {
    filterOptions() {
      if (typeof this.filter == "string") {
        this.options = this.cache.filter((item) => {
          return this.filter == 2 ? item.id == 1 : item.id != 1;
        });
        setTimeout(() => {
          if (this.options[0]) {
            this.value = String(this.options[0].id).trim();
          }
        });
      }
    },
    async getDeptList(){
      if (this.filter) {
        let res = await axios.get(getCreateUserDept,{ params: {type:this.filter == 2?0:1 }});
        let temp = Array.isArray(res.data) ? res.data : [res.data];
        this.options = temp.map(item=>{return {...item,id:item.id.toString()}})
        console.log(this.options,'this.options');
      }
    },
    ...extObj.methods
  },
  async mounted() {
    // ret = [];
    // let res = await axios.get(getCreateUserDept,{ params: {type:1 }});
    // let coms = Array.isArray(res.data) ? res.data : [res.data];
    // _getOptions(coms);

    // this.options = ret;
    // this.cache = JSON.parse(JSON.stringify(ret));
    // this.filterOptions()
    this.getDeptList()
    this.$emit("asyncLoad");
    extObj.mounted.call(this);
  },
};
</script>
