<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" label-width="130px" label-position="left">
            <el-form-item label="管理机构" prop="manageCom">
              <el-select v-model="queryParams.manageCom" 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-form-item label="结算批次号" prop="settlBatchNo">
              <el-input v-model="queryParams.settlBatchNo" placeholder="请输入结算批次号" clearable />
            </el-form-item>
            <el-form-item label="结算方名称" prop="settlVendorName">
              <el-input v-model="queryParams.settlVendorName" placeholder="请输入结算方名称" clearable />
            </el-form-item>
            <el-form-item label="保单号" prop="policyNo">
              <el-input v-model="queryParams.policyNo" placeholder="请输入保单号" clearable />
            </el-form-item>
            <el-form-item label="业务类型" prop="businessType">
              <el-select v-model="queryParams.businessType" clearable placeholder="请输入业务类型">
                <el-option
                  v-for="dict in bfs_business_type"
                  :key="dict.value"
                  :label="dict.value + '-' + dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="处理状态" prop="status">
              <el-select v-model="queryParams.status" clearable placeholder="请输入处理状态">
                <el-option
                  v-for="dict in bfs_invoice_status"
                  :key="dict.value"
                  :label="dict.value + '-' + dict.label"
                  :value="dict.value"
                ></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="never">
      <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="invoiceList" @row-dblclick="rowDbclick">
        <el-table-column label="管理机构" align="center" prop="manageCom" width="120" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="sys_managecom" :value="scope.row.manageCom" />
          </template>
        </el-table-column>
        <el-table-column label="结算批次号" align="center" prop="settlBatchNo" width="150" show-overflow-tooltip />
        <el-table-column label="结算方名称" align="center" prop="settlVendorName" width="180" show-overflow-tooltip>
        </el-table-column>
        <el-table-column align="center" label="保单号" prop="policyNo" width="120" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="结算含税总金额" align="center" prop="settlAmount" width="100" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="结算不含税总金额" align="center" prop="settlIncome" width="100" show-overflow-tooltip />
        <el-table-column label="结算总税额" align="center" prop="settlTax" width="100" show-overflow-tooltip />
        <el-table-column label="核销含税总金额" align="center" prop="writeOfAmount" width="100" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="核销不含税总金额" align="center" prop="writeOfIncome" width="100" show-overflow-tooltip> </el-table-column>
        <el-table-column label="核销总税额" align="center" prop="writeOfTax" width="100">
        </el-table-column>
        <el-table-column label="处理状态" align="center" prop="status" width="120" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="bfs_invoice_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="['bfs:invoice:detail']" link type="primary" icon="Edit" @click="handleDeal(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 {listBusinessInvoice} from "@/api/bfs/invoice";
import {queryManageCom} from "@/api/bfs/cache";
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_managecom } = toRefs<any>(proxy?.useDict('sys_managecom'));
const { bfs_invoice_status } = toRefs<any>(proxy?.useDict('bfs_invoice_status'));
const { bfs_business_type } = toRefs<any>(proxy?.useDict('bfs_business_type'));

const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const likeName = ref('');
const filteredManagecom = ref([]);
const sys_managecomCache = ref([]);
const invoiceList = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    manageCom: undefined,
    settlBatchNo: undefined,
    settlVendorName: undefined,
    grpContNo: undefined,
    businessType: undefined,
    status: undefined,
    params: {}
  },
  rules: {
  }
});
const { queryParams, rules } = toRefs(data);

const getList = async () => {
  loading.value = true;
  const res = await listBusinessInvoice(queryParams.value);
  invoiceList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

/** 模糊查询 */
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;
  }
};

const handleDeal = (row: any) => {
  proxy.$router.push({
    path: '/bfs/Operation/Invoice/detail/invoiceCalbackDetail',
    query: { invoiceId: row.invoiceId }
  });
};

onMounted(() => {
  getCacheManageCom();
  getList();
});

</script>

<style scoped lang="scss">

</style>
