<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="profitId"
          :columns="columns"
          :datasource="datasource"
          :parse-data="otherData"
          v-model:selection="selection"
          tool-class="ele-toolbar-form"
          :scroll="{ x: 1200 }"
          class="sys-org-table"
          :striped="true"
          @done="onTableDone"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @advanced="openAdvanced"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'profitId'">
              {{ record.profitId}}
            </template>
            <template v-if="column.key === 'orderUserName'">
              {{ record.orderUserName}}
            </template>
            <template v-if="column.key === 'merchantName'">
              <div style="display: flex; flex-direction: column">
                <span>{{ record.merchantName }}</span>
                <span class="ele-text-placeholder">
                  {{ record.merchantCode }}
                </span>
              </div>
            </template>
            <template v-if="column.key === 'sceneDis'">
              {{ record.sceneDis }}
            </template>
            <template v-if="column.key === 'orderNo'">
              {{ record.orderNo }}
            </template>
            <template v-if="column.key === 'money'">
              <p>
                <span class="ele-text-warning price-edit">
                  ￥{{ record.money }}
                </span></p>
            </template>
            <template v-if="column.key === 'comments'">
              <span :style="{'white-space':'pre-line'}">
                {{ record.comments }}
              </span>
            </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 === 'createTime'">
              {{ record.createTime }}
            </template>
            <!-- <template v-if="column.key === 'updateTime'">
              {{ record.updateTime }}
            </template> -->
            <template v-if="column.key === 'action'">
              <!-- <a-space>
                <a-button @click="openInfo(record)">详情</a-button>
              </a-space> -->
            </template>
          </template>
          <template #summary>
                <a-table-summary fixed>
                  <a-table-summary-row>
                    <a-table-summary-cell :index="0"></a-table-summary-cell>
                    <a-table-summary-cell :index="1">合计</a-table-summary-cell>
                    <a-table-summary-cell :col-span="4"></a-table-summary-cell>
                    <a-table-summary-cell :index="5">￥{{amountSummary}}</a-table-summary-cell>
                  </a-table-summary-row>
                </a-table-summary>
              </template>

        </ele-pro-table>
      </a-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem,
    EleProTableDone
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import Search from './search.vue';
  import type { Profit, ProfitParam } from '@/api/profit/model';
  import { profitPage } from '@/api/profit';

  // 当前用户信息
  // const userStore = useUserStore();
  // const loginUser = computed(() => userStore.info ?? {});
  // const orderType = localStorage.getItem('orderType');
  defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

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

  /* 获取字典数据 */
  // const orderSource = getDictionaryOptions('equipmentCategory');

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 20,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      hideInTable: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    /* {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center',
      fixed: 'left',
      hideInSetting: true
    }, */
    {
      title: '编号',
      dataIndex: 'profitId',
      key: 'profitId',
      sorter: true
    },
    {
      title: '下单用户',
      dataIndex: 'orderUserName',
      key: 'orderUserName'
    },
    {
      title: '用户实名',
      dataIndex: 'orderRealName',
      key: 'orderRealName',
    },
    {
      title: '门店',
      dataIndex: 'merchantName',
      key: 'merchantName'
    },
    {
      title: '收益类别',
      dataIndex: 'sceneDis',
      key: 'sceneDis'
    },
    {
      title: '关联订单号',
      dataIndex: 'relationOrderNo',
      key: 'relationOrderNo'
    },
    {
      title: '金额',
      key: 'money',
      dataIndex: 'money',
      ellipsis: true
    },
    {
      title: '备注',
      dataIndex: 'comments',
      key: 'comments'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: true/* ,
      customRender: ({ text }) => toDateString(text) */
    }
  ]);

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

  // 是否显示高级搜索
  const showAdvancedSearch = ref(false);

  // 合计
  const amountSummary = ref(0);

  // 案卷数据
  // const data = ref<Profit[]>([]);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    // 搜索条件
    if (filters.scene) {
      where.scene = filters.scene;
    }
    where.tenantId = localStorage.getItem('tenantId');
    return profitPage({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /* 表格数据加载完成事件 */
  const onTableDone: EleProTableDone<Profit> = (res) => {
    console.log(res);
    // data.value = res.data;
    /* amountSummary.value = res.data
      .map((item) => Number(item.amount))
      .reduce((prev, curr) => {
        const value = Number(curr);
        if (!isNaN(value)) {
          return prev + curr;
        } else {
          return prev;
        }
      }, 0); */
      amountSummary.value = res.response.otherData.total;
  };

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

  /* 打开高级搜索 */
  const openAdvanced = () => {
    showAdvancedSearch.value = !showAdvancedSearch.value;
  };

</script>

<script lang="ts">
  export default {
    name: 'ProfitIndex'

  };
</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;
  }
</style>
