<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="placeId"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        v-model:selection="selection"
        :need-page="false"
        :expand-icon-column-index="1"
        :scroll="{ x: 1200 }"
        cache-key="paramPlace"
      >
        <template #toolbar>
          <a-space>
            <a-button type="primary" class="ele-btn-icon" @click="openEdit()">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
            <a-button
              danger
              type="primary"
              class="ele-btn-icon"
              :disabled="selection.length === 0"
              @click="removeBatch"
            >
              <template #icon>
                <DeleteOutlined />
              </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 === 'isShow'">
            <a-switch
              :checked="record.isShow === 1"
              checked-children="是"
              un-checked-children="否"
            />
          </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>
    <!-- 编辑弹窗 -->
    <PlaceEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import {
    ExclamationCircleOutlined,
    PlusOutlined
  } from '@ant-design/icons-vue';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading } from 'ele-admin-pro/es';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type {
    TowerPlace,
    TowerPlaceParam
  } from '@/api/tower/param/place/model';
  import {
    listTowerPlace,
    removeBatchTowerPlace,
    removeTowerPlace
  } from '@/api/tower/param/place';
  import PlaceEdit from './components/place-edit.vue';
  import { Modal } from 'ant-design-vue';
  import { removeBatchProject } from '@/api/tower/project';

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

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      title: '序号',
      width: 60,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '部位编码',
      dataIndex: 'placeCode',
      key: 'placeCode',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '设备类型',
      dataIndex: 'equipmentType',
      key: 'equipmentType',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '部位名称',
      dataIndex: 'name',
      key: 'name',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '检查/保养标准',
      dataIndex: 'standard',
      key: 'standard',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '部位类型',
      dataIndex: 'type',
      key: 'type',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '是否默认显示',
      dataIndex: 'isShow',
      key: 'isShow'
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center'
    }
  ]);

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

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

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

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

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

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

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

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

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