<template>
  <div class="p-2">
    <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" :model="queryParams" :inline="true">
            <el-form-item label="对账单号" prop="reconNo">
              <el-input v-model="queryParams.reconNo" placeholder="请输入对账单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="申请日期" prop="applyDate">
              <el-date-picker clearable v-model="queryParams.applyDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择申请日期" />
            </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>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['bill:reconApplication:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['bill:reconApplication:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['bill:reconApplication:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['bill:reconApplication:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="reconApplicationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" v-if="true" />
        <el-table-column label="对账单号" align="center" prop="reconNo" />
        <el-table-column label="总金额" align="center" prop="totalAmount" />
        <el-table-column label="申请日期" align="center" prop="applyDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="申请状态" align="center" prop="status" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleAudit(scope.row)" v-hasPermi="['bill:reconApplication:edit']">审核</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bill:reconApplication:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改对账申请抽屉 -->
    <ReconApplyDrawer v-model="drawerVisible" :title="drawerTitle" :form-data="currentFormData" @success="handleDrawerSuccess" />
  </div>
</template>

<script setup name="ReconApplication" lang="ts">
import { listReconApplication, getReconApplication, delReconApplication } from '@/api/bill/reconApplication';
import ReconApplyDrawer from './reconApplyDrawer.vue';
import { ReconApplicationVO, ReconApplicationQuery, ReconApplicationForm } from '@/api/bill/reconApplication/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const reconApplicationList = ref<ReconApplicationVO[]>([]);
const drawerVisible = ref(false);
const drawerTitle = ref('');
const currentFormData = ref<ReconApplicationForm>();
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const data = reactive<PageData<ReconApplicationForm, ReconApplicationQuery>>({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    reconNo: undefined,
    totalAmount: undefined,
    applyDate: undefined,
    orderIds: undefined,
    status: undefined,
    platformCode: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams } = toRefs(data);

/** 查询对账申请列表 */
const getList = async () => {
  loading.value = true;
  const res = await listReconApplication(queryParams.value);
  reconApplicationList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 抽屉成功回调 */
const handleDrawerSuccess = () => {
  getList();
};

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  currentFormData.value = undefined;
  drawerTitle.value = '添加对账申请';
  drawerVisible.value = true;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ReconApplicationVO) => {
  const _id = row?.id || ids.value[0];
  const res = await getReconApplication(_id);
  currentFormData.value = res.data;
  drawerTitle.value = '修改对账申请';
  drawerVisible.value = true;
};

/** 修改按钮操作 */
const handleAudit = async (row?: ReconApplicationVO) => {
  const _id = row?.id || ids.value[0];
};

/** 删除按钮操作 */
const handleDelete = async (row?: ReconApplicationVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除对账申请编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delReconApplication(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'bill/reconApplication/export',
    {
      ...queryParams.value
    },
    `reconApplication_${new Date().getTime()}.xlsx`
  );
};

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