<template>
  <a-select
    v-model:value="value"
    show-search
    :placeholder="$t('lang.NeedApproveWorkflow.all')"
    style="width: 200px"
    allow-clear
    :options="approveTypes"
    :field-names="{ label: 'windowName', value: 'windowNo' }"
    :filter-option="filterOption"
    @change="getWindowNo"
  />
</template>
<script setup>
import { ref, defineEmits } from 'vue';
import Common from '../common/Common.js';
import { message } from 'ant-design-vue';

const emit = defineEmits(['getType']);
// const model = defineModel();
const approveTypes = ref([]);
const value = ref(undefined);

// 获取所选类型
const getWindowNo = windowNo => {
  emit('getType', windowNo);
};

// 加载工作流类型
const loadWorkflowTypes = () => {
  $.ajax({
    url: Common.getApiURL('WorkflowResource/approveType'),
    type: 'GET',
    dataType: 'json',
    beforeSend: function (request) {
      Common.addTokenToRequest(request);
    },
    success: function (data) {
      if (data.errorCode == 0) {
        approveTypes.value = data.datas;
      } else {
        message.error(data.errorMessage);
      }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      Common.processException(XMLHttpRequest, textStatus, errorThrown);
    },
  });
};

loadWorkflowTypes();

const filterOption = (input, option) => {
  return option.windowName.toLowerCase().indexOf(input.toLowerCase()) >= 0;
};
</script>