<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="学/工号" prop="userNumb">
            <el-input v-model="queryParams.userNumb" placeholder="请输入学/工号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="用户姓名" prop="realName">
            <el-input v-model="queryParams.realName" placeholder="请输入用户姓名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="消费日期" style="width: 308px">
            <el-date-picker
                v-model="dateRangeConsumeDate"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item label="餐类" prop="mealType">
            <el-select v-model="queryParams.mealType" placeholder="请选择餐类" clearable>
              <el-option v-for="dict in meal_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </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>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns"  @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="xfConsumedetailList" max-height="calc(100vh - 27rem)" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column v-if="false" label="消费记录Id,主键" align="center" prop="consumeId" />
        <!-- <el-table-column v-if="columns[1].visible" label="人员Id" align="center" prop="userId" /> -->
        <el-table-column v-if="columns[2].visible" label="学/工号" align="center" prop="userNumb" />
        <el-table-column v-if="columns[3].visible" label="用户姓名" align="center" prop="realName" />
        <el-table-column v-if="columns[4].visible" label="部门名称" align="center" prop="deptName" />
        <el-table-column v-if="columns[5].visible" label="消费日期" align="center" prop="consumeDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.consumeDate, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[16].visible" label="餐类" align="center" prop="mealType">
          <template #default="scope">
            <dict-tag :options="meal_type" :value="scope.row.mealType"/>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[15].visible" label="钱包" align="center" prop="bagType">
          <template #default="scope">
            <dict-tag :options="CARDBAGTYPE" :value="scope.row.bagType"/>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[6].visible" label="消费金额" align="center" prop="consumeMoney" />
        <el-table-column v-if="columns[9].visible" label="余额" align="center" prop="cardValue" />
        <!-- <el-table-column v-if="columns[7].visible" label="卡流水号" align="center" prop="cardNo" /> -->
        <el-table-column v-if="columns[8].visible" label="物理卡号" align="center" prop="factoryId" />
        <!-- <el-table-column v-if="columns[10].visible" label="消费金额" align="center" prop="consumeBalance" /> -->
        <!-- <el-table-column v-if="columns[11].visible" label="设备机号" align="center" prop="termNo" /> -->
        <el-table-column v-if="columns[14].visible" label="房间名称" align="center" prop="roomName" />
        <el-table-column v-if="columns[12].visible" label="设备名称" align="center" prop="termName" />
        <!-- <el-table-column v-if="columns[13].visible" label="状态标识" align="center" prop="statusFlag" /> -->
        <!-- <el-table-column v-if="columns[15].visible" label="钱包类型" align="center" prop="bagType" /> -->
        
      </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="XfConsumedetail" lang="ts">
/** 模块导入 */
import { listXfConsumedetail, delXfConsumedetail } from '@/api/consumption/report/xfConsumedetail';
import { XfConsumedetailVO } from '@/api/consumption/report/xfConsumedetail/types';

defineOptions({ name: 'XfConsumedetail' });

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
/** 当前页面引用的数据字典 */
const { meal_type, CARDBAGTYPE } = toRefs<any>(proxy?.useDict('meal_type', 'CARDBAGTYPE'));
// 是否加载遮罩层
const loading = ref(true);
// 是否显示查询项
const showSearch = ref(true);
const xfConsumedetailList = ref<XfConsumedetailVO[]>([]);
// 列表选中的记录Id
const ids = ref<Array<number | string>>([]);
// 单选
const single = ref(true);
// 多选
const multiple = ref(true);
// 总记录数
const total = ref(0);

// 日期范围
const dateRangeConsumeDate = ref<[DateModelType, DateModelType]>(['', '']);
// 查询窗体Ref
const queryFormRef = ref<ElFormInstance>();

const columns = ref<FieldOption[]>([
      { key: 0, label: `消费记录Id,主键`, visible: true, children: [] },
      { key: 1, label: `人员Id`, visible: true, children: [] },
      { key: 2, label: `学/工号`, visible: true, children: [] },
      { key: 3, label: `用户姓名`, visible: true, children: [] },
      { key: 4, label: `部门名称`, visible: true, children: [] },
      { key: 5, label: `消费日期`, visible: true, children: [] },
      { key: 6, label: `消费金额`, visible: true, children: [] },
      { key: 7, label: `卡流水号`, visible: true, children: [] },
      { key: 8, label: `物理卡号`, visible: true, children: [] },
      { key: 9, label: `余额`, visible: true, children: [] },
      { key: 10, label: `消费金额`, visible: true, children: [] },
      { key: 11, label: `设备机号`, visible: true, children: [] },
      { key: 12, label: `设备名称`, visible: true, children: [] },
      { key: 13, label: `状态标识`, visible: true, children: [] },
      { key: 14, label: `房间名称`, visible: true, children: [] },
      { key: 15, label: `钱包类型`, visible: true, children: [] },
      { key: 16, label: `餐类Id`, visible: true, children: [] },
]);

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  userId: undefined,
  userNumb: '',
  realName: '',
  consumeDate: undefined,
  mealType: undefined,
  params: {}
});

/** 查询消费明细列表 */
const getList = async () => {
  loading.value = true;
  // queryParams.value.params = {};
  proxy?.addDateRange(queryParams, dateRangeConsumeDate.value, 'ConsumeDate');
  const res = await listXfConsumedetail(queryParams);
  xfConsumedetailList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};


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

/** 重置按钮操作 */
const resetQuery = () => {
  dateRangeConsumeDate.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: XfConsumedetailVO[]) => {
  ids.value = selection.map((item) => item.consumeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 删除按钮操作 */
const handleDelete = async (row?: XfConsumedetailVO) => {
  const _consumeIds = row?.consumeId || ids.value;
  await proxy?.$modal.confirm('是否确认删除消费明细编号为"' + _consumeIds + '"的数据项？').finally(() => (loading.value = false));
  await delXfConsumedetail(_consumeIds);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'backstage/consumption/xfConsumedetail/export',
    {
      ...queryParams
    },
    `xfConsumedetail_${new Date().getTime()}.xlsx`);
};
/** 弹出框(增加、修改) */
const detailFormRef = ref();
const openForm = (type: string, row?: XfConsumedetailVO) => {
  const id = row?.consumeId || ids.value[0];
  detailFormRef?.value.open(type, id);
};
onMounted(() => {
  getList();
});
</script>
