<template>
  <div class="account-dashboard-wrap">
    <div class="filter-wrap bg-#fff rounded-8px border-1px border-#D7D7D9 border-solid pb-24px mb-16px">
      <a-tabs v-model:activeKey="accountType" @tab-click="handleTabClick">
        <a-tab-pane key="1" title="账户"></a-tab-pane>
        <a-tab-pane key="2" title="计划"></a-tab-pane>
      </a-tabs>

      <div class="container px-24px">
        <div class="filter-row flex mb-20px">
          <div class="filter-row-item flex items-center">
            <span class="label">{{ accountType == 1 ? '账号名称' : '计划名称' }}</span>
            <a-space size="medium" class="w-240px">
              <a-input v-model="query.names" placeholder="请搜索..." size="medium" allow-clear @change="handleSearch">
                <template #prefix>
                  <icon-search />
                </template>
              </a-input>
            </a-space>
          </div>
          <div class="filter-row-item flex items-center">
            <span class="label">平台</span>
            <a-select
              v-model="query.platform"
              class="w-150"
              size="medium"
              placeholder="全部"
              allow-clear
              @change="handleSearch"
            >
              <a-option v-for="(item, index) in PLATFORM_LIST" :key="index" :value="item.value" :label="item.label"
                >{{ item.label }}
              </a-option>
            </a-select>
          </div>

          <div class="filter-row-item flex items-center">
            <span class="label">运营人员</span>
            <a-space class="w-160px">
              <OperatorSelect v-model="query.operator_id" :options="operators"  />
            </a-space>
          </div>
        </div>
        <div class="filter-row flex">
          <div class="filter-row-item flex items-center">
            <span class="label">时间筛选</span>
            <a-space class="w-240px">
              <a-range-picker v-model="query.data_time" size="medium" allow-clear format="YYYY-MM-DD" class="w-100%" />
            </a-space>
          </div>
          <a-button class="w-84px search-btn mr-12px" size="medium" @click="handleSearch">
            <template #icon>
              <icon-search />
            </template>
            <template #default>搜索</template>
          </a-button>
          <a-button class="w-84px reset-btn" size="medium" @click="handleReset">
            <template #icon>
              <icon-refresh />
            </template>
            <template #default>重置</template>
          </a-button>
        </div>
      </div>
    </div>
    <div class="table-wrap rounded-8px py-5px flex-1 flex flex-col" v-if="onLoading == false">
      <a-row class="grid-demo" :gutter="24">
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.total_use_amount?.date"
            :seriesData="xhlEcharts?.total_use_amount?.series_data"
            :title="{ name: '消耗量', popover: '消耗量' }"
          ></EchartsItem>
        </a-col>
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.avg_conversion_cost?.date"
            :seriesData="xhlEcharts?.avg_conversion_cost?.series_data"
            :title="{ name: '展示量', popover: '展示量' }"
          ></EchartsItem>
        </a-col>
      </a-row>
      <a-row class="grid-demo" :gutter="24">
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.click_number?.date"
            :seriesData="xhlEcharts?.click_number?.series_data"
            :title="{ name: '点击量', popover: '点击量' }"
          ></EchartsItem>
        </a-col>
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.click_rate?.date"
            :seriesData="xhlEcharts?.click_rate?.series_data"
            :title="{ name: '点击率', popover: '点击率' }"
          ></EchartsItem>
        </a-col>
      </a-row>
      <a-row class="grid-demo" :gutter="24">
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.avg_click_cost?.date"
            :seriesData="xhlEcharts?.avg_click_cost?.series_data"
            :title="{ name: '平均点击成本', popover: '平均点击成本' }"
          ></EchartsItem>
        </a-col>
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.thousand_show_cost?.date"
            :seriesData="xhlEcharts?.thousand_show_cost?.series_data"
            :title="{ name: '千次展示成本', popover: '千次展示成本' }"
          ></EchartsItem>
        </a-col>
      </a-row>
      <a-row class="grid-demo" :gutter="24">
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.conversion_number?.date"
            :seriesData="xhlEcharts?.conversion_number?.series_data"
            :title="{ name: '转化数', popover: '转化数' }"
          ></EchartsItem>
        </a-col>
        <a-col :span="12">
          <EchartsItem
            :xAxisData="xhlEcharts?.conversion_rate?.date"
            :seriesData="xhlEcharts?.conversion_rate?.series_data"
            :title="{ name: '转化率', popover: '转化率' }"
          ></EchartsItem>
        </a-col>
      </a-row>
    </div>
  </div>
</template>
<script setup lang="ts">
import EchartsItem from './components/echarts-item/index';
import { PLATFORM_LIST } from '../common_constants';
import {
  getPlacementAccountsList,
  getPlacementAccountsTrend,
  getPlacementAccountProjectsTrend,
  fetchAccountOperators,
} from '@/api/all/propertyMarketing';
import OperatorSelect from '@/views/property-marketing/media-account/components/operator-select/index.vue';

const accountType = ref(1);

const onLoading = ref(true);

const getOperators = async () => {
  const { code, data } = await fetchAccountOperators();
  if (code === 200) {
    operators.value = data;
  }
};
const query = reactive({
  names: '',
  platform: '',
  operator_id: '',
  data_time: [],
});
const xhlEcharts = reactive({});
const getAccountsTrends = async () => {
  const { code, data } = await getPlacementAccountsTrend(query);
  if (code === 200) {
    Object.assign(xhlEcharts, data);
  }
  onLoading.value = false;
};

const getAccountProjectsTrend = async () => {
  const { code, data } = await getPlacementAccountProjectsTrend(query);
  if (code === 200) {
    Object.assign(xhlEcharts, data);
  }
  onLoading.value = false;
};

const handleSearch = async () => {
  if (accountType.value == 1) {
    getAccountsTrends();
  } else if (accountType.value == 2) {
    getAccountProjectsTrend();
  }
};
const handleReset = async () => {};

const operators = ref([]);
const accountList = ref([]);
const handleTabClick = (key) => {
  handleSearch();
};

// 获取账户名称
const getAccountList = async () => {
  const { code, data } = await getPlacementAccountsList(query);
  if (code === 200) {
    accountList.value = data;
  }
};

onMounted(() => {
  handleSearch();
  getAccountList();
  getOperators();
});
</script>

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