<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch">
        <el-card shadow="hover" class="mb-[10px]">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="项目名称" prop="productName">
              <el-select v-model="queryParams.productName" placeholder="请选择项目">
                <el-option v-for="project in projectLists" :key="project.productName" :label="project.productName" :value="project.productName" />
              </el-select>
            </el-form-item>
            <el-form-item label="项目阶段" prop="reviewSn">
              <el-select v-model="queryParams.reviewSn" placeholder="请选择项目阶段">
                <el-option v-for="item in item_stage" :key="item.value" :label="`${item.label}阶段`" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="专家姓名" prop="expertName">
              <el-select v-model="queryParams.expertName" filterable placeholder="请选择专家姓名" clearable>
                <el-option
                  v-for="project in expertLists"
                  :key="project.expertName"
                  :label="`${project.workUnit} - ${project.expertName}（${project.contact}）`"
                  :value="project.expertName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="开票情况" prop="invStu">
              <el-select v-model="queryParams.invStu" placeholder="请选择开票情况">
                <el-option v-for="item in invoicing_status" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <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="never">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="warning" plain @click="exportExcel">导出</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="tableRows">
            <el-table-column label="序号" type="index" width="50px"></el-table-column>
            <el-table-column label="项目名称" prop="productName" />
            <el-table-column label="项目阶段" prop="reviewSn" />
            <el-table-column label="专家姓名" prop="expertName" />
            <el-table-column label="结算单号" prop="settlementSn" />
            <el-table-column label="结算金额" prop="settlementMoney">
              <template #default="{ row }"> {{ row.settlementMoney }}元 </template>
            </el-table-column>
            <el-table-column label="开票情况" prop="invStu"/>
            <el-table-column label="开票金额" prop="invoiceMoney">
              <template #default="{ row }"> {{ row.invoiceMoney }}元 </template>
            </el-table-column>
          </el-table>

          <pagination v-show="page.total > 0" :total="page.total" v-model:page="page.pageNum" v-model:limit="page.pageSize" @pagination="getList" />
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { listProject } from '@/api/project/review';
import { expertList } from '@/api/expert/portrait';
import { getDetailList } from "@/api/expertMoneyConfig/statistics"
const route = useRoute();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { item_stage } = toRefs<any>(proxy?.useDict('item_stage'));
const { invoicing_status } = toRefs<any>(proxy?.useDict('invoicing_status'));
const showSearch = ref(true);

const id = computed(() => route.query.id);

const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryFormRef = ref();
const queryParams = ref<any>({
  productName: '',
  reviewSn: '',
  expertName: '',
  invStu: ''
});
const projectLists = ref([]);
const getProjectLists = async () => {
  const res = await listProject({
    pageNum: 1,
    pageSize: 100000000
  });
  projectLists.value = [...new Map(res.rows.map((item) => [item.productId, item])).values()];
};
const expertLists = ref([]);
const getExpertLists = async () => {
  let res = await expertList();
  expertLists.value = res.data || [];
};
/** 搜索按钮操作 */
const handleQuery = () => {
  page.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const tableRows = ref([]);
const loading = ref(false);
const getList = async () => {
  loading.value = true;
  let res = await getDetailList({
    id: id.value,
    ...queryParams.value,
    ...page.value
  }).finally(() => {
    loading.value = false;
  })
  tableRows.value = res.rows || []
};
const exportExcel = () => {
  proxy?.download('/zjk-api/invReport/export', {
    id: id.value,
    ...queryParams.value
  }, `发票报表详情_${new Date().getTime()}.xlsx`)
};
watchEffect(() => {
  if (!id.value) return;
  getList();
  getProjectLists();
  getExpertLists();
});
</script>
