<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="100px">
            <el-form-item label="申请日期" prop="dateRange">
              <el-date-picker
                v-model="queryParams.dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                clearable
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="红冲申请流水号" prop="invoicingId">
              <el-input v-model="queryParams.invoicingId" placeholder="请输入红冲申请流水号"></el-input>
            </el-form-item>

            <el-form-item label="申请机构" prop="applyManageCom">
              <el-select v-model="queryParams.applyManageCom" clearable 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-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>
      </div>
    </transition>
    <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="invoicingList">
        <el-table-column label="红冲申请流水号" align="center" prop="invoicingId" width="200px" />
        <el-table-column label="红冲原因" align="center" prop="invoicingReason" width="120px">
          <template #default="scope">
            <dict-tag :options="bfs_invoicing_reason" :value="scope.row.invoicingReason" />
          </template>
        </el-table-column>
        <el-table-column label="红冲原因描述" align="center" prop="invoicingDetail" width="300px" />
        <el-table-column label="申请人" align="center" prop="applyName" width="150px" />
        <el-table-column label="申请机构" align="center" prop="applyManageCom" width="150px" >
          <template #default="scope">
            <dict-tag :options="sys_managecom" :value="scope.row.applyManageCom" />
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" prop="applyTime" width="200px" />
        <el-table-column label="状态" align="center" prop="status" >
          <template #default="scope">
            <dict-tag :options="bfs_invoicing_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 content="审核" placement="top">
              <el-button
                v-hasPermi="['bls:invoicing:audit']"
                link
                type="primary"
                icon="Edit"
                @click="handleAudit(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 { getInvoicingList } from '@/api/bfs/busnessQury/redInvoicing';
import {queryManageCom} from "@/api/bfs/cache";

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

const showSearch = ref(true);
const loading = ref(false);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    invoicingType: undefined,
    invoicingId: undefined,
    applyManageCom: undefined,
    status: '1',
    dateRange: undefined
  },
  rules: {}
});

const { queryParams, rules } = toRefs(data);
const queryFormRef = ref<ElFormInstance>();
const invoicingList = ref([]);
const total = ref(0);
const likeName = ref('');
const filteredManagecom = ref([]);
const sys_managecomCache = ref([]);

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

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

const handleAudit = (row: any) => {
  proxy.$router.push({
    path: '/bfs/Operation/invoicing/invoiceAudit',
    query: { invoicingId: row.invoicingId }
  });
};

/** 模糊查询 */
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(() => {
  getCacheManageCom();
  getList();
});

</script>

<style scoped lang="scss">

</style>
