<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="appId"
      :columns="columns"
      :customRow="customRow"
      :datasource="datasource"
      tool-class="ele-toolbar-form"
      class="sys-org-table"
    >
      <template #toolbar>
        <a-space>
          <a-input-search
            allow-clear
            v-model:value="where.appName"
            placeholder="请输入搜索关键词"
            style="width: 200px"
            @search="reload"
            @pressEnter="reload"
          />
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'appName'">
          <div class="app-box">
            <a-image
              :height="45"
              :width="45"
              :preview="false"
              :src="record.appIcon"
              fallback="https://file.wsdns.cn/20230218/550e610d43334dd2a7f66d5b20bd58eb.svg"
            />
            <div class="app-info">
              <a class="ele-text-heading">
                {{ record.appName }}
              </a>
              <span class="ele-text-placeholder">
                {{ record.companyName }}
              </span>
            </div>
          </div>
        </template>
        <template v-if="column.key === 'action'">
            <a-radio @click="onDone(record)" />
            <span class="ele-text-secondary">选择</span>
        </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 { App, AppParam } from '@/api/oa/app/model';
  import { EleProTable } from 'ele-admin-pro';
  import useSearch from '@/utils/use-search';
  import { pageApp } from '@/api/oa/app';

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

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

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

  // 表单数据
  const { where } = useSearch<AppParam>({
    appId: undefined,
    appName: undefined,
    nickname: '',
    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: '名称',
      dataIndex: 'appName',
      key: 'appName'
    },
    {
      title: '操作',
      key: 'action',
      align: 'center',
      hideInSetting: true
    }
  ]);

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

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

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

  /* 自定义行属性 */
  const customRow = (record: App) => {
    return {
      // 行点击事件
      onClick: () => {
        updateVisible(false);
        emit('done', record);
      }
    };
  };
</script>
<style lang="less">
  .app-box {
    display: flex;
    .app-info {
      display: flex;
      margin-left: 15px;
      margin-right: 15px;
      flex-direction: column;
    }
  }
</style>
