<!-- eslint-disable prettier/prettier -->
<script setup lang="ts">
import { shallowRef, ref, watch, reactive } from "vue";
import wanyuCompletionConfig from "./config/wanyuCompletion.config";
import createCompanyConfig from "./config/company.config";
import wanyuTypeConfig from "./config/wanyuType.config";
import wanyuConfig from "./config/wanyu.config";

import {
  addition,
  division,
  multiplication,
  unit2TenThousand
} from "/@/utils/calc";

const PageName = "performanceReport";

const companies = [
  { companyNo: "GS2302231125079621", companyName: "北京百辰荣达国际科贸有限公司" },
  { companyNo: "GS2302231323386950", companyName: "北京泓源广诚国际商贸有限公司" },
  { companyNo: "GS2304031312553746", companyName: "北京锦兴弘昌科技有限公司" },
  { companyNo: "GS2302231124114965", companyName: "北京普润心堂商贸有限公司" },
  { companyNo: "GS2203161855277894", companyName: "北京万宇恒通国际科贸有限公司" }
];

const daytime = shallowRef("");

const wanyuCompletionReportRef = ref<any>(null);
const wanyuTypeReportRef = ref<any>(null);
const wanyuReportRef = ref<any>(null);
const dashboardRef = ref<any[]>([]);

const transformTime = function () {
  let time = new Date();
  let y = time.getFullYear();
  let M = time.getMonth() + 1;
  let d = time.getDate();
  return y + "-" + (M < 10 ? "0" + M : M) + "-" + (d < 10 ? "0" + d : d);
};

const initalTotal = {
  total_tips: 0,
  sale_total: 0,
  completionRate: 0
};

const platformDimensionTotal = reactive({
  wanyu:{...initalTotal},
  wanyuCompletion:{...initalTotal}
})

const companyDimensionTotal = reactive(companies.reduce((prev,{companyNo}) => ({
  ...prev,
  [companyNo]:{...initalTotal}
}),{}))

const getPlatformReportResult = function (data: Record<string, any>[] = [], property:string) {
  platformDimensionTotal[property] = data.reduce((prev, current) => {
    const total_tips = addition(prev.total_tips, current.total_tips);
    const sale_total = addition(prev.sale_total, current.monthinfo.monthNetSales);
    const completionRate = multiplication(division(sale_total, total_tips),100);
    return { total_tips, sale_total, completionRate };
  }, initalTotal);
};

const getCompanyReportResult = function (data: Record<string, any>[] = [], companyNo:string) {
  companyDimensionTotal[companyNo] = data.reduce((prev, current) => {
    const total_tips = Number(current.total_tips)
    const sale_total = addition(prev.sale_total,current.monthinfo.msale_total)
    const completionRate = multiplication(division(sale_total,total_tips) || 0, 100)
    return { total_tips, sale_total, completionRate };
  }, initalTotal);
};


watch(
  () => daytime.value,
  daytime => {
    const list = [
      wanyuReportRef.value,
      wanyuCompletionReportRef.value,
      wanyuTypeReportRef.value,
      ...dashboardRef.value
    ]

    list.forEach(dashboard => {
      if (!dashboard) return;
      const contentRef = dashboard.getContent();
      if (!contentRef) return;
      contentRef.value.getPageData({ daytime });
    });
  }
);
daytime.value = transformTime();
</script>

