<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="180px">
            <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.value"
                    :label="dict.value + '-' + dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="凭证大类" prop="certificateId">
                <el-select v-model="queryParams.certificateId" placeholder="请选择凭证大类">
                  <el-option v-for="dict in certificateList" :key="dict.certificateId" :label="dict.certificateName" :value="dict.certificateId" />
                </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 v-hasPermi="['bfs:accounting:accounting']" type="primary" icon="Search" @click="handleQuery">制 证</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
  </div>
</template>
<script setup name ="AccountingCertification" lang="ts">

import {accountingCertificateId, getCacheList, getCacheListCertify} from '@/api/bfs/busnessQury/costBalance';

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,
    params: {}
  }
});
const { queryParams } = toRefs(data);
const certificateList = ref([]);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const loading = ref(false);
const likeName = ref('');
const filteredManagecom = ref([]);

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


/** 重置按钮操作 */
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 accountingCertificateId(proxy?.addDateRange(queryParams.value, dateRange.value));
  if (res.code == 200) {
    proxy?.$modal.msgSuccess('已开始制证，请等待处理结果');
  } else {
    proxy?.$modal.msgError(res.msg);
  }
  loading.value = false;
};

/** 模糊查询 */
const filterByManageCom = () => {
  if (likeName.value) {
    filteredManagecom.value = sys_managecom.value.filter((item) => {
      return item.label.includes(likeName.value) || item.value.includes(likeName.value);
    });
  } else {
    filteredManagecom.value = sys_managecom.value;
  }
};

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

<style scoped lang="scss">

</style>
