<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <cashier-search @search="reload" />
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="cashierId"
        :columns="columns"
        :datasource="datasource"
        v-model:selection="selection"
        :scroll="{ x: 800 }"
      >
        <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"
              @click="removeBatch"
            >
              <template #icon>
                <delete-outlined />
              </template>
              <span>删除</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'outTradeNo'">
            <a-tooltip title="查看详情">
              <a href="#" @click="openInfo(record)">{{ record.outTradeNo }}</a>
            </a-tooltip>
          </template>
          <template v-if="column.key === 'status'">
            <a-tag v-if="record.status === '0'" color="green">正常</a-tag>
            <a-tag v-if="record.status === '1'" color="red">禁用</a-tag>
          </template>

          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="openInfo(record)">详情</a>
              <a-divider type="vertical" />
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm
                title="确定要删除此记录吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <cashier-edit v-model:visible="showEdit" :data="current" @done="reload" />
    <!-- 用户详情 -->
    <cashier-info v-model:visible="showInfo" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import {
    PlusOutlined,
    DeleteOutlined,
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import CashierSearch from './components/cashier-search.vue';
  import CashierEdit from './components/cashier-edit.vue';
  import CashierInfo from './components/cashier-info.vue';
  import {
    pageCashier,
    removeCashier,
    removeBatchCashier
  } from '@/api/apps/cashier';
  import type { Cashier, CashierParam } from '@/api/apps/cashier/model';
  import { toDateString } from 'ele-admin-pro';

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

  // 是否显示资产详情
  const showInfo = ref(false);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '商户名称',
      dataIndex: 'merchantName',
      key: 'status',
      sorter: true
    },
    {
      title: '设备ID',
      dataIndex: 'merchantCode',
      key: 'merchantCode',
      width: 280,
      ellipsis: true,
      sorter: true
    },
    {
      title: '支付方式',
      dataIndex: 'payType'
    },
    {
      title: '支付金额',
      dataIndex: 'totalAmount'
    },
    {
      title: '备注',
      dataIndex: 'orderRemark'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: true,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center',
      hideInSetting: true
    }
  ]);

  // 表格选中数据
  const selection = ref<Cashier[]>([]);

  // 当前编辑数据
  const current = ref<Cashier | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

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

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

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

  /* 打开用户详情弹窗 */
  const openInfo = (row?: Cashier) => {
    current.value = row ?? null;
    showInfo.value = true;
  };

  /* 打开外部链接 */
  const openUrl = (record) => {
    window.open(record.panel);
  };

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

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

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