<template>
  <div class="p-2 h-full flex flex-col">
    <pageHeader></pageHeader>
    <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="hover">
          <el-form ref="queryFormRef" :inline="true" :model="queryParams" @submit.prevent="handleQuery">
            <div class="flex justify-between">
              <div>
                <el-form-item label="小区" prop="villageId">
                  <el-select v-model="queryParams.villageId" placeholder="请选择">
                    <el-option v-for="item in villageList" :key="item.villageId" :label="item.villageName" :value="item.villageId" />
                  </el-select>
                </el-form-item>
                <el-form-item label="收费类型" prop="chargeTypeId">
                  <el-select v-model="queryParams.chargeTypeId" placeholder="请选择">
                    <el-option v-for="item in chargeTypeList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
                <el-form-item label="查询日期">
                  <el-date-picker
                    v-model="queryTime"
                    end-placeholder="结束日期"
                    format="YYYY-MM"
                    placeholder="请选择日期"
                    range-separator="至"
                    start-placeholder="开始日期"
                    type="monthrange"
                    value-format="YYYY-MM"
                    @update:model-value="handleQueryTime"
                  />
                </el-form-item>
              </div>
              <div>
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
              </div>
            </div>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="5">
            <span>应收欠费明细表</span>
          </el-col>
          <el-col :span="15"></el-col>
          <el-col :span="3"> </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="chargeTrendList" border>
        <el-table-column v-for="(item, index) in rows" :key="index" :label="item" align="center">
          <template #default="scope">
            <span>{{ scope.row[item] }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="echartsBox" style="flex: 1">
        <Echarts :options="chartConfig" />
      </div>
      <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>
账龄
<script lang="ts" name="Arrears" setup>
import pagination from '@/components/Pagination/index.vue';
import pageHeader from '@/components/Pageheader/index.vue';
import { getCommunitylistAPI } from '@/api/system/community';
import { getChargeTend } from '@/api/system/report/chargeTrend';
import Echarts from '@/components/Echarts/index.vue';
import type { EChartsOption } from 'echarts';
import { listChargeType } from '@/api/system/ChargeType';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const queryTime = ref([]);
function handleQueryTime(val: string[]) {
  queryParams.value.startTime = val[0];
  queryParams.value.endTime = val[1];
}
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    villageId: '-1',
    startTime: '',
    endTime: '',
    chargeTypeId: '-1'
  }
});

const { queryParams } = toRefs(data);

// 查询小区
const villageList = ref([]);
function getAllVillage() {
  getCommunitylistAPI().then((res) => {
    villageList.value = [
      {
        villageId: '-1',
        villageName: '全部区域'
      },
      ...res.rows
    ];
  });
}

const chargeTrendList = ref([]);
const rows = ref([]);

const chargeTypeList = ref([]);
function getChartTypeList() {
  listChargeType().then((res) => {
    chargeTypeList.value = [
      {
        id: '-1',
        name: '全部收费类型'
      },
      ...res.rows
    ];
  });
}

/** 查询电抄记录列表 */
const getList = async () => {
  loading.value = true;
  const search = { ...queryParams.value };
  // 移除空字符串字段
  Object.keys(search).forEach((key) => {
    if (search[key] === '-1') {
      search[key] = undefined;
    }
  });
  rows.value = [];
  getChargeTend(search)
    .then((res) => {
      const obj = {};
      res.data.forEach((item) => {
        const label = item.month.split('-')[1];
        const key = `${label}月`;
        rows.value.push(key);
        obj[key] = item.amount;
      });
      chargeTrendList.value = [obj];
      total.value = res.total;
      handleChartsData();
    })
    .finally(() => {
      loading.value = false;
    });
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    villageId: '-1',
    startTime: '',
    endTime: '',
    chargeTypeId: '-1'
  };

  queryTime.value = [];
  handleQuery();
};

const chartConfig = shallowRef<EChartsOption>({
  grid: {
    left: '1%',
    right: '1%',
    top: '10%',
    bottom: '1%',
    containLabel: true
  },
  xAxis: {
    type: 'category', // 建议明确指定类型
    data: []
  },
  yAxis: {
    type: 'value'
  },
  tooltip: {
    trigger: 'axis'
  },
  series: [
    {
      data: [],
      type: 'bar',
      barWidth: '10%',
      itemStyle: {
        color: '#1a75ff'
      },
      label: {
        show: true,
        color: '#3f8afc',
        position: 'top'
      }
    }
  ]
});

function handleChartsData() {
  const data = Object.entries(chargeTrendList.value[0]);
  const x = data.map((item) => item[0]);
  const y = data.map((item) => item[1]);
  // 如果使用的是 ref，直接修改属性即可触发响应式
  chartConfig.value = {
    grid: chartConfig.value.grid,
    yAxis: chartConfig.value.yAxis,
    tooltip: chartConfig.value.tooltip,
    xAxis: {
      type: 'category',
      data: x
    },
    series: [
      {
        ...chartConfig.value.series?.[0],
        type: 'bar',
        data: y
      }
    ]
  } as EChartsOption;
}

onMounted(() => {
  getList();
  getAllVillage();
  getChartTypeList();
});
</script>

<style lang="scss" scoped>
.echartsBox {
  flex: 1;
  margin-top: 30px;
  height: 500px;
}
</style>
