<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <el-card shadow="never">
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" :rules="rules" label-position="left" label-width="130px">
          <el-row>
            <el-form-item label="申请日期" prop="dateRange">
              <el-date-picker
                v-model="queryParams.dateRange"
                end-placeholder="结束日期"
                range-separator="-"
                start-placeholder="开始日期"
                type="daterange"
                value-format="YYYY-MM-DD"
                @change="handleDateRangeChange"
              >
              </el-date-picker>
            </el-form-item>
          </el-row>

          <el-row>
            <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-row>
        </el-form>
      </el-card>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['bfs:manualvoucher:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="manaulBookkeepingList">
        <el-table-column label="手工记账申请批次号" align="center" prop="manualBatchId" />
        <el-table-column label="申请机构" align="center" prop="manageCom" />
        <el-table-column label="申请时间" align="center" prop="applyDate" />
        <el-table-column label="申请状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="bfs_manaul_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip v-if="scope.row.status === '0'" content="修改" placement="top">
              <el-button v-hasPermi="['bfs:manualvoucher:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip v-if="scope.row.status != '0'" content="查看" placement="top">
              <el-button v-hasPermi="['bfs:manualvoucher:view']" link type="primary" icon="View" @click="handleView(scope.row)"></el-button>
            </el-tooltip>
          </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>
  </div>
</template>

<script setup lang="ts">
import {listApplyManualVoucher, listManualVoucher} from '@/api/bfs/exception/ManaulBookkeeping';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_managecom } = toRefs<any>(proxy?.useDict('sys_managecom'));
const { bfs_manaul_status } = toRefs<any>(proxy?.useDict('bfs_manaul_status'));

const showSearch = ref(true);
const queryFormRef = ref<ElFormInstance>();
const loading = ref(false);
const manaulBookkeepingList = ref([]);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dateRange: undefined
  },
  rules: {}
});
const { queryParams, rules } = toRefs(data);

// 计算属性用于动态设置 picker options
const handleDateRangeChange = (value: [string, string] | null) => {
  if (!value || !value[0] || !value[1]) return;

  const startDate = new Date(value[0]);
  const endDate = new Date(value[1]);

  const maxEndDate = new Date(startDate);
  maxEndDate.setDate(maxEndDate.getDate() + 365);

  if (endDate > maxEndDate) {
    proxy.$modal.msgError('结束日期不能超过开始日期一年以上');
    dateRange.value = ['', ''];
  }
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  // 重置表单字段
  queryFormRef.value?.resetFields();
  // 重置日期范围
  queryFormRef.value.dateRange = ['', ''];
};

const getList = async () => {
  loading.value = true;
  try {
    const res = await listApplyManualVoucher(proxy?.addDateRange(queryParams.value, queryParams.value.dateRange));
    manaulBookkeepingList.value = res.rows;
    total.value = res.total;
  } finally {
    loading.value = false;
  }
};

/** 新增按钮操作 */
const handleAdd = () => {
  proxy.$router.push('/bfs/exception/manual-voucher-detail/manualBatchId');
};

/** 修改 */
const handleUpdate = (row) => {
  proxy.$router.push({
    path: `/bfs/exception/manual-voucher-detail/manualBatchId`,
    query: {
      manualBatchId: row.manualBatchId
    }
  });
};

/** 查看申请详情 */
const handleView = (row) => {
  proxy.$router.push({
    path: `/bfs/exception/manual-voucher-detail/manualView`,
    query: {
      manualBatchId: row.manualBatchId
    }
  });
};
</script>

<style scoped lang="scss"></style>
