<template>
  <div class="p-2 auto-overflow-y">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px" class="-mb-15px">
          <el-form-item label="开始时间" style="width: 225px" prop="beginDate">
            <el-date-picker
                v-model="queryParams.beginDate"
                value-format="YYYY"
                type="year"
            />
          </el-form-item>
          <el-form-item label="结束时间" style="width: 225px" prop="endDate">
            <el-date-picker
                v-model="queryParams.endDate"
                value-format="YYYY"
                type="year"
            />
          </el-form-item>

          <el-form-item label="结算账户" prop="accountIds">
            <el-select v-model="queryParams.accountIds" placeholder="请选择结算账户" clearable>
              <el-option v-for="act in ptAccountList" :key="act.accountId" :label="act.accountName" :value="act.accountId" />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="groupByTerm">
            <el-checkbox v-model="queryParams.groupByTerm" label="按消费设备统计" size="large" />
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['consumption:xfConsumedetail:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="monthAnalyzeList" show-summary max-height="calc(100vh - 27rem)">
        <el-table-column label="时间" align="center" prop="consumeDate" />
        <el-table-column label="结算账户" align="center" prop="accountName" />
        <el-table-column label="消费设备" align="center" prop="termName" />
        <el-table-column label="早餐金额" align="center" prop="zaoCanJinE" />
        <el-table-column label="早餐次数" align="center" prop="zaoCanCiShu" />
        <el-table-column label="午餐金额" align="center" prop="wuCanJinE" />
        <el-table-column label="午餐次数" align="center" prop="wuCanCiShu" />
        <el-table-column label="晚餐金额" align="center" prop="wanCanJinE" />
        <el-table-column label="晚餐次数" align="center" prop="wanCanCiShu" />
        <el-table-column label="宵夜金额" align="center" prop="xiaoYeJinE" />
        <el-table-column label="宵夜次数" align="center" prop="xiaoYeCiShu" />
        <el-table-column label="错扣补款金额" align="center"  prop="tuiKuanJinE" />
        <el-table-column label="错扣补款次数" align="center" prop="tuiKuanCiShu" />
        <el-table-column label="小计金额" align="center" prop="xiaoJiJinE" />
        <el-table-column label="小计次数" align="center" prop="xiaoJiCiShu" />
      </el-table>
      <!-- 翻页组件 -->
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改消费明细对话框 -->
    <detail-form ref="detailFormRef" apped-to-body @success="getList" />
  </div>
</template>

<script setup name="yearAnalyze" lang="ts">
/** 模块导入 */
import { PtAccountVO } from '@/api/basics/merchant/ptAccount/types';
import { consumeAnalyze, defaultDateByYear } from '@/api/consumption/report/xfConsumedetail';
import { monthAnalyzeVo } from '@/api/consumption/report/xfConsumedetail/types';
import { listPtAccount } from '@/api/basics/merchant/ptAccount';

defineOptions({ name: 'XfConsumedetail' });

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
/** 当前页面引用的数据字典 */
const ptAccountList = ref<PtAccountVO[]>([]);
const getPtAccount = async () =>{
  const res = await listPtAccount();
  ptAccountList.value = res.rows;
}
// 是否加载遮罩层
const loading = ref(true);
// 是否显示查询项
const showSearch = ref(true);
const monthAnalyzeList = ref<monthAnalyzeVo[]>([]);

// 总记录数
const total = ref(0);

// 查询窗体Ref
const queryFormRef = ref<ElFormInstance>();

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 20,
  beginDate: defaultDateByYear(),
  endDate: defaultDateByYear(),
  accountIds: '',
  groupByTerm: false,
  dateFormat: 'yyyy',
  params: {}
});

/** 查询消费分析列表 */
const getList = async () => {
  loading.value = true;
  const res = await consumeAnalyze(queryParams);
  monthAnalyzeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};


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

/** 重置按钮操作 */
const resetQuery = () => {

  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'backstage/consumption/xfConsumeDetail/exportConsumeAnalyze',
    {
      ...queryParams
    },
    `年营业报表_${new Date().getTime()}.xlsx`);
};

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