<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="houseId"
        :columns="columns"
        :datasource="datasource"
        v-model:selection="selection"
        :scroll="{ x: 1200 }"
        :customRow="customRow"
        cache-key="proHouseInfoTable"
      >
        <template #toolbar>
          <search
            :selection="selection"
            @search="reload"
            @add="openEdit"
            @remove="removeBatch"
          />
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'nickname'">
            <div class="user-box">
              <a-avatar
                :size="30"
                :src="`${record.avatar}`"
                style="margin-right: 4px"
              >
                <template #icon>
                  <UserOutlined />
                </template>
              </a-avatar>
              <div class="user-info">
                <span>{{ record.realName }}</span>
                <span class="ele-text-placeholder">{{ record.nickname }}</span>
              </div>
            </div>
          </template>
          <template v-else-if="column.key === 'houseLabel'">
            <template v-if="JSON.parse(record.houseLabel)">
              <a-tag v-for="item in JSON.parse(record.houseLabel)" :key="item">
                {{ item }}
              </a-tag>
            </template>
          </template>
          <template v-if="column.key === 'rent'">
            <span class="ele-text-danger">
              ￥{{ formatNumber(record.rent) }}
            </span>
          </template>
          <template v-if="column.key === 'monthlyRent'">
            <span class="ele-text-danger">
              ￥{{ formatNumber(record.monthlyRent) }}
            </span>
          </template>
          <template v-if="column.key === 'extent'">
            <span>
              {{ record.extent }}
            </span>
          </template>
          <template v-else-if="column.key === 'action'">
            <a-space>
              <!--              <a-divider type="vertical" />-->
              <!--              <a-button @click="openEdit(record)">修改</a-button>-->
              <!--              <a-divider type="vertical" />-->
              <!--              <a-button @click="resetPsw(record)">重置密码</a-button>-->
              <!--              <a-divider type="vertical" />-->
              <!--              <a-popconfirm-->
              <!--                placement="topRight"-->
              <!--                title="确定要删除此用户吗？"-->
              <!--                @confirm="remove(record)"-->
              <!--              >-->
              <!--                <a class="ele-text-danger">删除</a>-->
              <!--              </a-popconfirm>-->
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <InfoEdit
      v-model:visible="showEdit"
      :data="current"
      :organization-list="data"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue/es';
  import {
    ExclamationCircleOutlined,
    UserOutlined
  } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro/es';
  import { formatNumber, messageLoading, toDateString } from 'ele-admin-pro/es';
  import type {
    ColumnItem,
    DatasourceFunction
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import InfoEdit from './components/info-edit.vue';
  import { pageHouseInfo, removeBatchHouseInfo } from '@/api/house/info';
  import type { HouseInfo, HouseInfoParam } from '@/api/house/info/model';
  import { Organization } from '@/api/system/organization/model';
  import Search from './components/search.vue';

  // 树形数据
  const data = ref<Organization[]>([]);
  // 表格选中数据
  const selection = ref<HouseInfo[]>([]);
  // 当前编辑数据
  const current = ref<HouseInfo | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格实例
  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: 'houseId',
      width: 80,
      showSorterTooltip: false
    },
    {
      title: '经纪人',
      dataIndex: 'nickname',
      key: 'nickname',
      ellipsis: true,
      width: 160
    },
    {
      title: '标题',
      dataIndex: 'houseTitle',
      width: 180
    },
    {
      title: '城市',
      dataIndex: 'city'
    },
    {
      title: '户型',
      key: 'houseType',
      dataIndex: 'houseType'
    },
    {
      title: '租赁方式',
      dataIndex: 'leaseMethod'
    },
    {
      title: '租金(元/m²)',
      dataIndex: 'rent',
      key: 'rent',
      sorter: true
    },
    {
      title: '月租金',
      dataIndex: 'monthlyRent',
      key: 'monthlyRent',
      sorter: true
    },
    {
      title: '面积(m²)',
      dataIndex: 'extent',
      key: 'extent',
      sorter: true
    },
    {
      title: '楼层',
      dataIndex: 'floor'
    },
    {
      title: '朝向',
      dataIndex: 'toward'
    },
    {
      title: '房号',
      dataIndex: 'roomNumber'
    },
    {
      title: '入房密码',
      dataIndex: 'password'
    },
    // {
    //   title: '详细地址',
    //   dataIndex: 'address',
    //   width: 300
    // },
    // {
    //   title: '房屋标签',
    //   dataIndex: 'houseLabel',
    //   key: 'houseLabel',
    //   width: 300
    // },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 170,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    }
  ]);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    where.showProfile = true;
    where.roleId = filters.roles;
    return pageHouseInfo({ page, limit, ...where, ...orders });
  };
  /* 搜索 */
  const reload = (where?: HouseInfoParam) => {
    selection.value = [];
    tableRef?.value?.reload({ where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: HouseInfo) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的用户吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = messageLoading('请求中..', 0);
        removeBatchHouseInfo(selection.value.map((d) => d.houseId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

  /* 自定义行属性 */
  const customRow = (record: HouseInfo) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        openEdit(record);
      }
    };
  };
</script>

<script lang="ts">
  export default {
    name: 'HouseInfo'
  };
</script>

<style lang="less" scoped>
  .user-box {
    display: flex;
    align-items: center;
    .user-info {
      display: flex;
      flex-direction: column;
      align-items: start;
    }
  }
</style>
