<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="id"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          :scroll="{ x: 1200 }"
          class="sys-org-table"
          :striped="true"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @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-if="column.key === 'money'">
              <span class="ele-text-warning">￥{{ record.money }}</span>
            </template>
            <template v-if="column.key === 'payType'">
              <a-tag v-if="record.payType === 10">微信</a-tag>
              <a-tag v-if="record.payType === 20">支付宝</a-tag>
              <a-tag v-if="record.payType === 30">银行卡</a-tag>
            </template>
            <template v-if="column.key === 'account'">
              <div v-if="record.payType === 20">{{ record.alipayName }}</div>
              <div v-if="record.payType === 20">{{ record.alipayAccount }}</div>
              <div v-if="record.payType === 30">{{ record.bankName }}</div>
              <div v-if="record.payType === 30">{{ record.bankAccount }}</div>
              <div v-if="record.payType === 30">{{ record.bankCard }}</div>
            </template>
            <template v-if="column.key === 'applyStatus'">
              <a-tag v-if="record.applyStatus === 10" color="warning"
                >待审核</a-tag
              >
              <a-tag v-if="record.applyStatus === 20" color="blue"
                >审核通过</a-tag
              >
              <a-tag v-if="record.applyStatus === 30" color="red">已驳回</a-tag>
              <a-tag v-if="record.applyStatus === 40" color="green"
                >已打款</a-tag
              >
            </template>
            <template v-if="column.key === 'createTime'">
              {{ record.createTime }}
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button @click="openEdit(record)">详情</a-button>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <WithdrawEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { 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 { toDateString } from 'ele-admin-pro';
  import Search from './components/search.vue';
  import WithdrawEdit from './components/withdraw-edit.vue';
  import {
    pageMerchantWithdraw,
    removeBatchMerchantWithdraw
  } from '@/api/love/withdraw';
  import type {
    MerchantWithdraw,
    MerchantWithdrawParam
  } from '@/api/love/withdraw/model';
  import { UserPlanEquity } from '@/api/love/plan-equity/model';

  defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

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

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '姓名',
      dataIndex: 'nickname',
      key: 'nickname'
    },
    {
      title: '提现金额',
      dataIndex: 'money',
      key: 'money'
    },
    {
      title: '打款方式',
      dataIndex: 'payType',
      key: 'payType'
    },
    {
      title: '账号信息',
      dataIndex: 'account',
      key: 'account'
    },
    {
      title: '状态',
      dataIndex: 'applyStatus',
      key: 'applyStatus'
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      key: 'createTime',
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center',
      hideInSetting: true
    }
  ]);

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

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    // 搜索条件
    if (filters.payStatus) {
      where.payStatus = filters.payStatus;
    }
    if (filters.deliveryStatus) {
      where.deliveryStatus = filters.deliveryStatus;
    }
    where.payStatus = 20;
    where.hasNum = false;
    where.orderStatus = 10;
    where.tenantId = localStorage.getItem('tenantId');
    return pageMerchantWithdraw({
      ...where,
      ...orders,
      page,
      limit
    });
  };

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

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

  /* 批量删除 */
  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);
        removeBatchMerchantWithdraw(selection.value.map((d) => d.id))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

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

  reload();
</script>

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

<style lang="less" scoped>
  p {
    line-height: 0.8;
  }
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
  .price-edit {
    padding-right: 5px;
  }
  .comments {
    max-width: 200px;
  }
  .user-box {
    display: flex;
    align-items: center;
    .user-info {
      display: flex;
      flex-direction: column;
      align-items: start;
    }
  }
</style>
