<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"
      :customRow="customRow"
      :pagination="false"
    >
      <template #toolbar>
        <a-space>
          <span class="text-orange-400 px-3">操作提示：双击选择</span>
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'websiteLogo'">
          <a-avatar :src="record.websiteLogo" />
        </template>
        <template v-if="column.key === 'action'">
          <a-radio @click="onRadio(record)" />
        </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 { User, UserParam } from '@/api/system/user/model';
  import { pageUsers } from '@/api/system/user';

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 标题
    title?: string;
    // 修改回显的数据
    data?: User | null;
  }>();

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

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

  // 搜索内容
  const searchText = ref(null);

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

  // 表格配置
  const columns = ref<ColumnItem[]>([
    {
      title: 'LOGO',
      dataIndex: 'websiteLogo',
      key: 'websiteLogo',
      width: 90,
      align: 'center'
    },
    {
      title: '网站名称',
      dataIndex: 'websiteName',
      key: 'websiteName'
    },
    {
      title: '域名',
      dataIndex: 'domain',
      key: 'domain'
    }
    // {
    //   title: '状态',
    //   dataIndex: 'status',
    //   key: 'status',
    //   align: 'center'
    // }
  ]);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    if (searchText.value) {
      where.keywords = searchText.value;
    }
    return pageUsers({
      ...where,
      ...orders,
      page,
      limit
    });
  };

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

  const onRadio = (record: User) => {
    updateVisible(false);
    emit('done', record);
  };

  /* 自定义行属性 */
  const customRow = (record: User) => {
    return {
      // 行点击事件
      // onClick: () => {
      //   updateVisible(false);
      //   emit('done', record);
      // },
      // 行双击事件
      onDblclick: () => {
        updateVisible(false);
        emit('done', record);
      }
    };
  };
</script>
<style lang="less"></style>
