<!--
 * @Author: RenXiaoDong
 * @Date: 2025-06-28 14:13:42
-->
<template>
  <div class="account-data-wrap">
    <div class="filter-wrap bg-#fff rounded-8px border-1px border-#D7D7D9 border-solid pb-24px mb-16px">
      <a-tabs v-model="activeTab" @tab-click="handleTabClick">
        <a-tab-pane key="1" title="账户"></a-tab-pane>
        <a-tab-pane key="2" title="计划"></a-tab-pane>
        <template v-if="!isAccountTab" #extra>
          <a-button class="w-112px mr-12px search-btn flex items-center" size="medium" @click="handleOpenGroupModal">
            <template #icon>
              <img :src="icon2" width="16" height="16" />
            </template>
            <template #default>分组管理</template>
          </a-button>
        </template>
      </a-tabs>
      <FilterBlock ref="filterBlockRef" v-model:query="query" @onSearch="getData" @onReset="handleReset" />
    </div>
    <div
      class="table-wrap bg-#fff rounded-8px border-1px border-#D7D7D9 border-solid px-24px py-24px flex-1 flex flex-col"
    >
      <BoardTable
        ref="accountTableRef"
        :dataSource="dataSource"
        @export="handleExport"
        @sorterChange="handleSorterChange"
        @selectionChange="handleSelectionChange"
      />
      <div v-if="pageInfo.total > 0" class="pagination-box">
        <a-pagination
          :total="pageInfo.total"
          size="mini"
          show-total
          show-jumper
          show-page-size
          :current="pageInfo.page"
          :page-size="pageInfo.pageSize"
          @change="onPageChange"
          @page-size-change="onPageSizeChange"
        />
      </div>
    </div>

    <GroupManageModal ref="groupManageModalRef" @update="filterBlockRef?.getGroups" />
  </div>
</template>

<script setup>
import FilterBlock from './components/filter-block';
import BoardTable from './components/board-table';
import GroupManageModal from './components/group-manage-modal';

import {
  getPlacementAccountData,
  postPlacementAccountDataExport,
  getPlacementAccountDataList,
  postPlacementAccountDataListExport,
} from '@/api/all/propertyMarketing';

import { INITIAL_QUERY } from './constants';

import icon2 from '@/assets/img/media-account/icon-group.png';

const selectedRowKeys = ref([]);
const activeTab = ref('1');
const accountTableRef = ref(null);
const groupManageModalRef = ref(null);
const filterBlockRef = ref(null);
const query = ref({});
const dataSource = ref([]);
const pageInfo = ref({
  page: 1,
  pageSize: 20,
  total: 100,
});

const isAccountTab = computed(() => activeTab.value === '1');

const getData = async () => {
  const _fn = isAccountTab.value ? getPlacementAccountData : getPlacementAccountDataList;
  const { code, data } = await _fn(query.value);
  if (code === 200) {
    dataSource.value = data?.data ?? [];
    pageInfo.value.total = data.total;
  }
};

const onPageChange = (current) => {
  pageInfo.value.page = current;
  getData();
};

const onPageSizeChange = (pageSize) => {
  pageInfo.value.pageSize = pageSize;
  reload();
};

const reload = () => {
  pageInfo.value.page = 1;
  getData();
};

const handleReset = () => {
  pageInfo.value.page = 1;
  pageInfo.value.pageSize = 20;
  pageInfo.value.total = 0;
  selectedRowKeys.value = [];
  accountTableRef.value?.resetTable();
  query.value = cloneDeep(INITIAL_QUERY);
  reload();
};

const handleSorterChange = (column, order) => {
  query.value.column = column;
  query.value.order = order;
  reload();
};
const handleSelectionChange = (selectedRows) => {
  selectedRowKeys.value = selectedRows;
};

const handleTabClick = (key) => {
  activeTab.value = key;
  handleReset();
};

const handleExport = () => {
  const _fn = isAccountTab.value ? postPlacementAccountDataExport : postPlacementAccountDataListExport;
  _fn({
    ...query.value,
  }).then((res) => {
    const { code, data } = res;
    if (code === 200) {
      window.open(data.download_url, '_blank');
    }
  });
};

const handleOpenGroupModal = () => {
  groupManageModalRef.value?.open();
};

onMounted(() => {
  getData();
});
</script>

<style scoped lang="scss">
@import './style.scss';
</style>
