<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :maskClosable="false"
    :title="title"
    :footer="null"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
  >
    <ele-pro-table
      ref="tableRef"
      row-key="userId"
      :datasource="datasource"
      :columns="columns"
      :pagination="false"
    >
      <template #toolbar>
        <a-space>
          <a-input-search
            allow-clear
            v-model:value="where.keywords"
            placeholder="请输入搜索关键词"
            style="width: 200px"
            @search="reload"
            @pressEnter="reload"
          />
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'roles'">
          <a-tag v-for="(item, index) in record.roles" :key="index">{{
            item.roleName
          }}</a-tag>
        </template>
        <template v-if="column.key === 'action'">
          <a-space>
            <a-button type="primary" @click="done(record)">选择</a-button>
          </a-space>
        </template>
      </template>
    </ele-pro-table>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import {
    ColumnItem,
    DatasourceFunction
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { EleProTable } from 'ele-admin-pro';
  import useSearch from '@/utils/use-search';
  import { pageShopCommunity } from "@/api/shop/shopCommunity";
  import { ShopDealerUser } from "@/api/shop/shopDealerUser/model";
  import {ShopCommunityParam} from "@/api/shop/shopCommunity/model";

  defineProps<{
    // 弹窗是否打开
    visible: boolean;
    title?: string;
    // 修改回显的数据
    data?: ShopDealerUser | null;
    type?: string;
  }>();

  const emit = defineEmits<{
    (e: 'done', data: ShopDealerUser): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单数据
  const { where } = useSearch<ShopCommunityParam>({
    userId: undefined,
    name: undefined,
    keywords: ''
  });

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格配置
  const columns = ref<ColumnItem[]>([
    {
      title: 'ID',
      dataIndex: 'id'
    },
    {
      title: '小区名称',
      dataIndex: 'name'
    },
    {
      title: '操作',
      key: 'action',
      align: 'center',
      hideInSetting: true
    }
  ]);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    where.type = 1;
    return pageShopCommunity({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /* 搜索 */
  const reload = () => {
    tableRef?.value?.reload({ page: 1, where });
  };

  const done = (record: ShopDealerUser) => {
    updateVisible(false);
    emit('done', record);
  };
</script>
<style lang="less"></style>
