<template>
  <div class="container">
    <a-table
        :scroll="{ x: 2000, y: 550 }"
        :dataSource="dataSource"
        :columns="columns"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        bordered
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'projectImage'">

          <img
              :src="downLoadImage+record.projectImage"
              alt="项目图片"
              style="width: 50px; height: 50px"
          />
        </template>
        <template v-if="column.dataIndex === 'projectCodeImage'">

          <img
              :src="downLoadImage+record.projectCodeImage"
              alt="推广码"
              style="width: 50px; height: 50px"
          />
        </template>
        <template v-if="column.key === 'action'">
          <a-space :size="8">
            <a-tag
                color="orange"
                @click="settlementRecord(record)"
            >
              结算记录
            </a-tag>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import type { TableProps } from 'ant-design-vue';

import myAxios from "../../api/myAxios.ts";
import router from "../../router";
import {downLoadImage} from "../../api/ImageUrl.ts";

interface PromoRecord {
  id: number;
  salespersonName: string;
  salespersonPhone: string;
  promoCodeInfoKey: string;
  promoCodeLink: string;
  projectName: string;
  projectImage: string;
  projectId: number;
  userId: number;
  createTime: string;
}


const columns = ref([
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    width: 10,
    fixed: 'left',
    align: 'center',
    sorter: true,
    sortDirections: ['ascend', 'descend']
  },
  {
    title: '项目名称',
    dataIndex: 'projectName',
    key: 'projectName',
    width: 20,
    fixed: 'left',
    align: 'center',
    sorter: true,
    sortDirections: ['ascend', 'descend']
  },
  {
    title: '推广人员',
    dataIndex: 'salespersonName',
    key: 'salespersonName',
    width: 20,
    fixed: 'left',
    align: 'center',
    sorter: true,
    sortDirections: ['ascend', 'descend']
  },
  {
    title: '手机号',
    dataIndex: 'salespersonPhone',
    key: 'salespersonPhone',
    width: 20,
    align: 'center',
    sorter: true,
    sortDirections: ['ascend', 'descend']
  },
  {
    title: '推广信息key',
    dataIndex: 'promoCodeInfoKey',
    key: 'promoCodeInfoKey',
    width: 20,
    align: 'center'
  },
  {
    title: '推广链接',
    dataIndex: 'promoCodeLink',
    key: 'promoCodeLink',
    width: 60,
    align: 'center'
  },
  {
    title: '推广码',
    dataIndex: 'projectCodeImage',
    key: 'projectCodeImage',
    width: 20,
    align: 'center'
  },
  {
    title: '项目图片',
    dataIndex: 'projectImage',
    key: 'projectImage',
    width: 20,
    align: 'center'
  },
  {
    title: '项目ID',
    dataIndex: 'projectId',
    key: 'projectId',
    width: 20,
    align: 'center'
  },
  {
    title: '用户ID',
    dataIndex: 'userId',
    key: 'userId',
    width: 20,
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    width: 40,
    align: 'center',
    sorter: true,
    sortDirections: ['ascend', 'descend']
  },
  {
    title: '操作',
    key: 'action',
    fixed: 'right',
    width: 30,
    align: 'center'
  }
]);

const dataSource = ref<PromoRecord[]>([]);
const loading = ref(false);


const sortParams = reactive({
  sortField: 'id',
  sortOrder: 'ascend'
});

const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0,
  showSizeChanger: true,
  showTotal: (total: number) => `共 ${total} 条`,
});


const fetchData = async () => {
  const storedToken = localStorage.getItem('token');
  try {
    loading.value = true;
    const response:any = await myAxios.post('/promoCodeApply/page', {
      current: pagination.current,
      pageSize: pagination.pageSize,
      sortField: sortParams.sortField,
      sortOrder: sortParams.sortOrder
    }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': storedToken
      }
    });

    if (response.code === 1) {
      dataSource.value = response.data.records;
      pagination.total = response.data.total;
    }
  } catch (error) {
    console.error('请求失败:', error);
  } finally {
    loading.value = false;
  }
};


const handleTableChange: TableProps['onChange'] = (pag, _, sorter) => {
  // 处理分页参数
  if (pag) {
    pagination.current = pag.current!;
    pagination.pageSize = pag.pageSize!;
  }

  if (sorter) {

    const { field, order } = sorter as { field?: string; order?: string };

    if (field && order) {
      sortParams.sortField = field;
      sortParams.sortOrder = order;
    } else {

      sortParams.sortField = 'id';
      sortParams.sortOrder = 'ascend';
    }
  }

  fetchData();
};

onMounted(() => {
  fetchData();
});

const settlementRecord = (record: PromoRecord) => {
  router.push({
    path: '/moneyRecord',
    query: {
      id: String(record.id),
      projectId: String(record.projectId),
      userId: String(record.userId)
    }
  })
}
</script>

<style scoped>
.container {
  padding: 20px;
}
</style>