<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" v-loading="loading" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80px">
            <el-row>
              <el-form-item label="管理机构" prop="manageCom">
                <el-select v-model="queryParams.manageCom" placeholder="请输入管理机构">
                  <template #header>
                    <el-input v-model="likeName" placeholder="可根据机构编码或名称模糊查询" @input="filterByManageCom"></el-input>
                  </template>
                  <el-option
                    v-for="dict in filteredManagecom"
                    :key="dict.dictValue"
                    :label="dict.dictValue + '-' + dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="凭证大类" prop="certificateId">
                <el-select v-model="queryParams.certificateId" clearable placeholder="请选择凭证大类">
                  <el-option v-for="dict in certificateList" :key="dict.certificateId" :label="dict.certificateName" :value="dict.certificateId" />
                </el-select>
              </el-form-item>
              <el-form-item label="凭证细类" prop="costId">
                <el-select v-model="queryParams.costId" placeholder="请选择凭证大类" clearable>
                  <el-option v-for="dict in costList" :key="dict.costId" :label="dict.costName" :value="dict.costId" />
                </el-select>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="记账日期" prop="arrivalDate">
                <el-date-picker
                  v-model="dateRange"
                  value-format="YYYY-MM-DD"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </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-form>
        </el-card>

        <el-card shadow="hover">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
            </el-row>
          </template>
          <el-table v-loading="loading" :data="costResultList">
            <el-table-column label="凭证大类" align="center" prop="certificateId" width="80px" show-overflow-tooltip/>
            <el-table-column label="凭证大类描述" align="center" prop="certificateName" width="200px" show-overflow-tooltip>
              <template #default="scope">
                {{ getCertificateNameById(scope.row.certificateId) }}
              </template>
            </el-table-column>
            <el-table-column label="明细科目" align="center" prop="costId" width="100px" show-overflow-tooltip/>
            <el-table-column label="明细科目描述" align="center" prop="costName" width="260px" show-overflow-tooltip>
              <template #default="scope">
                {{ getCostNameById(scope.row.costId) }}
              </template>
            </el-table-column>
            <el-table-column label="借贷标记" align="center" prop="finItemType" />
            <el-table-column label="业务号码" align="center" prop="businessNo" width="260px" show-overflow-tooltip/>
            <el-table-column label="金额" align="center" prop="amount" width="80px" show-overflow-tooltip />
            <el-table-column label="业务日期" align="center" prop="accountDate" width="160px" show-overflow-tooltip/>
            <el-table-column label="状态" align="center" prop="status" />
            <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-tooltip content="回滚" placement="top">
                  <el-button v-hasPermi="['bfs:datarollback:one']" link type="primary" icon="RefreshLeft" @click="handleRollback(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>
    </transition>
  </div>
</template>
<script setup name="AccountingCertification" lang="ts">
import {
  datatransRollbackByOne,
  getCacheList,
  getCacheListCertify,
  getDatatransList
} from '@/api/bfs/busnessQury/costBalance';
import {queryManageCom} from "@/api/bfs/cache";

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

const showSearch = ref(true);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    manageCom: undefined,
    certificateId: undefined,
    costId: undefined,
    params: {}
  }
});
const { queryParams } = toRefs(data);
const certificateList = ref([]);
const costList = ref([]);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const loading = ref(false);
const costResultList = ref([]);
const total = ref(0);
const likeName = ref('');
const filteredManagecom = ref([]);
const sys_managecomCache = ref([]);

/** 查询字典下拉信息 */
const getCache = async () => {
  const res = await getCacheListCertify();
  certificateList.value = res.data.certificateList;
  costList.value = res.data.costList;
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 查询科目明细 */
const getList = async () => {
  loading.value = true;
  const res = await getDatatransList(proxy?.addDateRange(queryParams.value, dateRange.value));
  costResultList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
const getCertificateNameById = (id) => {
  const item = certificateList.value.find((item) => item.certificateId === id);
  return item ? item.certificateName : '';
};

const getCostNameById = (id) => {
  const item = (costList.value || []).find((item) => item.costId === id);
  return item ? item.costName : '';
};

const handleRollback = async (row) => {
  const dictIds = row?.id;
  await proxy?.$modal.confirm('是否确认回滚"' + dictIds + '"的数据项？同一原始数据的业财明细将一并回滚。');
  if (row.status === '9') {
    proxy?.$modal.msgError('凭证明细已过账，请红冲处理');
    return false;
  }
  const res = await datatransRollbackByOne(row);
  if (res.code == 200) {
    proxy?.$modal.msgSuccess('回滚成功');
  } else {
    proxy?.$modal.msgError(res.msg);
  }
  getList();
};

/** 模糊查询 */
const filterByManageCom = () => {
  if (likeName.value) {
    filteredManagecom.value = sys_managecomCache.value.filter((item) => {
      return item.dictLabel.includes(likeName.value) || item.dictValue.includes(likeName.value);
    });
  } else {
    filteredManagecom.value = sys_managecomCache.value;
  }
};
const getCacheManageCom = async () => {
  const res = await queryManageCom();
  if (res.code == 200) {
    sys_managecomCache.value = res.data;
    filteredManagecom.value = sys_managecomCache.value;
  }
};

onMounted(() => {
  getCache();
  getCacheManageCom();
});
</script>

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