<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="modelId"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        :need-page="false"
        :expand-icon-column-index="1"
        :scroll="{ x: 1200 }"
        cache-key="towerModel"
      >
        <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 === '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>
    <!-- 编辑弹窗 -->
    <ModelEdit 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 { TowerModel, TowerModelParam } from '@/api/tower/model/model';
  import { listTowerModel, removeTowerModel } from '@/api/tower/model';
  import ModelEdit from '@/views/tower/model/components/model-edit.vue';
  import { TowerEquipment } from "@/api/tower/equipment/model";

  // 表格实例
  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: 'name',
      key: 'name',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '设备型号',
      dataIndex: 'model',
      key: 'model',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '使用年限',
      dataIndex: 'yearLife',
      key: 'yearLife',
      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<TowerModel | null>(null);
  const searchText = ref('');
  // 是否显示编辑弹窗
  const showEdit = ref(false);

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

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

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

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

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

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

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