<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>
          <a-select ref="select" v-model:value="fake" style="width: 120px">
            <a-select-option :value="0">查看全部</a-select-option>
            <a-select-option :value="1">气氛组</a-select-option>
            <a-select-option :value="2">真实用户</a-select-option>
          </a-select>
          <a-input-search
            allow-clear
            v-model:value="searchText"
            placeholder="请输入搜索关键词"
            style="width: 200px"
            @search="reload"
            @pressEnter="reload"
          />
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'avatar'">
          <a-avatar :src="record.avatar" />
        </template>
        <template v-if="column.key === 'action'">
          <a-space>
            <a-button type="link">选择</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 { pageUsers } from '@/api/system/user';
  import { User, UserParam } from '@/api/system/user/model';
  import { EleProTable } from 'ele-admin-pro';
  import useSearch from '@/utils/use-search';

  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 fake = ref<number>(0);
  // 表单数据
  const { where } = useSearch<UserParam>({
    userId: undefined,
    nickname: undefined,
    isStaff: true,
    keywords: ''
  });

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

  // 表格配置
  const columns = ref<ColumnItem[]>([
    // {
    //   key: 'index',
    //   width: 48,
    //   align: 'center',
    //   fixed: 'left',
    //   hideInSetting: true,
    //   customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    // },
    {
      title: 'ID',
      dataIndex: 'userId'
    },
    {
      title: '用户名',
      dataIndex: 'username'
    },
    {
      title: '头像',
      key: 'avatar',
      dataIndex: 'avatar'
    },

    {
      title: '姓名',
      dataIndex: 'nickname'
    },
    {
      title: '联系电话',
      dataIndex: 'phone',
      key: 'phone'
    },
    {
      title: '所属部门',
      dataIndex: 'organizationName'
    },
    {
      title: '操作',
      key: 'action',
      align: 'center',
      hideInSetting: true
    }
  ]);

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

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

  const handleSearch = () => {
    tableRef?.value?.reload({ page: 1, where });
  };

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