'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var svg = require('@element-plus/icons-vue');
require('../../api/index.js');
require('./component/userSelectDialog.vue.js');
var index = require('../../api/base/index.js');
var userSelectDialog_vue_vue_type_script_setup_true_lang = require('./component/userSelectDialog.vue2.js');

const _hoisted_1 = { style: { "font-size": "14px", "line-height": "30px" } };
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "FmAutocomplete"
  },
  __name: "index",
  props: /* @__PURE__ */ vue.mergeModels({
    placeholder: {
      type: String,
      default: "\u8BF7\u8F93\u5165\u5FEB\u901F\u641C\u7D22\u9009\u62E9"
    },
    /**
     * ato service name
     */
    atoService: {
      type: String,
      default: "planOrder"
    },
    /**
    * ato service 下的方法
    */
    atoAction: {
      type: String,
      default: "autoQuerySearch"
    },
    top: {
      type: Number,
      default: 15
    },
    /**
      * 值的属性值
      */
    vId: {
      type: String,
      default: "id"
    },
    /**
     * 值的属性标签
     */
    vName: {
      type: String,
      default: "name"
    },
    /**
     * 查询业务表ID
     */
    qId: {
      type: Number,
      default: 0
    },
    params: {
      type: Object,
      default: () => {
      }
    },
    /**
     * 打开类型 1 用户 2 单位
     * @default 1
     */
    openType: {
      type: Number,
      default: 1
    },
    /**
     * 打开参数
     */
    openParams: {
      type: Object,
      default: () => {
      }
    }
  }, {
    "name": {
      required: true,
      default: ""
    },
    "nameModifiers": {},
    "id": {
      required: true,
      default: 0
    },
    "idModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(["select", "reset"], ["update:name", "update:id"]),
  setup(__props, { emit: __emit }) {
    const autoName = vue.useModel(__props, "name");
    const autoId = vue.useModel(__props, "id");
    const props = __props;
    const visibleopenDialog = vue.ref(false);
    const emit = __emit;
    const querySearchAsync = async (queryString, cb) => {
      var _a;
      resetValue();
      const postdata = { keywork: queryString, id: props.qId, ...props.params };
      const res = await index.useBaseApi(props.atoService).post(postdata, props.atoAction);
      const data = (_a = res.data.result) != null ? _a : [];
      if (data.length === 0) {
        return cb([]);
      }
      const results = data.map((item) => ({
        ...item,
        displayText: `${item[props.vName]}`
        // 自定义展示格式
        //fullData: item                                // 保留原始对象
      }));
      cb(results);
    };
    const handleSelect = (item) => {
      autoName.value = item[props.vName];
      autoId.value = item[props.vId];
      emit("select", item);
    };
    const resetValue = () => {
      autoId.value = 0;
      emit("reset");
    };
    const handleSelectDialogChange = (value) => {
      visibleopenDialog.value = false;
      autoName.value = value.name;
      autoId.value = value.id;
      emit("select", value);
    };
    return (_ctx, _cache) => {
      const _component_el_icon = vue.resolveComponent("el-icon");
      const _component_el_autocomplete = vue.resolveComponent("el-autocomplete");
      return vue.openBlock(), vue.createBlock(_component_el_autocomplete, {
        modelValue: autoName.value,
        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => autoName.value = $event),
        debounce: 500,
        "fetch-suggestions": querySearchAsync,
        placeholder: props.placeholder,
        "trigger-on-focus": false,
        onSelect: handleSelect,
        style: { "margin-top": "-2px" }
      }, {
        default: vue.withCtx(({ item }) => [
          vue.createElementVNode(
            "div",
            _hoisted_1,
            vue.toDisplayString(item[__props.vName]),
            1
            /* TEXT */
          )
        ]),
        append: vue.withCtx(() => [
          vue.renderSlot(_ctx.$slots, "append", {}, () => [
            vue.createVNode(_component_el_icon, {
              color: "#E6A23C",
              onClick: _cache[0] || (_cache[0] = () => visibleopenDialog.value = true)
            }, {
              default: vue.withCtx(() => [
                vue.createVNode(vue.unref(svg.Search))
              ]),
              _: 1
              /* STABLE */
            }),
            vue.createVNode(userSelectDialog_vue_vue_type_script_setup_true_lang.default, {
              visible: visibleopenDialog.value,
              "onUpdate:visible": _cache[1] || (_cache[1] = ($event) => visibleopenDialog.value = $event),
              onChange: handleSelectDialogChange
            }, null, 8, ["visible"])
          ])
        ]),
        _: 3
        /* FORWARDED */
      }, 8, ["modelValue", "placeholder"]);
    };
  }
});

exports.default = _sfc_main;
