<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="98px" class="-mb-15px">
          <el-form-item label="预计到账时间" style="width: 308px">
            <el-date-picker
              v-model="dateRangeFillDate"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            />
          </el-form-item>
          <el-form-item label="领取状态" prop="fillStatus">
            <el-select v-model="queryParams.fillStatus" placeholder="请选择领取状态" clearable>
              <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </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="部门名称" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </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="['subsidy:ptSubsidyitem: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="ptSubsidyitemList" max-height="calc(100vh - 23.75rem)" @selection-change="handleSelectionChange">
        <el-table-column v-if="false" label="ID" align="center" prop="itemId" />
        <el-table-column v-if="columns[6].visible" label="用户姓名" align="center" prop="realName" />
        <el-table-column v-if="columns[7].visible" label="部门名称" align="center" prop="deptName" />
        <el-table-column v-if="columns[1].visible" label="补助类型" align="center" prop="subsidyType">
          <template #default="scope">
            <dict-tag :options="SUBTYPE" :value="scope.row.subsidyType" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[2].visible" label="补助金额" align="center" prop="fillMoney" />
        <el-table-column v-if="columns[3].visible" label="预计到账时间" align="center" prop="fillDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.fillDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[4].visible" label="领取状态" align="center" prop="fillStatus">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.fillStatus" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[5].visible" label="领取时间" align="center" prop="getDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.getDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[8].visible" label="补助说明" align="center" prop="remark" />
        <el-table-column v-if="columns[9].visible" label="创建者" align="center" prop="createByName" />
        <el-table-column v-if="columns[10].visible" label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </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="PtSubsidyitem" lang="ts">
/** 模块导入 */
import { listPtSubsidyitem, delPtSubsidyitem } from '@/api/cardCenter/subsidy/ptSubsidyitem';
import { PtSubsidyitemVO } from '@/api/cardCenter/subsidy/ptSubsidyitem/types';
import DetailForm from './DetailForm.vue';

defineOptions({ name: 'PtSubsidyitem' });

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

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

const columns = ref<FieldOption[]>([
  { key: 0, label: `明细Id,主键`, visible: true, children: [] },
  { key: 1, label: `补助类型`, 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: [] }
]);

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  fillDate: undefined,
  fillStatus: '',
  realName: '',
  deptName: '',
  params: {}
});

/** 查询补助明细列表 */
const getList = async () => {
  loading.value = true;
  queryParams.params = {};
  proxy?.addDateRange(queryParams, dateRangeFillDate.value, 'FillDate');
  const res = await listPtSubsidyitem(queryParams);
  ptSubsidyitemList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

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