<script setup>
import InfiniteSelect from "@/components/InfiniteSelect";
import { reactive, ref, toRefs } from "vue";
import { agentList } from "@/api/dataList/agent";

const props = defineProps({
  modelValue: {
    type: Number,
  },
  show: {
    type: Boolean,
    default: false,
  },
  defaultLabel: {
    type: String,
  },
});
const formRef = ref();
const emit = defineEmits(["update:show", "confirm"]);
const data = reactive({
  form: {},
});
const { form } = toRefs(data);
const reset = () => {
  form.value = {};
  formRef.value?.resetFields();
};
const submit = () => {
  emit("confirm", form.value);
};

const open = () => {
  reset();
};
const close = () => {
  reset();
  emit("update:show", false);
};
const handleConfirmBroker = (option) => {
  form.value.brokerName = option.name;
};
const loadBrokerOptions = (query) => agentList(query).then((resp) => resp.rows);
</script>

<template>
  <el-dialog
    :model-value="show"
    :width="500"
    append-to-body
    destroy-on-close
    title="指派经纪人"
    @close="close"
    @open="open"
  >
    <el-form ref="formRef" :model="form">
      <el-form-item label="经纪人">
        <infinite-select
          v-model="form.brokerId"
          :default-label="defaultLabel"
          :prop="{ label: 'name', value: 'brokerId' }"
          :query="{ page: 'pageNum', size: 'pageSize', searchKey: 'name' }"
          :remote-method="loadBrokerOptions"
          suffix="plus"
          @confirm="handleConfirmBroker"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
