<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="never">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane v-for="item in tabs" :label="item.label" :name="item.value">
            </el-tab-pane>
          </el-tabs>
          <el-table v-loading="loading" :data="rows">
            <el-table-column
              v-for="{ label, prop, align, minWidth } in headers"
              :key="prop"
              :align="align || 'center'"
              :label="label"
              :min-width="minWidth || '100px'"
              prop="expertName"
            >
              <template #default="{ row }">
                <div v-if="prop == 'trend'" class="trend">
                  <el-icon :color="trendIcon(row.tendency)?.color" :size="20">
                    <template v-if="row.tendency > 0">
                      <Top />
                    </template>
                    <template v-else-if="row.tendency < 0">
                      <Bottom />
                    </template>
                    <template v-else>
                      <Right />
                    </template>
                  </el-icon>
                  <div class="trend-value">{{ Math.abs(row.tendency) }}</div>
                  （与上个月比较）
                </div>
                <span v-else>{{ row[prop] === 0 ? '0' : row[prop] || '-' }}</span>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="page.total > 0" v-model:limit="page.pageSize" v-model:page="page.pageNum"
                      :total="page.total" @pagination="getList" />
          <div class="flex justify-end mt-[10px]">
            <el-button type="primary" @click="goBack">返回</el-button>
          </div>
        </el-card>
      </div>
    </transition>
  </div>
</template>
<script lang="ts" setup>

import { getAnalysisList, getReportList, getSummaryList } from '@/api/expertMoneyConfig/report';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const showSearch = ref(true);
const route = useRoute();
const id = computed(() => route.query.id);
const activeName = ref('report');
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const activeFn = () => {
  let json = {
    report: getReportList,
    analysis: getAnalysisList,
    summary: getSummaryList
  };
  return new Promise(async (resolve, reject) => {
    const params = {
      pageNum: page.value.pageNum,
      pageSize: page.value.pageSize,
      id: id.value
    }
    let res = await json[activeName.value](params);
    resolve(res);
  });
};

const tabs = [
  {
    label: '报表',
    value: 'report'
  },
  {
    label: '分析',
    value: 'analysis'
  },
  {
    label: '汇总',
    value: 'summary'
  }
];
const handleClick = async () => {
  await proxy?.$nextTick();
  page.value.pageNum = 1;
  page.value.pageSize = 10;
  await getList();
};
const trendIcon = (val) => {
  let type = val > 0 ? 'up' : val < 0 ? 'down' : '-'
  const color = {
    up: '#67C23A',
    down:'#F56C6C',
    '-':'gray'
  }
  return {
    color:color[type],
    type
  }
};
const headers = computed(() => {
  let json = {
    report: [
      { label: '专家姓名', prop: 'expertName' },
      { label: '专家类别', prop: 'expertType' },
      { label: '总费用（元）', prop: 'sumFee' },
      { label: '已支付（元）', prop: 'payFee' },
      { label: '待支付（元）', prop: 'noPayFee' },
      { label: '结算周期', prop: 'moneyCycle' }
    ],
    analysis: [
      { label: '专家类别', prop: 'expertType' },
      { label: '总费用（元）', prop: 'sumFee' },
      { label: '占比（%）', prop: 'proportion' },
      { label: '变化趋势', prop: 'trend' }
    ],
    summary: [
      { label: '总费用（元）', prop: 'sumFee' },
      { label: '总已支付（元）', prop: 'payMoney' },
      { label: '总待支付（元）', prop: 'noPayMoney' },
      { label: '总专家人数', prop: 'expertCount' },
      { label: '结算周期', prop: 'moneyCycle' }
    ]
  };
  return json[activeName.value];
});
const loading = ref(false);
const rows = ref([]);
const getList = async () => {
  loading.value = true;
  const res = await activeFn();
  rows.value = res.rows;
  page.value.total = res.total;
  loading.value = false;
};
const goBack = () => {
  proxy?.$tab.closePage(proxy?.$route).then(() => {
    proxy?.$router.back();
  });
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.trend {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
}
:deep(.pagination-container){
  height: 52px;
}
</style>