<template>
  <PageAuth :pageName="PageName">
    <div class="bg-white">
      <div w-full flex justify-end p-2>
        <ElDatePicker
          v-model="daytime"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
          type="date"
          placeholder="选择日期"
          :disabledDate="time => time.getTime() > Date.now()"
        />
      </div>

      <div class="ml-[15px]">
        <h2 style="font-size: 20px; font-weight: 700; margin-bottom: 10px">
          整体搞了
          {{
            unit2TenThousand(platformDimensionTotal.wanyuCompletion.sale_total)
          }}
          元（万宇平台+业务公司供应商端）
        </h2>
      </div>
      <div class="mx-[12px] mb-[-15px]">
        <ElTable
          border
          size="small"
          :data="[platformDimensionTotal.wanyuCompletion]"
        >
          <ElTableColumn label="月指标">
            <template #="{ row }">{{
              unit2TenThousand(row.total_tips)
            }}</template>
          </ElTableColumn>
          <ElTableColumn label="完成额">
            <template #="{ row }">{{
              unit2TenThousand(row.sale_total)
            }}</template>
          </ElTableColumn>
          <ElTableColumn label="完成率">
            <template #="{ row }">{{
              Number(row.completionRate).toFixed(2) + "%"
            }}</template>
          </ElTableColumn>
        </ElTable>
      </div>

      <PageContainer
        ref="wanyuCompletionReportRef"
        :contentConfig="wanyuCompletionConfig"
        :events="{
          content: {
            handleResult: data =>
              getPlatformReportResult(data, 'wanyuCompletion')
          }
        }"
      />

      <h2
        style="
          font-size: 20px;
          font-weight: 700;
          margin-bottom: 10px;
          margin-left: 15px;
        "
      >
        平台维度
      </h2>

      <ElRow style="overflow: hidden">
        <ElCol :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
          <p class="ml-[15px]" style="line-height: 42px">
            北京万宇恒通国际科贸有限公司
          </p>
          <div class="mx-[12px] mb-[-15px]">
            <ElTable border size="small" :data="[platformDimensionTotal.wanyu]">
              <ElTableColumn label="月指标">
                <template #="{ row }">{{
                  unit2TenThousand(row.total_tips)
                }}</template>
              </ElTableColumn>
              <ElTableColumn label="完成额">
                <template #="{ row }">{{
                  unit2TenThousand(row.sale_total)
                }}</template>
              </ElTableColumn>
              <ElTableColumn label="完成率">
                <template #="{ row }">{{
                  Number(row.completionRate).toFixed(2) + "%"
                }}</template>
              </ElTableColumn>
            </ElTable>
          </div>
          <PageContainer
            ref="wanyuReportRef"
            :contentConfig="wanyuConfig"
            :events="{
              content: {
                handleResult: data => getPlatformReportResult(data, 'wanyu')
              }
            }"
          />
        </ElCol>

        <ElCol :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
          <PageContainer
            ref="wanyuTypeReportRef"
            :contentConfig="wanyuTypeConfig"
          />
        </ElCol>
      </ElRow>

      <h2
        style="
          font-size: 20px;
          font-weight: 700;
          margin-bottom: 10px;
          margin-left: 15px;
        "
      >
        公司维度
      </h2>
      <ElRow style="overflow: hidden">
        <ElCol
          :xs="24"
          :sm="24"
          :md="12"
          :lg="12"
          :xl="12"
          v-for="(company, index) in companies"
          :key="company.companyNo"
        >
          <div class="ml-15px" style="line-height: 32px">
            {{ index + 1 }}.{{ company.companyName || "--" }}
          </div>
          <div class="mx-[12px] mb-[-15px]">
            <ElTable
              border
              size="small"
              :data="[companyDimensionTotal[company.companyNo]]"
            >
              <ElTableColumn label="月指标">
                <template #="{ row }">{{
                  unit2TenThousand(row.total_tips)
                }}</template>
              </ElTableColumn>
              <ElTableColumn label="完成额">
                <template #="{ row }">{{
                  unit2TenThousand(row.sale_total)
                }}</template>
              </ElTableColumn>
              <ElTableColumn label="完成率">
                <template #="{ row }">{{
                  Number(row.completionRate).toFixed(2) + "%"
                }}</template>
              </ElTableColumn>
            </ElTable>
          </div>

          <PageContainer
            ref="dashboardRef"
            :contentConfig="createCompanyConfig(company.companyNo)"
            :events="{
              content: {
                handleResult: data =>
                  getCompanyReportResult(data, company.companyNo)
              }
            }"
          />
        </ElCol>
      </ElRow>
    </div>
  </PageAuth>
</template>
