<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="subsidyName">
            <el-input v-model="queryParams.subsidyName" placeholder="请输入补助名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="补助类型" prop="subsidyType">
            <el-select v-model="queryParams.subsidyType" placeholder="请选择补助类型" clearable>
              <el-option v-for="dict in SUBTYPE" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </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>
            <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:ptSubsidy:add']" type="primary" plain icon="Plus" @click="openForm('create')">新增</el-button>
          </el-col>
          <!--<el-col :span="1.5">
            <el-button v-hasPermi="['system:post:edit']" type="success" plain icon="Edit" :disabled="single" @click="openForm('update')">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['subsidy:ptSubsidy:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button v-hasPermi="['subsidy:ptSubsidy: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="ptSubsidyList" max-height="calc(100vh - 23.75rem)" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="ID" align="center" prop="mainId" />
        <el-table-column v-if="columns[1].visible" label="补助名称" align="center" prop="subsidyName">
          <template #default="scope">
            <el-link type="primary" @click="openForm('update', scope.row, false)">{{ scope.row.subsidyName }}</el-link>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[2].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[3].visible" label="补助金额" align="center" prop="fillMoney" />
        <el-table-column v-if="columns[4].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[5].visible" label="入账钱包" align="center" prop="bagCode">
          <template #default="scope">
            <dict-tag :options="CARDBAGTYPE" :value="scope.row.bagCode" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[6].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[7].visible" label="审核状态" align="center" prop="auditStatus">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.auditStatus" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[8].visible" label="审核人" align="center" prop="auditUser" />
        <el-table-column v-if="columns[9].visible" label="审核时间" align="center" prop="auditDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.auditDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[10].visible" label="补助说明" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button
              v-if="scope.row.auditStatus == 'N'"
              v-hasPermi="['subsidy:ptSubsidy:audit']"
              link
              type="primary"
              @click="handleAudit(scope.row)"
              >审核</el-button
            >
            <el-button
              v-if="scope.row.auditStatus == 'N'"
              v-hasPermi="['subsidy:ptSubsidy:edit']"
              link
              type="primary"
              icon="Edit"
              @click="openForm('update', scope.row, true)"
            ></el-button>
            <el-button
              v-if="scope.row.auditStatus == 'N'"
              v-hasPermi="['subsidy:ptSubsidy:remove']"
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </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="PtSubsidy" lang="ts">
/** 模块导入 */
import { listPtSubsidy, delPtSubsidy, updatePtSubsidy, getCurrentDateString } from '@/api/cardCenter/subsidy/ptSubsidy';
import { PtSubsidyVO } from '@/api/cardCenter/subsidy/ptSubsidy/types';
import DetailForm from './DetailForm.vue';
import { getInfo } from '@/api/login';
import { cloneDeep } from 'lodash';
defineOptions({ name: 'PtSubsidy' });

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

// 查询窗体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,
  subsidyName: '',
  subsidyType: '',
  fillDate: undefined,
  fillStatus: '',
  params: {}
});

/** 查询补助设置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPtSubsidy(queryParams);
  ptSubsidyList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

/** 审核按钮操作 */
const handleAudit = async (row?: PtSubsidyVO) => {
  let ptSubsidy = cloneDeep(row);
  console.log(ptSubsidy)
  ptSubsidy.auditStatus = 'Y';
  ptSubsidy.auditDate = getCurrentDateString();
  ptSubsidy.auditUser = (await getInfo()).data.user.phone;
  console.log(ptSubsidy.auditUser)
  await proxy?.$modal.confirm('是否同意发放该补助？').finally(() => (loading.value = false));
  await updatePtSubsidy(ptSubsidy);
  await getList();
  proxy?.$modal.msgSuccess('审核成功');
};

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

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