<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="rentId"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        :need-page="false"
        :expand-icon-column-index="1"
        :scroll="{ x: 1200 }"
        cache-key="towerRent"
      >
        <template #toolbar>
          <a-space>
            <a-button type="primary" class="ele-btn-icon" @click="openEdit()">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
            <!-- 搜索表单 -->
            <a-input-search
              allow-clear
              v-model:value="searchText"
              placeholder="请输入搜索关键词"
              @search="search"
              @pressEnter="search"
            />
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key == 'totalRent'">
            {{ record.month * record.num }}
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <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>
    <!-- 编辑弹窗 -->
    <RentEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading, toDateString } from 'ele-admin-pro/es';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type { TowerRent, TowerRentParam } from '@/api/tower/rent/model';
  import { listTowerRent, removeTowerRent } from '@/api/tower/rent';
  import RentEdit from '@/views/tower/rent/components/rent-edit.vue';

  // 表格实例
  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: 'companyName',
      key: 'companyName',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '月租金',
      dataIndex: 'month',
      key: 'month',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '月数',
      dataIndex: 'num',
      key: 'num',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '租金',
      dataIndex: 'totalRent',
      key: 'totalRent',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      width: 180,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center'
    }
  ]);

  // 当前编辑数据
  const current = ref<TowerRent | null>(null);
  const searchText = ref('');
  // 是否显示编辑弹窗
  const showEdit = ref(false);

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

  /* 刷新表格 */
  const reload = (where?: TowerRentParam) => {
    tableRef?.value?.reload({ where });
  };

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

  const search = (searchText) => {
    reload({ keywords: searchText });
  };

  /* 删除单个 */
  const remove = (row: TowerRent) => {
    const hide = messageLoading('请求中..', 0);
    removeTowerRent(row.rentId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

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

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